@@ -12,38 +12,52 @@ class Tag < Liquid::Tag
1212 @img = nil
1313
1414 def initialize ( tag_name , markup , tokens )
15- if markup =~ /title:['|"](.+?)['|"]/
16- @title = $1. strip
15+ @markup = markup
16+ super
17+ end
18+
19+ def render ( context )
20+ begin
21+ attributes = image ( context ) . collect do |k , v |
22+ "#{ k } =\" #{ v } \" " if v
23+ end . join ( " " )
24+
25+ "<img #{ attributes } >"
26+ rescue
27+ raise "Error processing input, expected syntax: {% img [class name(s)] [http[s]:/]/path/to/image [width [height]] [title text | \" title text\" [\" alt text\" ]] %}"
1728 end
29+ end
1830
19- markup = markup . gsub ( /title:['|"].+?['|"]/ , '' ) . strip
31+ def image ( context )
32+ @markup = process_liquid ( context )
2033
21- if markup =~ /(?<class>\S .*\s +)?(?<src>(?:https?:\/ \/ |\/ |\S +\/ )\S +)(?:\s +(?<width>\d \S +))?(?:\s +(?<height>\d \S +))?(?<alt>\s +.+)?/i
34+ title = /title:['|"](.+?)['|"]/
35+ @title = @markup . scan ( title ) . flatten . compact . last
36+ @markup . gsub! ( title , '' )
37+
38+ if @markup =~ /(?<class>\S .*\s +)?(?<src>(?:https?:\/ \/ |\/ |\S +\/ )\S +)(?:\s +(?<width>\d \S +))?(?:\s +(?<height>\d \S +))?(?<alt>\s +.+)?/i
2239 attributes = [ 'class' , 'src' , 'width' , 'height' , 'alt' ]
23- @img = attributes . reduce ( { } ) { |img , attr | img [ attr ] ||= $~[ attr ] . strip if $~[ attr ] ; img }
24- text = @img [ 'alt' ]
40+ image = attributes . reduce ( { } ) { |img , attr | img [ attr ] ||= $~[ attr ] . strip if $~[ attr ] ; img }
41+ text = image [ 'alt' ]
2542
2643 # Allow parsing "title" "alt"
2744 if text =~ /(?:"|')(?<title>[^"']+)?(?:"|')\s +(?:"|')(?<alt>[^"']+)?(?:"|')/
28- @img [ 'title' ] = title
29- @img [ 'alt' ] = alt
45+ image [ 'title' ] = title
46+ image [ 'alt' ] = alt
3047 else
3148 # Set alt text and title from text
32- @img [ 'alt' ] . gsub! ( /"/ , '' ) if @img [ 'alt' ]
49+ image [ 'alt' ] . gsub! ( /"/ , '' ) if image [ 'alt' ]
3350 end
3451 end
3552
36- @img [ 'title' ] ||= @title
37- @img [ 'alt' ] ||= @title
38- super
53+ image [ 'title' ] ||= @title
54+ image [ 'alt' ] ||= @title
55+
56+ image
3957 end
4058
41- def render ( context )
42- if @img
43- "<img #{ @img . collect { |k , v | "#{ k } =\" #{ v } \" " if v } . join ( " " ) } >"
44- else
45- "Error processing input, expected syntax: {% img [class name(s)] [http[s]:/]/path/to/image [width [height]] [title text | \" title text\" [\" alt text\" ]] %}"
46- end
59+ def process_liquid ( context )
60+ Liquid ::Template . parse ( @markup ) . render! ( context . environments . first )
4761 end
4862 end
4963 end
0 commit comments