Class: Jekyll::OEmbedPlugin

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

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, text, tokens) ⇒ OEmbedPlugin



22
23
24
25
# File 'lib/jekyll_oembed.rb', line 22

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

Instance Method Details

#error_message(url) ⇒ Object



47
48
49
50
51
52
# File 'lib/jekyll_oembed.rb', line 47

def error_message(url)
  # Silent error. This seems preferable to a hard fail
  # because a user could make a URL private anytime.
  puts "OEmbedError: The url, #{url}, is not available as an oembed. " \
    'Consider using an HTML embed instead.'.red
end

#html_output(result) ⇒ Object



42
43
44
45
# File 'lib/jekyll_oembed.rb', line 42

def html_output(result)
  "<div class=\"embed-container #{result.fields['type']} " \
    "#{result.fields['provider']}\">#{result.html}</div>"
end

#render(context) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jekyll_oembed.rb', line 27

def render(context)
  # Pipe param through liquid to make additional replacements possible
  url = Liquid::Template.parse(@text).render context
  url = url.strip! || url.strip
  begin
    # OEmbed look up
    result = ::OEmbed::Providers.get(url)

    html_output(result)
  rescue
    error_message(url)
    ''
  end
end