Class: Jekyll::Spaceship::VideoProcessor

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

Constant Summary

Constants inherited from Processor

Processor::DEFAULT_PRIORITY, Processor::PRIORITY_MAP

Instance Attribute Summary

Attributes inherited from Processor

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

Instance Method Summary collapse

Methods inherited from Processor

#after_exclude, #converter, #dispatch, exclude, #ext, #initialize, #initialize_exclusions, #initialize_priority, #initialize_register, #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

Instance Method Details

#handle_dailymotion(content) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/jekyll-spaceship/processors/video-processor.rb', line 41

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

#handle_video(content, data) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/jekyll-spaceship/processors/video-processor.rb', line 49

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

  iframe_url = data[:iframe_url]
  id = 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 = 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

    width = qs['width'] || data[:width] || 600
    height = qs['height'] || data[:height] || 400
    style = "max-width: 100%" if width.nil?

    url = URI("#{iframe_url}#{id}").tap do |v|
      v.query = URI.encode_www_form(qs) if qs.size > 0
    end

    html = "<iframe \
      src=\"#{url}\" \
      title=\"#{title}\" \
      width=\"#{width}\" \
      height=\"#{height}\" \
      style=\"#{style}\" \
      allow=\"autoplay; encrypted-media\" \
      frameborder=\"0\" \
      allowfullscreen=\"\">\
      </iframe>"

    content = content.gsub(match_data[0], html)
    self.handled = true
  end
  content
end

#handle_vimeo(content) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/jekyll-spaceship/processors/video-processor.rb', line 28

def handle_vimeo(content)
  handle_video(content, {
    host: '(https?:)?\\/\\/vimeo\\.com\\/',
    id: '([0-9]+)',
    iframe_url: "https://player.vimeo.com/video/",
    width: 600,
    height: 350
  })
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)



17
18
19
20
21
22
23
# File 'lib/jekyll-spaceship/processors/video-processor.rb', line 17

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

#on_handle_markdown(content) ⇒ Object



7
8
9
10
11
# File 'lib/jekyll-spaceship/processors/video-processor.rb', line 7

def on_handle_markdown(content)
  content = handle_youtube(content)
  content = handle_vimeo(content)
  content = handle_dailymotion(content)
end