Skip to content

Instantly share code, notes, and snippets.

@mdekstrand
Created October 9, 2015 00:18
Show Gist options
  • Save mdekstrand/2f0bf2d97aeb7aba7835 to your computer and use it in GitHub Desktop.
Save mdekstrand/2f0bf2d97aeb7aba7835 to your computer and use it in GitHub Desktop.
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