summaryrefslogtreecommitdiff
path: root/lib/banzai/filter/video_link_filter.rb
blob: 0ae885708f7098eeca3a226985381fdb524d398a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
module Banzai
  module Filter

    # HTML Filter that handles video uploads.

    class VideoLinkFilter < HTML::Pipeline::Filter
      include ActionView::Helpers::TagHelper
      include ActionView::Context

      EXTENSIONS = %w(.mov .mp4 .ogg .webm .flv)

      def call
        doc.search('img').each do |el|
          if video?(el)
            el.replace video_node(el)
          end
        end

        doc
      end

      private

      def video?(element)
        EXTENSIONS.include? File.extname(element.attribute('src').value)
      end

      # Return a video tag Nokogiri node
      #
      def video_node(element)
        vtag = content_tag(:video, "", {
                            src: element.attribute('src').value,
                            class: 'video-js', preload: 'auto',
                            controls: true
                            })

        Nokogiri::HTML::DocumentFragment.parse(vtag)
      end
    end

  end
end