Search code examples
haskellpandoccitations

How can I process citations using Pandoc's Citeproc, in Haskell?


Starting with "A Simple Example" from the Pandoc documentation, I want to add citation processing functionality. The docs for Text.Pandoc.Citeproc show a function processCitations which supposedly processes citations. Yet given simple org-mode input, and a citation [@test2022], it doesn't seem to work. It compiles and runs just fine, but the output of the code below is: <p><span class="spurious-link" target="url"><em>testing</em></span> [@test2022]</p>, i.e., the citation isn't actually processed. What am I doing wrong? And how can I get this to process my citation?

import Text.Pandoc
import qualified Data.Text as T
import qualified Data.Text.IO as TIO
import Text.Pandoc.Filter
import Text.Pandoc.Citeproc

main :: IO ()
main = do
  result <- runIO $ do
    doc <- readOrg def (T.pack "#+bibliography: test.bib\n [[url][testing]]\n[@test2022]")
    processed <- processCitations doc
    writeHtml5String def processed
  html <- handleError result
  TIO.putStrLn html

For reference, here's my test.bib bibtex file:

@Book{test2022,
  author =    {Barus, Foobius},
  title =        {The Very Persistent Foo or Bar},
  publisher =    {Foobar Publications, Inc},
  year =         {2022}
}

Solution

  • I figured this out myself, eventually. Turns out you have to set some extensions, and some options, and set the metadata for the document:

    {-# LANGUAGE OverloadedStrings #-}
    
    import Text.Pandoc
    import qualified Data.Text as T
    import qualified Data.Text.IO as TIO
    import Text.Pandoc.Filter
    import Text.Pandoc.Citeproc
    import qualified Data.Map as M
    import Text.Pandoc.Builder (setMeta)
    
    main :: IO ()
    main = do
      let exts = extensionsFromList [ Ext_citations ]
      let readerOptions = def{ readerExtensions = exts}
      let writerOptions = def{ writerExtensions = exts}
      result <- runIO $ do
        doc <- readMarkdown readerOptions (T.pack "Testing testing\n[@test2022]\n")
        let doc' = setMeta (T.pack "bibliography") (T.pack "test.bib") doc :: Pandoc
        processed <- processCitations doc'
        writeHtml5String writerOptions processed
      html <- handleError result
      TIO.putStrLn html