on bbb2b2a help sign in
elk/compiler/Gen/ExtractNoscript.hs
59 lines 1.8 kB history raw
1module ExtractNoscript (extractNoscript) where
3import Ast
4import Located
5import qualified Text.Pandoc as Pan
7extractNoscript :: Module -> String
8extractNoscript modul = concat (extract modul)
10class Extract a where
11 extract :: a -> [String]
13instance Extract Module where
14 extract (Module _ _ _ stmts) =
15 map (\s -> "<p>" ++ s ++ "</p>") (concatMap extract stmts)
17instance Extract Statement where
18 extract (Definition d) = extract d
19 extract _ = []
21instance Extract Def where
22 extract (FnDef _ _ e) = extract e
23 extract (OpDef _ _ _ e) = extract e
24 extract _ = []
26instance Extract e => Extract (Located e) where
27 extract (L _ _ e) = extract e
29instance Extract Expr where
30 extract expr =
31 let f = extract in
32 case expr of
33 Str s -> [s]
34 Binop op e1 e2 -> case (op, f e1, f e2) of
35 ("++", [s1], [s2]) -> [s1 ++ s2]
36 (_ , ss1 , ss2 ) -> ss1 ++ ss2
37 Lambda v e -> f e
38 App (L _ _ (App (L _ _ (Var "link")) src)) txt -> linkExtract src txt
39 App (L _ _ (App (L _ _ (Var "Text.link")) src)) txt -> linkExtract src txt
40 App (L _ _ (Var "header")) e -> tag "h1" e
41 App (L _ _ (Var "bold")) e -> tag "b" e
42 App (L _ _ (Var "italic")) e -> tag "i" e
43 App (L _ _ (Var "monospace")) e -> tag "code" e
44 App e1 e2 -> f e1 ++ f e2
45 Let defs e -> concatMap extract defs ++ f e
46 Var _ -> []
47 Case e cases -> concatMap (f . snd) cases
48 Data _ es -> concatMap f es
49 MultiIf es -> concatMap (f . snd) es
50 Markdown doc -> [ Pan.writeHtmlString Pan.def doc ]
51 _ -> []
53linkExtract src txt =
54 case (extract src, extract txt) of
55 ([s1],[s2]) -> [ "<a href=\"" ++ s1 ++ "\">" ++ s2 ++ "</a>" ]
56 ( ss1, ss2) -> ss1 ++ ss2
59tag t e = map (\s -> concat [ "<", t, ">", s, "</", t, ">" ]) (extract e)