class Octopress::Tags::ImageTag::Tag
Public Class Methods
new(tag_name, markup, tokens)
click to toggle source
Calls superclass method
# File lib/octopress-image-tag.rb, line 14 def initialize(tag_name, markup, tokens) @markup = markup super end
Public Instance Methods
image(context)
click to toggle source
# File lib/octopress-image-tag.rb, line 31 def image(context) @markup = process_liquid(context) title = /title:['|"](.+?)['|"]/ @title = @markup.scan(title).flatten.compact.last @markup.gsub!(title, '') if @markup =~ /(?<class>\S.*\s+)?(?<src>(?:https?:\/\/|\/|\S+\/)\S+)(?:\s+(?<width>\d\S+))?(?:\s+(?<height>\d\S+))?(?<alt>\s+.+)?/i attributes = ['class', 'src', 'width', 'height', 'alt'] image = attributes.reduce({}) { |img, attr| img[attr] ||= $~[attr].strip if $~[attr]; img } text = image['alt'] # Allow parsing "title" "alt" if text =~ /(?:"|')(?<title>[^"']+)?(?:"|')\s+(?:"|')(?<alt>[^"']+)?(?:"|')/ image['title'] = title image['alt'] = alt else # Set alt text and title from text image['alt'].gsub!(/"/, '') if image['alt'] end end image['title'] ||= @title image['alt'] ||= @title image end
process_liquid(context)
click to toggle source
# File lib/octopress-image-tag.rb, line 59 def process_liquid(context) Liquid::Template.parse(@markup).render!(context.environments.first) end
render(context)
click to toggle source
# File lib/octopress-image-tag.rb, line 19 def render(context) begin attributes = image(context).collect do |k,v| "#{k}=\"#{v}\"" if v end.join(" ") "<img #{attributes}>" rescue raise "Error processing input, expected syntax: {% img [class name(s)] [http[s]:/]/path/to/image [width [height]] [title text | \"title text\" [\"alt text\"]] %}" end end