summaryrefslogtreecommitdiffstats
path: root/src/Zettel/Bibliography.hs
blob: 42476be57516dbcded1d74fed64befd275d0c635 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
{-# 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}