Class: HTML::Pipeline::TrixVideoFilter

Inherits:
TextFilter
  • Object
show all
Defined in:
lib/html/pipeline/trix_video/trix_video_filter.rb

Overview

HTML Filter for Trix-compliant video preview filters

Instance Method Summary collapse

Instance Method Details

#callObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/html/pipeline/trix_video/trix_video_filter.rb', line 8

def call
  text_fragment.css("figure").each do |figure|
    service, id = video_information(figure)

    case service
    when "youtube"
      figure.replace "https://youtube.com/watch?v=#{id}"
    when "vimeo"
      figure.replace "https://vimeo.com/#{id}"
    end
  end

  text_fragment.to_s
end

#text_fragmentObject



38
39
40
# File 'lib/html/pipeline/trix_video/trix_video_filter.rb', line 38

def text_fragment
  @fragment ||= Nokogiri::HTML::DocumentFragment.parse(@text)
end

#valid_id(url, regex) ⇒ Object



33
34
35
36
# File 'lib/html/pipeline/trix_video/trix_video_filter.rb', line 33

def valid_id(url, regex)
  return unless url
  url.match(regex)
end

#video_information(figure) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/html/pipeline/trix_video/trix_video_filter.rb', line 23

def video_information(figure)
  url = figure.css("img").first ? figure.css("img").first["src"] : nil

  if match = valid_id(url, youtube_regex)
    [ "youtube", match[1] ]
  elsif match = valid_id(url, vimeo_regex)
    [ "vimeo",   match[1] ]
  end
end

#vimeo_regexObject



46
47
48
# File 'lib/html/pipeline/trix_video/trix_video_filter.rb', line 46

def vimeo_regex
  /i.vimeocdn.com\/video\/[0-9]*_[0-9]{3}x[0-9]{3}.jpg\?r=pad\?id=([0-9]*)/
end

#youtube_regexObject



42
43
44
# File 'lib/html/pipeline/trix_video/trix_video_filter.rb', line 42

def youtube_regex
  /img.youtube.com\/vi\/(\S*)\/\d.jpg/
end