Skip to content

Instantly share code, notes, and snippets.

@DSIW
Forked from gehaxelt/Fancybox.rb
Created September 15, 2012 14:28
Show Gist options
  • Save DSIW/3728201 to your computer and use it in GitHub Desktop.
Save DSIW/3728201 to your computer and use it in GitHub Desktop.
Fancybox.rb
# encoding: utf-8
module Jekyll
class Fancybox < Liquid::Tag
BASE_URL = "http://gehaxelt.in"
def initialize(name, params, tokens)
super
@msg = "Params of 'fancybox' have to be [img_url] \"[title]\" \"[alt]\""
@img_url = scan_url(params).strip
@title = scan_quoted(params, 0).strip
@alt = scan_quoted(params, 1).strip
raise ArgumentError, @msg if @img_url && @img_url.empty?
end
def render(context)
link = anker(:href => @img_url) do
img({
:img_url => @img_url,
:alt => @alt,
:title => @title
})
end
%(#{link})
end
private
def scan_url(text)
text.scan(/^\s*(\S+?)\s+/)[0][0]
end
def scan_quoted(text, num)
text.scan(/(["'])(.*?)\1/)[num][1]
end
def url(uri)
"#{BASE_URL}/images/posts/small/#{uri}"
end
def img(options={})
img_url = options[:img_url]
alt = options[:alt]
title = options[:title]
"<img src=\"#{url(img_url)}\" alt=\"#{alt}\" title=\"#{title}\" />"
end
def anker(options={})
href = options[:href]
"<a href=\"#{url(href)}\" class=\"fancybox\" >#{yield}</a>"
end
end
end
Liquid::Template.register_tag('fancybox', Jekyll::Fancybox)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment