-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpandoc-ednotes.hs
77 lines (68 loc) · 2.35 KB
/
pandoc-ednotes.hs
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
--------------------------------------------------------------------------------
{-# LANGUAGE OverloadedStrings, FlexibleContexts #-}
--------------------------------------------------------------------------------
import Text.Pandoc.JSON
import Text.Pandoc.Generic
import Text.Pandoc.Walk
import Data.List
( isInfixOf )
import Data.List.Split
( splitOn )
import Utils
--------------------------------------------------------------------------------
-- This filter takes divs with the class "edition" and inserts the LaTeX
-- commands for an ednotes environment. It then converts all footnotes with
-- the relevant initial letters into ednotes.sty editorial comments.
rmKeys :: Walkable Inline a => a -> a
rmKeys = rmStr "|" . rmStr "&"
main :: IO ()
main = toJSONFilter ednotes
ednotes :: Maybe Format -> Pandoc -> Pandoc
ednotes (Just "latex")
= unionMetaInline "header-includes"
(RawInline "tex" "\\usepackage[Apara, Bpara]{ednotes}")
. topDown (concatMap processDivs)
ednotes _ = bottomUp (concatMap $ mvNotes "|")
. bottomUp (concatMap $ mvNotes "&")
processDivs :: Block -> [Block]
processDivs (Div (_, cs, _) contents)
| ["edition"] `isInfixOf` cs
= concat
[ [beginText]
, addNotes contents
, [endText]
]
where
beginText
= latexBlock
$ unlines
[ "\\begin{verse}"
, "\\begin{linenumbers}"
, "\\modulolinenumbers[2]"
, "\\firstlinenumber{1}"
]
endText
= latexBlock
$ unlines
[ "\\end{linenumbers}"
, "\\end{verse}"
]
addNotes
= overloadNote "|" (latexInline "\\Anote{") (latexInline "}") makeSplit
. overloadNote "&" (latexInline "\\Bnote{") (latexInline "}") makeSplit
processDivs x = [rmKeys x]
makeSplit :: [Inline] -> [Inline]
makeSplit
= concat
. uncurry ((++) . (++ [[latexInline "}{"]]))
. splitAt 1
. splitOn [Space, Str ":", Space]
mvNotes :: String -> Inline -> [Inline]
mvNotes char n@(Note (Para (c:_) : _))
| c == Str char = mvNote $ rmStr char n
where
mvNote (Note (Para cs : ps))
= (\ (w : w') -> w ++ [Note $ Para (concat w') : ps])
$ splitOn [Space, Str ":", Space] cs
mvNotes _ x = [x]
--------------------------------------------------------------------------------