diff --git a/_plugins/image.rb b/_plugins/image.rb index 81bae4c1..36330901 100644 --- a/_plugins/image.rb +++ b/_plugins/image.rb @@ -2,11 +2,49 @@ module Tags class ImageTag < Liquid::Tag def render(context) - src = @markup.strip - baseurl = context.registers[:site].config['baseurl'] - " - - " + 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}\" + +
#{caption}
+
" + end end end end