Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
60 lines (50 sloc) 2.33 KB
title author tags
Citations in Hakyll using Bibtex
Matthias C. M. Troffaes

Trying to move my homepage to Hakyll, to my pleasant surprise I found out that pandoc, which drives Hakyll, supports citations. The documentation on how to use this feature from within Hakyll is rather sparse. After some searching, I came across the source of Julien Tanguy's website, and in particular, his site script. In a nutshell, you need to do the following.

First, download a csl file which encodes the referencing style. Zotero has an excellent index. I chose Elsevier (numeric, with titles, sorted alphabetically).

Then, you need to tell Hakyll to do a number of things. It needs to compile your csl and bibtex file(s). If you put all your bibtex files in the bib/ folder, and all your csl files in the csl/ folder, then you need, in your main = hakyll $ do{.haskell} block:

match "bib/*" $ compile biblioCompiler
match "csl/*" $ compile cslCompiler

Next, write a function that loads the compiled csl and bib files, and runs the pandoc compiler using those files:

bibtexCompiler :: String -> String -> Compiler (Item String)
bibtexCompiler cslFileName bibFileName = do
    csl <- load $ fromFilePath cslFileName
    bib <- load $ fromFilePath bibFileName
    liftM writePandoc
        (getResourceBody >>= readPandocBiblio def csl bib)

This is the least trivial bit. It would be nice if Hakyll provided this function by default; in good open source spirit, I submitted a patch.

Finally, compile any document that uses a bibliography with bibtexCompiler{.haskell} instead of pandocCompiler{.haskell}, and specify the relevant bib and csl files. For example, in your main = hakyll $ do{.haskell} block:

match "" $ do
    route   $ setExtension "html"
    compile $ bibtexCompiler
              "csl/elsevier-with-titles-alphabetical.csl" "bib/research.bib"
        >>= loadAndApplyTemplate "templates/default.html" defaultContext
        >>= relativizeUrls

That's it!