module Jekyll
module Tags
class ImageTag < Liquid::Tag
def render(context)
if tag_contents = determine_arguments(@markup.strip)
src, caption = tag_contents[0], tag_contents[1]
baseurl = context.registers[:site].config['baseurl']
img_tag(src, baseurl, caption)
else
raise ArgumentError.new <<-eos
Syntax error in tag 'image' while parsing the following markup:
#{@markup}
Valid syntax:
for image: {% image icon.png %}
for image with caption: {% image icon.png This is a caption. %}
eos
end
end
private
def determine_arguments(input)
matched = input.split(" ", 2)
if matched.length == 0
false
elsif matched.length > 1
[matched[0].to_s.strip, matched[1].to_s.strip]
else
[matched[0].to_s.strip, ""]
end
end
def img_tag(src, baseurl, caption = nil)
if caption.empty?
""
else
"#{caption}"
end
end
end
end
end
Liquid::Template.register_tag('image', Jekyll::Tags::ImageTag)