-
-
Save tdegrunt/60f0b85a34c6c725fae696834563488e 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