diff --git a/hakyll.cabal b/hakyll.cabal index 6c873113..a4ed88af 100644 --- a/hakyll.cabal +++ b/hakyll.cabal @@ -61,10 +61,13 @@ Extra-source-files: CHANGELOG.md tests/data/biblio/chicago.csl tests/data/biblio/cites-meijer.golden + tests/data/biblio/cites-multiple.golden + tests/data/biblio/cites-multiple.markdown tests/data/biblio/default.html tests/data/biblio/page.markdown tests/data/biblio/refs.bib tests/data/biblio/refs.yaml + tests/data/biblio/refs2.yaml tests/data/embed.html tests/data/example.md tests/data/example.md.metadata diff --git a/lib/Hakyll/Web/Pandoc/Biblio.hs b/lib/Hakyll/Web/Pandoc/Biblio.hs index e605bd1d..00b8b8c5 100644 --- a/lib/Hakyll/Web/Pandoc/Biblio.hs +++ b/lib/Hakyll/Web/Pandoc/Biblio.hs @@ -21,7 +21,9 @@ module Hakyll.Web.Pandoc.Biblio , Biblio (..) , biblioCompiler , readPandocBiblio + , readPandocBiblios , processPandocBiblio + , processPandocBiblios , pandocBiblioCompiler ) where @@ -46,7 +48,7 @@ import Text.Pandoc (Extension (..), Pandoc, enableExtension) import qualified Text.Pandoc as Pandoc import qualified Text.Pandoc.Citeproc as Pandoc (processCitations) -import System.FilePath (takeExtension) +import System.FilePath (addExtension, takeExtension) -------------------------------------------------------------------------------- @@ -88,9 +90,16 @@ readPandocBiblio :: ReaderOptions -> Item Biblio -> (Item String) -> Compiler (Item Pandoc) -readPandocBiblio ropt csl biblio item = do +readPandocBiblio ropt csl biblio = readPandocBiblios ropt csl [biblio] + +readPandocBiblios :: ReaderOptions + -> Item CSL + -> [Item Biblio] + -> (Item String) + -> Compiler (Item Pandoc) +readPandocBiblios ropt csl biblios item = do pandoc <- readPandocWith ropt item - processPandocBiblio csl biblio pandoc + processPandocBiblios csl biblios pandoc -------------------------------------------------------------------------------- @@ -98,7 +107,13 @@ processPandocBiblio :: Item CSL -> Item Biblio -> (Item Pandoc) -> Compiler (Item Pandoc) -processPandocBiblio csl biblio item = do +processPandocBiblio csl biblio = processPandocBiblios csl [biblio] + +processPandocBiblios :: Item CSL + -> [Item Biblio] + -> (Item Pandoc) + -> Compiler (Item Pandoc) +processPandocBiblios csl biblios item = do -- It's not straightforward to use the Pandoc API as of 2.11 to deal with -- citations, since it doesn't export many things in 'Text.Pandoc.Citeproc'. -- The 'citeproc' package is also hard to use. @@ -110,19 +125,25 @@ processPandocBiblio csl biblio item = do -- ersatz filesystem. let Pandoc.Pandoc (Pandoc.Meta meta) blocks = itemBody item cslFile = Pandoc.FileInfo zeroTime . unCSL $ itemBody csl - bibFile = Pandoc.FileInfo zeroTime . unBiblio $ itemBody biblio - bibFileType = takeExtension $ toFilePath $ itemIdentifier biblio - internalBibFileName = "_hakyll/refs." ++ bibFileType - - addBiblioFiles = \st -> st - { Pandoc.stFiles = - Pandoc.insertInFileTree "_hakyll/style.csl" cslFile . - Pandoc.insertInFileTree internalBibFileName bibFile $ - Pandoc.stFiles st - } + bibFiles = zipWith (\x y -> + ( addExtension ("_hakyll/bibliography-" ++ show x) + (takeExtension $ toFilePath $ itemIdentifier y) + , Pandoc.FileInfo zeroTime . unBiblio . itemBody $ y + ) + ) + [0 ..] + biblios + + stFiles = foldr ((.) . uncurry Pandoc.insertInFileTree) + (Pandoc.insertInFileTree "_hakyll/style.csl" cslFile) + bibFiles + + addBiblioFiles = \st -> st { Pandoc.stFiles = stFiles $ Pandoc.stFiles st } + biblioMeta = Pandoc.Meta . Map.insert "csl" (Pandoc.MetaString "_hakyll/style.csl") . - Map.insert "bibliography" (Pandoc.MetaString $ T.pack internalBibFileName) $ + Map.insert "bibliography" + (Pandoc.MetaList $ map (Pandoc.MetaString . T.pack . fst) bibFiles) $ meta errOrPandoc = Pandoc.runPure $ do Pandoc.modifyPureState addBiblioFiles diff --git a/tests/Hakyll/Web/Pandoc/Biblio/Tests.hs b/tests/Hakyll/Web/Pandoc/Biblio/Tests.hs index 317d4d72..ab78d5b3 100644 --- a/tests/Hakyll/Web/Pandoc/Biblio/Tests.hs +++ b/tests/Hakyll/Web/Pandoc/Biblio/Tests.hs @@ -26,6 +26,7 @@ tests :: TestTree tests = testGroup "Hakyll.Web.Pandoc.Biblio.Tests" $ [ goldenTest01 , goldenTest02 + , goldenTest03 ] -------------------------------------------------------------------------------- @@ -98,3 +99,38 @@ goldenTest02 = cleanTestEnv return output) + +goldenTest03 :: TestTree +goldenTest03 = + goldenVsString + "biblio03" + (goldenTestsDataDir > "cites-multiple.golden") + (do + -- Code lifted from https://github.com/jaspervdj/hakyll-citeproc-example. + logger <- Logger.new Logger.Error + let config = testConfiguration { providerDirectory = goldenTestsDataDir } + _ <- run RunModeNormal config logger $ do + let myPandocBiblioCompiler = do + csl <- load "chicago.csl" + bib1 <- load "refs.bib" + bib2 <- load "refs2.yaml" + getResourceBody >>= + readPandocBiblios defaultHakyllReaderOptions csl [bib1, bib2] >>= + return . writePandoc + + match "default.html" $ compile templateCompiler + match "chicago.csl" $ compile cslCompiler + match "refs.bib" $ compile biblioCompiler + match "refs2.yaml" $ compile biblioCompiler + match "cites-multiple.markdown" $ do + route $ setExtension "html" + compile $ + myPandocBiblioCompiler >>= + loadAndApplyTemplate "default.html" defaultContext + + output <- fmap LBS.fromStrict $ B.readFile $ + destinationDirectory testConfiguration > "cites-multiple.html" + + cleanTestEnv + + return output) diff --git a/tests/data/biblio/cites-multiple.golden b/tests/data/biblio/cites-multiple.golden new file mode 100644 index 00000000..0cadbe9f --- /dev/null +++ b/tests/data/biblio/cites-multiple.golden @@ -0,0 +1,20 @@ + + +
+ +I would like to cite one of my favourite papers (Meijer, Fokkinga, and Paterson 1991) here.
+And also a book (Lipovača 2012).
+