From 6376e65fb7fcac8c60803b0835f365aed4e5e420 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 28 Apr 2020 07:29:31 -0400 Subject: [PATCH] update imagine plugin to support captions --- _plugins/image.rb | 48 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 5 deletions(-) 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