Skip to content

Instantly share code, notes, and snippets.

@ttscoff
Created January 24, 2013 14:55
Show Gist options
  • Save ttscoff/4622591 to your computer and use it in GitHub Desktop.
Save ttscoff/4622591 to your computer and use it in GitHub Desktop.
Jekyll tag for creating "download cards"
# Title: Download tag for Jekyll
# Author: Brett Terpstra http://brettterpstra.com
# Description: Create updateable download cards for downloadable projects. Reads from a downloads.csv file in the root folder
#
# Example: {% download id %} to read download [id] from the CSV
module Jekyll
class DownloadTag < Liquid::Tag
require 'csv'
@title = nil
@file = ''
@info = ''
@icon = nil
@template = 'link'
def initialize(tag_name, markup, tokens)
if markup =~ /^(\d+)( .*)?$/
req_id = $1.strip
@format = $2.strip.to_i unless $2.nil?
CSV.read("downloads.csv").each do |line|
if line[0] == req_id
id, title, file, version, description, info, icon, updated = line
@file = file
@version = version == '' ? '' : %Q{ v#{version}}
@title = %Q{#{title}#{@version}}
@icon = icon == '' ? '' : %Q{<img src="#{icon}" width="100" height="100">}
@updated = updated == '' ? '' : %Q{<p class="dl_updated">Updated #{updated.sub(/[\d:]+ -\d+ (\d+)/,"\\1").strip}.</p>}
@description = description == '' ? '' : %Q{<p class="dl_description">#{description}</p>#{@updated}}
@info = info == '' ? '' : %Q{<p class="dl_info"><a href="#{info}" title="More information on #{title}">More info&hellip;</a></p>}
break
end
end
end
super
end
# TODO: Make this read templates
# Example:
# Dir.chdir(includes_dir) do
# choices = Dir['**/*'].reject { |x| File.symlink?(x) }
# if choices.include?(file)
# source = File.read(file)
# partial = Liquid::Template.parse(source)
# context.stack do
# rtn = rtn + partial.render(context)
# end
# else
# rtn = rtn + "Included file '#{file}' not found in _includes directory"
# end
# end
def render(context)
output = super
# TODO: Enable template selection
if @title
download = %Q{<div class="download"><h4>#{@title}</h4><p class="dl_icon"><a href="#{@file}" title="Download #{@title}">#{@icon}</a></p><div class="dl_body"><p class="dl_link"><a href="#{@file}">Download #{@title}</a></p>#{@description}#{@info}</div></div>}
else
"Error processing input, expected syntax: {% download title filename [url/to/related/post] %}"
end
end
end
end
Liquid::Template.register_tag('download', Jekyll::DownloadTag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment