Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
model_tag Octopress plugin
# Title: Include Model Tag for Jekyll
# Author: Max Eskin http://maxeskin.com
# Description: Import a 3D model saved in JSON format for display in a fancybox iframe.
#
# Syntax {% model key=value ... %}
#
# Keys:
# model - the filename of the model JSON
# thumb - the thumbnail image to display on the link
# title - the title to display under the model and in the title of the image
# width - the width of the thumbnail
# height - the height of the thumbnail
#
# Only the model, thumb, and title are required.
require 'digest/md5'
module Jekyll
class ModelTag < Liquid::Tag
def initialize(tag_name, markup, tokens)
h = Hash[markup.scan(/(?:"(?:\\.|[^"])*"|[^" ])+/).map { |l| l.chomp.split('=', 2) }]
@filename = h.fetch('model', '')
@thumb = h.fetch('thumb', '')
@title = h.fetch('title', '').gsub('"', '')
@width = h.fetch('width', nil)
@height = h.fetch('height', nil)
super
end
def render(context)
p = PhotosUtil.new(context)
if @filename
clean_title = @title.gsub(/[^a-zA-Z0-9]/, '')
style = ""
if @width
style += "width= " + @width + " "
end
if @height
style += "height= " + @height
end
"<a id=\"#{clean_title}\" data-fancybox-type=\"iframe\" href=\"/js/modelFrame.html\" class=\"fancybox\" title=\"#{@title}\" onclick=\"#{clean_title}OnClick()\">\
<img src=\"#{p.thumb_for(@filename,@thumb)}\" alt=\"#{@title}\" #{style} />\
</a>\n<script>function #{clean_title}OnClick() {\
$(\"##{clean_title}\").fancybox({ width:580, height:600, tpl: {\
iframe: \"<iframe id='modelFrame' name='modelFrame' class='fancybox-iframe' frameborder='0' vspace='0' hspace='0' onload='document.getElementById(\\\"modelFrame\\\").contentWindow.init(\\\"#{p.path_for(@filename)}\\\");'/>\"\
}});} </script>"
else
"Error processing input, expected syntax: {% model filename [thumbnail] [title] %}"
end
end
end
end
Liquid::Template.register_tag('model', Jekyll::ModelTag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment