{-# LANGUAGE OverloadedRecordDot #-} -- | -- Module : Zettel.Bibliography -- Description : Render bibliography citations to links to org bibliography -- Copyright : (c) 2023, Yann Herklotz -- License : GPL-3 -- Maintainer : git [at] yannherklotz [dot] com -- Stability : experimental -- Portability : POSIX -- -- By default, if no Org bibliography file is provided, the citations are -- handled during rendering. However, it is useful to link to any bibliographic -- notes that one might already have instead of just citing the works in the -- 'Zettel'. -- -- 'handleBibliography' can therefore be used to replace all citations in all -- 'Zettel' to links instead. These will then link to the rendered -- bibliographic notes about the cited work, which can then include the full -- citation as well. module Zettel.Bibliography ( handleBibliography, replaceCitation, replaceCite, replaceCites, ) where import Text.Pandoc.Definition (Citation (..), Inline (..), Target) import Text.Pandoc.Walk (walk) import Zettel.Types -- | Replace the Citation by the link with a description and a target. replaceCitation :: Citation -> ([Inline], Target) replaceCitation c = ([Str $ "@" <> zid], ("/bib/" <> zid, "")) where zid = citationId c -- | Replace a citation by a link to the Org bibliography file. replaceCite :: Inline -> [Inline] replaceCite (Cite c _) = [Str "("] <> map (uncurry (Link mempty) . replaceCitation) c <> [Str ")"] replaceCite c = [c] -- | Replaces all citations by a link to the Org bibliography file. replaceCites :: [Inline] -> [Inline] replaceCites = concatMap replaceCite -- | If the bibliography is non-empty (i.e. 'zettelGraphBib' is non-empty), then -- it replaces all citations in 'Zettel' in 'unZettelGraph' by links to -- @\/bib\/citationId@. handleBibliography :: ZettelGraph -> ZettelGraph handleBibliography zg = if null zg.zettelGraphBib then zg else zg {unZettelGraph = walk replaceCites zg.unZettelGraph}