Created
January 2, 2012 01:03
-
-
Save pichfl/1548864 to your computer and use it in GitHub Desktop.
Liquid Image Tag for Jekyll
This file contains 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
# A simple plugin for Jekyll that allows you to use {% url "alt text" %} to add images to your posts. | |
# It will automatically check those images for width and height. | |
# | |
# Requires http://imagesize.rubyforge.org/ | |
require 'image_size' | |
require 'open-uri' | |
module Jekyll | |
class ImageTag < Liquid::Tag | |
@img = nil | |
@alt = "" | |
def initialize(tag_name, markup, tokens) | |
@img = markup.strip | |
if temp = @img.match(/^(.*)(\s)(\")(.*)(\")$/) | |
@img = temp[1] | |
@alt = temp[4] | |
end | |
super | |
end | |
def render(context) | |
w,h = open(@img, "rb") do |fh| | |
ImageSize.new(fh.read).get_size | |
end | |
if @img | |
"<img src=\"#{@img}\" width=\"#{w}\" height=\"#{h}\" alt=\"#{@alt}\" >" | |
else | |
end | |
end | |
end | |
end | |
Liquid::Template.register_tag('img', Jekyll::ImageTag) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, when using this, I've had success if you invoke the tag
for the updated header, see https://gist.github.com/alvarogarcia7/9aebc6ca3db08a26d6378e5bc4611a96