Module: UltraMarkdown::Filter::ImageTag

Included in:
SyntaxConverter
Defined in:
lib/ultra_markdown/filter/image_tag.rb

Instance Method Summary collapse

Instance Method Details

#liquid_image_tag(input) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ultra_markdown/filter/image_tag.rb', line 5

def liquid_image_tag(input)
  attributes = ['class', 'src', 'width', 'height', 'title']
  img = nil
  
  
  input.gsub!(/^\{\% *img ([^\n\}]+)\%\}/m) do
    markup = $1
  
    if markup =~ /(?<class>\S.*\s+)?(?<src>(?:https?:\/\/|\/|\S+\/)\S+)(?:\s+(?<width>\d+))?(?:\s+(?<height>\d+))?(?<title>\s+.+)?/i
      img = attributes.reduce({}) { |img_temp, attr| img_temp[attr] = $~[attr].strip if $~[attr]; img_temp }
      if /(?:"|')(?<title>[^"']+)?(?:"|')\s+(?:"|')(?<alt>[^"']+)?(?:"|')/ =~ img['title']
        img['title'] = title
        img['alt'] = alt
      else
        if img['title']
          img['title'].gsub!(/"/, '&#34;')
          img['alt'] = img['title']
        end
      end
      img['class'].gsub!(/"/, '') if img['class']
    end
  
    "<img #{img.collect {|k,v| "#{k}=\"#{v}\"" if v}.join(" ")}>"
  end
end