Skip to content

Instantly share code, notes, and snippets.

@ironbishop
Forked from alexbevi/galleria_tag.rb
Last active October 7, 2016 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ironbishop/4a3bd7df886ee1c98db4864bbad8f649 to your computer and use it in GitHub Desktop.
Save ironbishop/4a3bd7df886ee1c98db4864bbad8f649 to your computer and use it in GitHub Desktop.
Galleria Tag for Jekyll/Octopress
# Title: Galleria Tag for Jekyll
# Authors: Alex Bevilacqua
# Description: incorporate the Galleria gallery jquery plugin
#
# Adaption of "Photos tag for Jekyll" by Devin Weaver, and the derived
# "Gallery Tag for Jekyll" by Kevin Brown.
#
# Multiple gallery in same page, added by @ironbishop .
#
# Installation:
#
# {% galleria_includes %}
#
# This macro should be added to your `source/_includes/custom/head.html`
# in order to load the libraries needed by Galleria
#
# Example:
#
# {% galleria [id] %}
# photo1.jpg
# /path/to/photos/photo2.jpg:title
# {% endgalleria %}
#
module Jekyll
PLUGIN_GALLERIA_VERSION = "1.4.7"
PLUGIN_JQUERY_VERSION = "2.1.1"
class PhotosUtil
def initialize(context)
@context = context
end
def path_for(filename)
filename = filename.strip
prefix = (@context.environments.first['site']['photos_prefix'] unless filename =~ /^(?:\/|http)/i) || ""
"#{prefix}#{filename}"
end
end
class GalleriaScriptIncludePatch < Liquid::Tag
def render(context)
return <<-eof
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/#{PLUGIN_JQUERY_VERSION}/jquery.min.js" type="text/javascript"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/galleria/#{PLUGIN_GALLERIA_VERSION}/galleria.min.js" type="text/javascript"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/galleria/#{PLUGIN_GALLERIA_VERSION}/themes/classic/galleria.classic.min.js" type="text/javascript"></script>
<link href="//cdnjs.cloudflare.com/ajax/libs/galleria/#{PLUGIN_GALLERIA_VERSION}/themes/classic/galleria.classic.min.css" media="screen, projection" rel="stylesheet" type="text/css" />
<style>
/* This rule is read by Galleria to define the gallery height: */
#galleria{height:320px}
</style>
eof
end
end
class GalleriaTag < Liquid::Block
@galleria_id = nil
def initialize(tag_name, markup, tokens)
@galleria_id = markup.strip
super
end
def render(context)
# Convert the entire content array into one large string
lines = super
# split the text by newlines
lines = lines.split("\n")
p = PhotosUtil.new(context)
gallery = "<div id=\"galleria#{@galleria_id}\">"
#asd
lines.each_with_index do |line, i|
next if line.empty?
filename, title = line.split(":")
title = (title.nil?) ? "" : title.strip
gallery << "<img src=\"#{p.path_for(filename)}\" data-title=\"#{title}\" />"
end
gallery << "</div>"
gallery << "<script>"
gallery << " Galleria.configure('transition', 'fade');"
gallery << " Galleria.run('#galleria#{@galleria_id}');"
gallery << "</script>"
gallery
end
end
end
Liquid::Template.register_tag('galleria', Jekyll::GalleriaTag)
Liquid::Template.register_tag('galleria_includes', Jekyll::GalleriaScriptIncludePatch)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment