From 4f9fcbb8866c974c7873d23d241d9569e6af30c9 Mon Sep 17 00:00:00 2001 From: Brandon Mathis Date: Sun, 17 Mar 2013 23:52:12 -0500 Subject: [PATCH] Now image tag plugin supports relative urls. Improvements to alk attr and title attr support. Closes #724 --- plugins/image_tag.rb | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/plugins/image_tag.rb b/plugins/image_tag.rb index 6fa68dc..4f8a001 100644 --- a/plugins/image_tag.rb +++ b/plugins/image_tag.rb @@ -8,7 +8,7 @@ # Examples: # {% img /images/ninja.png Ninja Attack! %} # {% img left half http://site.com/images/ninja.png Ninja Attack! %} -# {% img left half http://site.com/images/ninja.png 150 150 "Ninja Attack!" "Ninja in attack posture" %} +# {% img left half http://site.com/images/ninja.png 150 150 "Ninja in attack posture" "Ninja Attack!" %} # # Output: # @@ -19,20 +19,25 @@ module Jekyll class ImageTag < Liquid::Tag - @img = nil def initialize(tag_name, markup, tokens) - attributes = ['class', 'src', 'width', 'height', 'title'] + @img = nil + title = nil + alt = nil + attributes = ['class', 'src', 'width', 'height', 'alt'] + + markup =~ /alt:(".+?")\s*/i + alt = $1 + markup.sub! /alt:".+?"\s*/i, '' - if markup =~ /(?[\S\s]*?)?\s*?(?(?:https?:\/\/|\/|\S+\/)\S+)(?:\s+(?\d+))?(?:\s+(?\d+))?(?\s+.+)?/i + markup =~ /title:(".+?")\s*/i + title = $1 + markup.sub! /title:".+?"\s*/i, '' + + if markup =~ /(?<class>[\S\s]*?)?\s*?(?<src>\S+\.\S+)(?:\s+(?<width>\d+))?(?:\s+(?<height>\d+))?\s*("?(?<alt>[^"]+)"?)?/i @img = attributes.reduce({}) { |img, attr| img[attr] = $~[attr].strip if $~[attr]; img } - if /(?:"|')(?<title>[^"']+)?(?:"|')\s+(?:"|')(?<alt>[^"']+)?(?:"|')/ =~ @img['title'] - @img['title'] = title - @img['alt'] = alt - else - @img['alt'] = @img['title'].gsub!(/"/, '"') if @img['title'] - end - @img['class'].gsub!(/"/, '') if @img['class'] + @img['alt'] = alt unless alt.nil? + @img['title'] = title unless title.nil? end super end @@ -41,7 +46,7 @@ module Jekyll if @img "<img #{@img.collect {|k,v| "#{k}=\"#{v}\"" if v}.join(" ")}>" else - "Error processing input, expected syntax: {% img [class name(s)] [http[s]:/]/path/to/image [width [height]] [title text | \"title text\" [\"alt text\"]] %}" + "Error processing input, expected syntax: {% img [class name(s)] path/to/image [width [height]] [alt:\"alt text\"] [title:\"title text\"] %}" end end end