Class: Jekyll::Spaceship::MediaProcessor

Inherits:
Processor
  • Object
show all
Defined in:
lib/jekyll-spaceship/processors/media-processor.rb

Constant Summary

Constants inherited from Processor

Processor::DEFAULT_PRIORITY, Processor::PRIORITY_MAP

Instance Attribute Summary

Attributes inherited from Processor

#config, #exclusions, #handled, #logger, #page, #priority, #registers

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Processor

#after_exclude, #converter, #dispatch, escape_html, exclude, #ext, fetch_img_data, #filename, #initialize, #initialize_exclusions, #initialize_priority, #initialize_register, make_img_tag, #name, #on_handle_html, #on_handle_html_block, #on_handled, #output_ext, #pre_exclude, priority, #process?, register

Constructor Details

This class inherits a constructor from Jekyll::Spaceship::Processor

Class Method Details

.configObject



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/jekyll-spaceship/processors/media-processor.rb', line 7

def self.config
  {
    'default' => {
      'id' => 'media-{id}',
      'class' => 'media',
      'width' => '100%',
      'height' => 350,
      'frameborder' => 0,
      'style' => 'max-width: 600px;outline: none',
      'allow' => 'encrypted-media; picture-in-picture'
    }
  }
end

Instance Method Details

#get_id_from_html(url, pattern) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/jekyll-spaceship/processors/media-processor.rb', line 217

def get_id_from_html(url, pattern)
  id = ''
  begin
    url = 'https:' + url if url.start_with? '//'
    res = Net::HTTP.get_response URI(url)
    raise res.body unless res.is_a?(Net::HTTPSuccess)
    res.body.match pattern do |match_data|
      id = match_data[0]
      break
    end
  rescue StandardError => msg
    data = url
    logger.log msg
  end
  id
end

#handle_audio(content, data) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/jekyll-spaceship/processors/media-processor.rb', line 183

def handle_audio(content, data)
  cfg = data[:cfg]
  html = "<audio"\
    " id=\"#{cfg['id']}\""\
    " class=\"#{cfg['class']}\""\
    " #{cfg['autoplay'] ? 'autoplay' : ''}"\
    " #{cfg['loop'] ? 'loop' : ''}"\
    " src=\"#{cfg['src']}\""\
    " style=\"#{cfg['style']}\""\
    " controls>" \
    "<p> Your browser doesn't support HTML5 audio."\
    " Here is a <a href=\"#{cfg['src']}\">link to download the audio</a>"\
    "instead. </p>"\
    "</audio>"
  content.gsub(data[:target], html)
end

#handle_dailymotion(content) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/jekyll-spaceship/processors/media-processor.rb', line 83

def handle_dailymotion(content)
  handle_media(content, {
    media_type: 'iframe',
    host: '(https?:)?\\/\\/.*dai.?ly.*',
    id: '(?<=video\\/|\\/)([a-zA-Z0-9\\_\\-]+)',
    base_url: "https://www.dailymotion.com/embed/video/"
  })
end

#handle_iframe(content, data) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/jekyll-spaceship/processors/media-processor.rb', line 200

def handle_iframe(content, data)
  cfg = data[:cfg]
  html = "<iframe"\
    " id=\"#{cfg['id']}\""\
    " class=\"#{cfg['class']}\""\
    " src=\"#{cfg['src']}\""\
    " title=\"#{cfg['title']}\""\
    " width=\"#{cfg['width']}\""\
    " height=\"#{cfg['height']}\""\
    " style=\"#{cfg['style']}\""\
    " allow=\"#{cfg['allow']}\""\
    " frameborder=\"#{cfg['frameborder']}\""\
    " allowfullscreen>"\
    "</iframe>"
  content.gsub(data[:target], html)
end

#handle_media(content, data) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/jekyll-spaceship/processors/media-processor.rb', line 119

