Created
July 10, 2011 20:52
-
-
Save destroytoday/1074958 to your computer and use it in GitHub Desktop.
Tag for auto-setting image sizes in Jekyll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Image | |
class ImageTag < Liquid::Tag | |
require 'RMagick' | |
SYNTAX = /(?:'|")([^ ]+)(?:'|")\s?(?:(?:'|")(.+)(?:'|"))*/ | |
def initialize(tag_name, markup, tokens) | |
super | |
if markup =~ SYNTAX | |
@url = $1 | |
@alt = $2 if defined? $2 | |
else | |
raise SyntaxError.new("Syntax Error in 'image' - Valid syntax: image <url> [alt]") | |
end | |
end | |
def render(context) | |
image = Magick::Image.read("#{context.registers[:site].dest}/#{@url}").first | |
@alt = " alt=\"#{@alt}\"" unless @alt.nil? | |
"<img src=\"#{@url}\" width=\"#{image.columns}\" height=\"#{image.rows}\"#{@alt} />" | |
end | |
end | |
end | |
Liquid::Template.register_tag('image', Image::ImageTag) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usage: {% image @url [@alt] %} | |
{% image '/images/blog/dayone/dayone_header.jpg' %} | |
# => <img src="/images/blog/dayone/dayone_header.jpg" width="645" height="400" /> | |
{% image '/images/blog/dayone/dayone_header.jpg' 'Day One' %} | |
# => <img src="/images/blog/dayone/dayone_header.jpg" width="645" height="400" alt="Day One" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment