Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
publication Liquid tag
# Copyright 2015 Michael D. Ekstrand.
# Licensed under the MIT License http://md.ekstrandom.net/copyright
require 'nokogiri'
module Jekyll
class PubCiteTag < Liquid::Tag
def initialize(tag_name, text, tokens)
super
@key = text.strip
@kpath = "research/pubs/#{@key}/index.md"
if tag_name == 'publication'
@wrap = false
elsif tag_name == 'pubcite'
@wrap = true
end
end
def render(context)
site = context.registers[:site]
page = site.pages.find { |pg|
pg.path == @kpath
}
if page.nil?
nil
else
parsed = Nokogiri::HTML::DocumentFragment.parse page.transform
cite = parsed.at_css('.citation')
if cite.nil?
cite = parsed.at_css('p')
end
if not cite.nil?
link = cite.at_css('a[href="#"]')
if not link.nil?
url = page.url
if @wrap
url = site.config['url'] + url
end
link['href'] = url
end
if @wrap
cite.to_html
else
cite.children.to_html
end
else
print parsed
"<strong>Bad citation #{@key}</strong>"
end
end
end
end
end
Liquid::Template.register_tag('publication', Jekyll::PubCiteTag)
Liquid::Template.register_tag('pubcite', Jekyll::PubCiteTag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment