baccarat/_plugins/lightbox.rb

38 lines
1.3 KiB
Ruby

# Lightbox tag plugin for Jekyll
# Usage: {% lightbox [position] [filename] [alt/title] %}
# Usage sample: {% lightbox left sample.jpg "This is the sample" %}
require "mini_magick"
module Jekyll
class LightBox < Liquid::Tag
def initialize(tag_name, markup, tokens)
@image = nil
@error = nil
results = markup.scan(/^(?<position>left|right|center)\s(?<filepath>.+)\.(?<extension>jpg|jpeg|jpe|gif|png|bmp)\s["]?(?<description>.+)["]?$/i)
unless results then
@error = "Image lightbox: Error processing input."
return
end
return
@image['file'] = results[2] + '.' + results[3]
@image['thumbnail'] = results[2] + '_thumb.' + results[3]
@image['position'] = results[1]
@image['description'] = results[4]
unless File.exist?(@image['file']) then @error = "Image lightbox: File not found." end
super
end
def render(context)
if @error then return @error end
return %Q!<a href="#{@image['file']}" class='lightbox'>
<img class="image-#{@image['position']}" title="#{@image['description']}" alt="#{@image['description']}" src="#{@image['thumbnail']}">
</a>!
end
end
end
Liquid::Template.register_tag('lightbox', Jekyll::LightBox)