def handle_media(content, data)
  host = data[:host]
  return content if content.sub(/#{host}/, '').nil?

  media_type = data[:media_type]
  base_url = data[:base_url]
  id = data[:id_from] === 'html' ? '()' : data[:id]
  url = "(#{host}#{id}\\S*)"
  title = '("(.*)".*){0,1}'

  # pre-handle reference-style links
  regex = /(\[(.*)\]:\s*(#{url}\s*#{title}))/
  content.scan regex do |match_data|
    match = match_data[0]
    ref_name = match_data[1]
    ref_value = match_data[2]
    content = content.gsub(match, '')
      .gsub(/\!\[(.*)\]\s*\[#{ref_name}\]/,
        "![\1](#{ref_value})")
  end

  # handle inline-style links
  regex = /(\!\[(.*)\]\(.*#{url}\s*#{title}\))/
  content.scan regex do |match_data|
    url = match_data[2]
    id = data[:id_from] === 'html' \
      ? get_id_from_html(url, data[:id]) \
      : match_data[4]
    title = match_data[6]
    qs = url.match(/(?<=\?)(\S*?)$/)
    qs = Hash[URI.decode_www_form(qs.to_s)].reject do |k, v|
      next true if v == id or v == ''
    end

    cfg = self.config['default'].clone
    cfg['id'] = qs['id'] || cfg['id']
    cfg['class'] = qs['class'] || cfg['class']
    cfg['style'] = qs['style'] || cfg['style']
    cfg['id'] = cfg['id'].gsub('{id}', id)
    cfg['class'] = cfg['class'].gsub('{id}', id)

    cfg['src'] = URI(base_url ? "#{base_url}#{id}" : url).tap do |v|
      v.query = URI.encode_www_form(qs) if qs.size > 0
    end

    case media_type
    when 'audio'
      cfg['autoplay'] = qs['autoplay'] || data[:autoplay] || cfg['autoplay']
      cfg['loop'] = qs['loop'] || data[:loop] || cfg['loop']
      cfg['style'] += ';display: none;' if qs['hidden']
      content = handle_audio(content, { target: match_data[0], cfg: cfg })
    when 'iframe'
      cfg['title'] = title
      cfg['width'] = qs['width'] || data[:width] || cfg['width']
      cfg['height'] = qs['height'] || data[:height] || cfg['height']
      cfg['frameborder'] = qs['frameborder'] || cfg['frameborder']
      cfg['allow'] ||= cfg['allow']
      content = handle_iframe(content, { target: match_data[0], cfg: cfg })
    end
    self.handled = true
  end
  content
end

#handle_normal_audio(content) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/jekyll-spaceship/processors/media-processor.rb', line 34

def handle_normal_audio(content)
  handle_media(content, {
    media_type: 'audio',
    host: '(https?:)?\\/\\/.*\\/',
    id: '(.+?\\.(mp3|wav|ogg|mid|midi|aac|wma))',
  })
end

#handle_normal_video(content) ⇒ Object

Examples: ![video](//www.html5rocks.com/en/tutorials/video/basics/devstories.webm) ![video](//techslides.com/demos/sample-videos/small.ogv?allow=autoplay) ![video](//techslides.com/demos/sample-videos/small.mp4?width=400)



47
48
49
50
51
52
53
# File 'lib/jekyll-spaceship/processors/media-processor.rb', line 47

def handle_normal_video(content)
  handle_media(content, {
    media_type: 'iframe',
    host: '(https?:)?\\/\\/.*\\/',
    id: '(.+?\\.(avi|mp4|webm|ogg|ogv|flv|mkv|mov|wmv|3gp|rmvb|asf))'
  })
end

#handle_soundcloud(content) ⇒ Object

Examples: ![soundcloud](//soundcloud.com/aviciiofficial/preview-avicii-vs-lenny)



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/jekyll-spaceship/processors/media-processor.rb', line 107

def handle_soundcloud(content)
  handle_media(content, {
    media_type: 'iframe',
    id_from: 'html',
    host: '(https?:)?\\/\\/soundcloud\\.com\\/.+\\/[^\\?]+',
    id: '(?<=soundcloud:\\/\\/sounds:)([0-9]+)',
    base_url: "https://w.soundcloud.com/player/?url="\
      "https%3A//api.soundcloud.com/tracks/",
    height: 125,
  })
end

#handle_spotify(content) ⇒ Object

Examples: ![spotify](//open.spotify.com/track/4Dg5moVCTqxAb7Wr8Dq2T5) ![spotify](//open.spotify.com/track/37mEkAaqCE7FXMvnlVA8pp?width=400)



95
96
97
98
99
100
101
102
103
# File 'lib/jekyll-spaceship/processors/media-processor.rb', line 95

def handle_spotify(content)
  handle_media(content, {
    media_type: 'iframe',
    host: '(https?:)?\\/\\/open\\.spotify\\.com\\/track\\/',
    id: '(?<=track\\/)([a-zA-Z0-9\\_\\-]+)',
    base_url: "https://open.spotify.com/embed/track/",
    height: 80
  })
end

#handle_vimeo(content) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/jekyll-spaceship/processors/media-processor.rb', line 71

def handle_vimeo(content)
  handle_media(content, {
    media_type: 'iframe',
    host: '(https?:)?\\/\\/vimeo\\.com\\/',
    id: '([0-9]+)',
    base_url: "https://player.vimeo.com/video/"
  })
end

#handle_youtube(content) ⇒ Object

Examples: ![youtube](www.youtube.com/watch?v=XA2WjJbmmoM “title”) ![youtube](www.youtube.com/embed/w-m_yZCLF5Q) ![youtube](//youtu.be/mEP3YXaSww8?height=100%&width=400)



59
60
61
62
63
64
65
66
# File 'lib/jekyll-spaceship/processors/media-processor.rb', line 59

def handle_youtube(content)
  handle_media(content, {
    media_type: 'iframe',
    host: '(https?:)?\\/\\/.*youtu.*',
    id: '(?<=\\?v\\=|embed\\/|\\.be\\/)([a-zA-Z0-9\\_\\-]+)',
    base_url: "https://www.youtube.com/embed/"
  })
end

#on_handle_markdown(content) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/jekyll-spaceship/processors/media-processor.rb', line 21

def on_handle_markdown(content)
  content = handle_normal_audio(content)
  content = handle_normal_video(content)
  content = handle_youtube(content)
  content = handle_vimeo(content)
  content = handle_dailymotion(content)
  content = handle_spotify(content)
  content = handle_soundcloud(content)
end