Class: Jekyll::ExampleEmbedTag

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

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, text, tokens) ⇒ ExampleEmbedTag

Returns a new instance of ExampleEmbedTag.



5
6
7
8
# File 'lib/jekyll_example_embed.rb', line 5

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

Instance Method Details

#render(context) ⇒ Object



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
# File 'lib/jekyll_example_embed.rb', line 10

def render(context)
  @context = context

  name_parts = @example.split('/')
  lang = name_parts.first
  name = name_parts.last

  coerce_documents!

  document = collection.docs.detect do |document|
    document.relative_path == "_examples/#{@example}"
  end

  if document.nil?
    puts "** ERROR: Could not find example #{@example}\n"
    return
  end

  highlight = context.stack do
    wrapped_content = "{% highlight #{lang} %}#{document.content}{% endhighlight %}"
    Liquid::Template.parse(wrapped_content).render!(context)
  end

  icon = Liquid::Template.parse('{% octicon link-external %}').render(context)

  [ '<section class="example-embed">',
    "<h4>#{name}</h4>",
    '<h5>Source</h5>',
    highlight,
    %{<h5><a href="#{document.url}" target="_new">View Result #{icon}</a></h5>},
    '</section>'
  ].join("\n")
end