-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranslator.idr
More file actions
63 lines (55 loc) · 2.17 KB
/
Copy pathTranslator.idr
File metadata and controls
63 lines (55 loc) · 2.17 KB
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
module A2ML.Translator
import A2ML.TypedCore
import A2ML.Surface
-- Minimal translation: headings become sections with generated IDs,
-- paragraphs and lists map directly. Directives map based on name.
export
translate : SDoc -> Doc
translate (MkSDoc bs) = MkDoc (map toBlock bs)
where
mkId : String -> Id
mkId t = MkId t
inlineToText : List Inline -> String
inlineToText = concatMap toString
where
toString : Inline -> String
toString (SText s) = s
toString (SEmph s) = s
toString (SStrong s) = s
toString (SLink label _) = label
listToText : List (List Inline) -> List String
listToText xs = map inlineToText xs
attrLookup : String -> List (String, String) -> Maybe String
attrLookup _ [] = Nothing
attrLookup k ((k2, v) :: rest) = if k == k2 then Just v else attrLookup k rest
collectParagraphs : List SBlock -> List String
collectParagraphs [] = []
collectParagraphs (SParagraph xs :: rest) = inlineToText xs :: collectParagraphs rest
collectParagraphs (_ :: rest) = collectParagraphs rest
toBlock : SBlock -> Block
toBlock (SHeading _ t) = Section (MkSec (MkId ("sec:" ++ t)) t [])
toBlock (SParagraph t) = Para (inlineToText t)
toBlock (SList xs) = Bullet (listToText xs)
toBlock (SDirective name attrs body) =
case name of
"fig" =>
let mid = attrLookup "id" attrs
mref = attrLookup "ref" attrs
caption = case collectParagraphs body of
[] => ""
(c :: _) => c
in Figure (MkFig (mkId (maybe "fig:unnamed" id mid)) caption (map mkId mref))
"table" =>
let mid = attrLookup "id" attrs
caption = case collectParagraphs body of
[] => ""
(c :: _) => c
in Table (MkTbl (mkId (maybe "tbl:unnamed" id mid)) caption)
"refs" =>
let labels = collectParagraphs body
refs = map MkRef labels
in Refs refs
"abstract" =>
Section (MkSec (mkId "sec:abstract") "Abstract" (map toBlock body))
_ =>
Section (MkSec (mkId ("sec:" ++ name)) name (map toBlock body))