Sha256: 82ec78d5295296263ba12515d6df62a1ec0f80358e8c602f9808e3304ac60133

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

module HTML
  class Pipeline
    class YoutubeFilter < TextFilter
      def call
        # This filter converts youtube video's url into youtube iframe.
        #
        # Context options:
        #   :video_width - integer, sets iframe's width
        #   :video_height - integer, sets iframe's height
        #   :video_frame_border - integer, sets iframe border's width
        #   :video_wmode - string, sets iframe's wmode option
        #   :video_autoplay - boolean, whether video should autoplay
        #   :video_hide_related - boolean, whether shows related videos
        regex = /(\s|^|<div>|<br>)(https?:\/\/)(www.)?(youtube\.com\/watch\?v=|youtu\.be\/|youtube\.com\/watch\?feature=player_embedded&v=)([A-Za-z0-9_-]*)(\&\S+)?(\?\S+)?/
        @text.gsub(regex) do
          youtube_id = $5
          close_tag = $1 if ["<br>", "<div>"].include? $1
          width = context[:video_width] || 420
          height = context[:video_height] || 315
          frameborder = context[:video_frameborder] || 0
          wmode = context[:video_wmode]
          autoplay = context[:video_autoplay] || false
          hide_related = context[:video_hide_related] || false
          src = "//www.youtube.com/embed/#{youtube_id}"
          params = []
          params << "wmode=#{wmode}" if wmode
          params << "autoplay=1" if autoplay
          params << "rel=0" if hide_related
          src += "?#{params.join '&'}" unless params.empty?

          %{#{close_tag}<div class="video youtube"><iframe width="#{width}" height="#{height}" src="#{src}" frameborder="#{frameborder}" allowfullscreen></iframe></div>}
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
html-pipeline-youtube-0.1.3 lib/html/pipeline/youtube/youtube_filter.rb