Class: Jekyll::OEmbedTag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/jekyll_oembed.rb

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, text, tokens) ⇒ OEmbedTag

Returns a new instance of OEmbedTag.



9
10
11
12
# File 'lib/jekyll_oembed.rb', line 9

def initialize(tag_name, text, tokens)
  super
  @text = text
end

Instance Method Details

#render(context) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jekyll_oembed.rb', line 14

def render(context)
  text = Liquid::Template.parse(@text).render context

  params = text.shellsplit
  url = params.shift
  params = Hash[*params.map{|val| val.split('=')}.flatten]

  resource = OEmbed::Providers.get(url, params)
  html = resource.html

  if url =~ /:\/\/(www.youtube.com|youtu.be)\//
    %w{width height}.each do |name|
       if params[name]
        html.gsub! Regexp.new(name+'="\\d+'), name+'="'+params[name]
      end
     end
  end

  # resource.video?, resource.thumbnail_url
  "<div class='oembed #{resource.type}'>#{html}</div>"
rescue OEmbed::NotFound
  warn "No embeddable content at #{url}"
  "<a href='#{url}'>#{url}</a>"
end