# Title: Simple Image Figure tag for Jekyll # Author: Brandon Mathis http://brandonmathis.com # Description: Easily output images in
with an optional
and class names. # # Syntax {% figure [class name(s)] url [caption text] %} # # Example: # {% figure left half http://site.com/images/ninja.png Ninja Attack! %} # # Output: #
Ninja Attack!
# # Example 2 (image with caption) # {% figure /images/ninja.png Ninja Attack! %} # # Output: #
Ninja Attack!
# # Example 3 (just an image with classes) # {% figure right /images/ninja.png %} # # Output: #
# module Jekyll class FigureImageTag < Liquid::Tag ClassImgCaption = /(\S[\S\s]*)\s+(https?:\/\/|\/)(\S+)\s+(.+)/i ClassImg = /(\S[\S\s]*)\s+(https?:\/\/|\/)(\S+)/i ImgCaption = /^\s*(https?:\/\/|\/)(\S+)\s+(.+)/i Img = /^\s*(https?:\/\/|\/)(\S+\s)/i @img = nil @caption = nil @class = '' def initialize(tag_name, markup, tokens) if markup =~ ClassImgCaption @class = $1 @img = $2 + $3 @caption = $4 elsif markup =~ ClassImg @class = $1 @img = $2 + $3 elsif markup =~ ImgCaption @img = $1 + $2 @caption = $3 elsif markup =~ Img @img = $1 + $2 end super end def render(context) output = super if @img figure = "
" figure += "" figure += "
#{@caption}
" if @caption figure += "
" else "Error processing input, expected syntax: {% figure [class name(s)] /url/to/image [caption] %}" end end end end Liquid::Template.register_tag('figure', Jekyll::FigureImageTag)