2011-07-23 04:23:50 +00:00
|
|
|
# Title: Simple Image tag for Jekyll
|
|
|
|
# Author: Brandon Mathis http://brandonmathis.com
|
|
|
|
# Description: Easily output images with optional class names and title/alt attributes
|
|
|
|
#
|
|
|
|
# Syntax {% image [class name(s)] url [title text] %}
|
|
|
|
#
|
|
|
|
# Example:
|
2011-07-23 21:40:17 +00:00
|
|
|
# {% ima left half http://site.com/images/ninja.png Ninja Attack! %}
|
2011-07-23 04:23:50 +00:00
|
|
|
#
|
|
|
|
# Output:
|
|
|
|
# <image class='left' src="http://site.com/images/ninja.png" title="Ninja Attack!" alt="Ninja Attack!">
|
|
|
|
#
|
|
|
|
|
|
|
|
module Jekyll
|
|
|
|
|
|
|
|
class ImageTag < Liquid::Tag
|
|
|
|
@img = nil
|
|
|
|
@title = nil
|
|
|
|
@class = ''
|
2011-07-29 22:29:39 +00:00
|
|
|
@width = ''
|
|
|
|
@height = ''
|
2011-07-23 04:23:50 +00:00
|
|
|
|
|
|
|
def initialize(tag_name, markup, tokens)
|
2011-07-29 22:29:39 +00:00
|
|
|
if markup =~ /(\S.*\s+)?(https?:\/\/|\/)(\S+)(\s+\d+\s+\d+)?(\s+.+)?/i
|
|
|
|
@class = $1 || ''
|
2011-07-23 04:23:50 +00:00
|
|
|
@img = $2 + $3
|
2011-07-29 22:29:39 +00:00
|
|
|
if $5
|
|
|
|
@title = $5.strip
|
|
|
|
end
|
|
|
|
if $4 =~ /\s*(\d+)\s+(\d+)/
|
|
|
|
@width = $1
|
|
|
|
@height = $2
|
|
|
|
end
|
2011-07-23 04:23:50 +00:00
|
|
|
end
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def render(context)
|
|
|
|
output = super
|
|
|
|
if @img
|
2011-07-29 22:29:39 +00:00
|
|
|
"<img class='#{@class}' src='#{@img}' width='#{@width}' height='#{@height}' alt='#{@title}' title='#{@title}'>"
|
2011-07-23 04:23:50 +00:00
|
|
|
else
|
2011-07-29 22:29:39 +00:00
|
|
|
"Error processing input, expected syntax: {% img [class name(s)] /url/to/image [width height] [title text] %}"
|
2011-07-23 04:23:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Liquid::Template.register_tag('img', Jekyll::ImageTag)
|