Now image tag plugin supports relative urls. Improvements to alk attr and title attr support. Closes #724

This commit is contained in:
Brandon Mathis 2013-03-17 23:52:12 -05:00
parent 4dfdfb80c2
commit 4f9fcbb886

View File

@ -8,7 +8,7 @@
# Examples: # Examples:
# {% img /images/ninja.png Ninja Attack! %} # {% 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 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: # Output:
# <img src="/images/ninja.png"> # <img src="/images/ninja.png">
@ -19,20 +19,25 @@
module Jekyll module Jekyll
class ImageTag < Liquid::Tag class ImageTag < Liquid::Tag
@img = nil
def initialize(tag_name, markup, tokens) def initialize(tag_name, markup, tokens)
attributes = ['class', 'src', 'width', 'height', 'title'] @img = nil
title = nil
alt = nil
attributes = ['class', 'src', 'width', 'height', 'alt']
if markup =~ /(?<class>[\S\s]*?)?\s*?(?<src>(?:https?:\/\/|\/|\S+\/)\S+)(?:\s+(?<width>\d+))?(?:\s+(?<height>\d+))?(?<title>\s+.+)?/i markup =~ /alt:(".+?")\s*/i
alt = $1
markup.sub! /alt:".+?"\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 } @img = attributes.reduce({}) { |img, attr| img[attr] = $~[attr].strip if $~[attr]; img }
if /(?:"|')(?<title>[^"']+)?(?:"|')\s+(?:"|')(?<alt>[^"']+)?(?:"|')/ =~ @img['title'] @img['alt'] = alt unless alt.nil?
@img['title'] = title @img['title'] = title unless title.nil?
@img['alt'] = alt
else
@img['alt'] = @img['title'].gsub!(/"/, '&#34;') if @img['title']
end
@img['class'].gsub!(/"/, '') if @img['class']
end end
super super
end end
@ -41,7 +46,7 @@ module Jekyll
if @img if @img
"<img #{@img.collect {|k,v| "#{k}=\"#{v}\"" if v}.join(" ")}>" "<img #{@img.collect {|k,v| "#{k}=\"#{v}\"" if v}.join(" ")}>"
else 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 end
end end