Class: Jekyll::FigureTag

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

Overview

Creates a Liquid figure tag. The figure tag should take this form:

{% figure filename svg,png,pdf 'Your caption here' %}

The first value is the filename, which should be shared across every format of the figure. The second value is a comma-separated list of extensions for the filename. The third value is a quoted caption. The tag will produce an img tag for the first file format in the list of extensions. It will include a caption with links to all the figure formats. If the figures directory is set in _config.yml, then the image and the links will point there.

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, text, tokens) ⇒ FigureTag

Returns a new instance of FigureTag.



20
21
22
23
24
# File 'lib/jekyll_figure.rb', line 20

def initialize(tag_name, text, tokens)
  super
  @text     = text.shellsplit
  @@fig_num = 0
end

Instance Method Details

#render(context) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jekyll_figure.rb', line 26

def render(context)
  @@fig_num += 1

  # If the figures directory is not defined, set dir to site root
  if defined? context.registers[:site].config["figures"]["dir"] 
    dir = context.registers[:site].config["figures"]["dir"]
  else
    dir = ""
  end

  # Should we explicitly enumerate the figures?
  enumeration = ""
  if defined? context.registers[:site].config["figures"]["enumerate"]
    if context.registers[:site].config["figures"]["enumerate"]
      enumeration = "Figure #{@@fig_num}: "
    end
  end

  filename   = @text[0]
  extensions = @text[1].split(",")
  caption    = @text[2]
  img_src    = "#{dir}/#{filename}.#{extensions.first}"
  downloads  = proc {
    d = []
    extensions.each do |ext|
      d.push %Q{<a href="#{dir}/#{filename}.#{ext}">#{ext.upcase}</a>,}
    end
    d.join(" ")[0..-2]
  }


  "<figure id='figure-#{@@fig_num}'>"                                +
  "<a href='#{img_src}'><img src='#{img_src}' alt='#{caption}'></a>" +
  "<figcaption>#{enumeration}"                                       +
  "#{caption} [#{downloads.call}]"                                   +
  "</figcaption>"                                                    +
  "</figure>"

end