dp.cx blog

Posted on

Filed under jekyll, ruby, and references

Recently, I wanted to make Jekyll create a references list ala Wikipedia; so I built one. It was actually easier than I thought it would be, though I'm still not 100% happy with the syntax.

To enable references, you'll need to add some tags to your Jekyll _plugins folder:

 
module Jekyll
 
    @@reflist = Array.new
 
    def self.push(x)
        @@reflist.push(x)
    end
 
    def self.get
        @@reflist
    end
 
    class Ref < Liquid::Block
        def render(context)
            content = super
            Jekyll.push(content)
            content = "<sup>#{Jekyll.get.length}</sup>"
        end
    end
 
    class RefList < Liquid::Block
        def render(context)
            content = super
            if Jekyll.get.length > 0
                str = "<hr /><ol><li>#{Jekyll.get.join("</li><li>")}</li></ol>"
            end
            content = str
        end
    end
end
 
Liquid::Template.register_tag('ref', Jekyll::Ref);
Liquid::Template.register_tag('reflist', Jekyll::RefList);
 

This will allow you to use [% ref %] and [% endref %] to surround the reference text, and then [% reflist %][% endreflist %] to display your reference list. Unfortunately, it doesn't seem that you can put the reflist tag in your actual templates, and so must go in your post.

Please note that the above "tags" use [% and %] instead of the Liquid Template default characters, as they're parsed out no matter where used. Change them if you want this to work on your install.