Created
March 7, 2015 12:39
-
-
Save liwei78/3c55844a4479ec59f347 to your computer and use it in GitHub Desktop.
Liquid::Block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Jekyll | |
class FigureTag < Liquid::Block | |
def initialize(tag_name, markup, tokens) | |
super | |
@attributes = {} | |
markup.scan(Liquid::TagAttributes) do |key, value| | |
@attributes[key] = value.gsub(/^'|"/, '').gsub(/'|"$/, '') | |
end | |
end | |
def render(context) | |
site = context.registers[:site] | |
converter = site.getConverterImpl(::Jekyll::Converters::Markdown) | |
figure_classes = @attributes['class'].to_s | |
figure_main = converter.convert(super(context)) | |
figure_caption = converter.convert(@attributes['caption'].to_s) | |
# Render <figure> | |
if @attributes['class'] | |
source = "<figure class=\"figure #{figure_classes}\">" | |
else | |
source = "<figure class=\"figure\">" | |
end | |
source += "<div class=\"figure__main\">#{figure_main}</div>" | |
if @attributes['caption'] | |
source += "<figcaption class=\"figure__caption\">#{figure_caption}</figcaption>" | |
source += "</figure>" | |
else | |
source += "</figure>" | |
end | |
end | |
end | |
end | |
Liquid::Template.register_tag('figure', Jekyll::FigureTag) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment