How to link citations with Hakyll?

2022-01-30 CC-BY

I am using the newest Hakyll version 4.15. This version depends on the newest citeproc package. Previously, pandoc-citeproc was used to handle citations and style.

I need to replace pandocCompilerWith with my own bibtexCompilerWith that will link the citations. bibtexCompilerWith could look something like this:

bibtexCompilerWith :: ReaderOptions
                   -> WriterOptions
                   -> FilePath
                   -> Identifier
                   -> Compiler (Item String)
bibtexCompilerWith readerOpts writerOpts bibPath cslPath = do
  csl <- load cslPath
  bib <- load (fromFilePath bibPath)

  getResourceBody
    >>= turnOnLinkCitations readerOpts  -- insert Pandoc link-citations metadata
    >>= processPandocBiblio csl bib     -- existing Hakyll function that inserts more metadata
    >>= return . writePandocWith writerOpts

Turning on link-citations is straightforward:

turnOnLinkCitations :: ReaderOptions
                    -> Item String
                    -> Compiler (Item Pandoc)
turnOnLinkCitations ropt item = do
  pandoc <- readPandocWith ropt item
  withItemBody (return . addLinkCitations) pandoc

addLinkCitations :: Pandoc -> Pandoc
addLinkCitations (Pandoc meta a) =
  meta & unMeta
       & M.insert "reference-section-title" (MetaString "Bibliography")
       & M.insert "link-citations" (MetaBool True)
       & \m -> Pandoc (Meta m) a

Hakyll provides the processPandocBiblio function and uses the given bibtex file to edit the meta map (that is also edited here for link-citations). If I would prefer to not use bibtex but list all of the citations in YAML frontmatter of the article, then I would edit the pandoc meta directly.

For this post, I create a bib/citations.bib file that includes a reference, then I configure it in the YAML and cite an entry in the text like this [@colbourn2006handbook], or like this [@colbourn2006handbook, pp.33-35] and leave it at the end of the paragraph. [2]

The bibliography below will include it. The citation style used is numeric but I can use any style by providing a custom .csl file that can be fetched from Zotero Styles Repository.

Note: To include references in the bibliography that are not cited in the article, I added a single hidden div element below that lists them.

<div style="display: none;">

[@furedi1996lottery; @burger2007towards]

[@etzion1995bounds]

[@vazirani2013approximation, chapter 1, page 33]

</div>

[1,4]

[3]

[5, page 33]

Another note: It is a bit weird to use footnotes at the same time.

Tangential note: I am linking citations but there is still no real article linking them. Such is life.

Bibliography

[1]
A.P. Burger, W. Grundlingh, J.H. van Vuuren, Towards a characterisation of lottery set overlapping structures, Ars Combinatoria. 84 (2007) 105–128.
[2]
C.J. Colbourn, J.H. Dinitz, Handbook of combinatorial designs, CRC press, 2006.
[3]
T. Etzion, V. Wei, Z. Zhang, Bounds on the sizes of constant weight covering codes, Designs, Codes and Cryptography. 5 (1995) 217–239.
[4]
Z. Füredi, G.J. Székely, Z. Zubor, On the lottery problem, Journal of Combinatorial Designs. 4 (1996) 5–10.
[5]
V.V. Vazirani, Approximation algorithms, Springer Science & Business Media, 2013.