on dcbe2fc help sign in
elk/compiler/Generate/Noscript.hs
65 lines 2.1 kB history raw
1module Generate.Noscript (noscript) where
3import Data.List (isInfixOf)
4import SourceSyntax.Everything
5import qualified Text.Pandoc as Pan
7noscript :: Module t v -> String
8noscript modul = concat (extract modul)
10class Extract a where
11 extract :: a -> [String]
13instance Extract (Module t v) where
14 extract (Module _ _ _ stmts) =
15 map (\s -> "<p>" ++ s ++ "</p>") (concatMap extract stmts)
17instance Extract (Declaration t v) where
18 extract (Definition d) = extract d
19 extract _ = []
21instance Extract (Def t v) where
22 extract (Def _ e) = extract e
23 extract _ = []
25instance Extract e => Extract (Located e) where
26 extract (L _ e) = extract e
28instance Extract (Expr t v) where
29 extract expr =
30 let f = extract in
31 case expr of
32 Literal (Str s) -> [s]
33 Binop op e1 e2 -> case (op, f e1, f e2) of
34 ("++", [s1], [s2]) -> [s1 ++ s2]
35 (_ , ss1 , ss2 ) -> ss1 ++ ss2
36 Lambda v e -> f e
37 App (L _ (App (L _ (App (L _ (Var func)) w)) h)) src
38 | "image" `isInfixOf` func -> extractImage src
39 App (L _ (App (L _ (Var func)) src)) txt
40 | "link" `isInfixOf` func -> extractLink src txt
41 App (L _ (Var func)) e
42 | "header" `isInfixOf` func -> tag "h1" e
43 | "bold" `isInfixOf` func -> tag "b" e
44 | "italic" `isInfixOf` func -> tag "i" e
45 | "monospace" `isInfixOf` func -> tag "code" e
46 App e1 e2 -> f e1 ++ f e2
47 Let defs e -> concatMap extract defs ++ f e
48 Var _ -> []
49 Case e cases -> concatMap (f . snd) cases
50 Data _ es -> concatMap f es
51 MultiIf es -> concatMap (f . snd) es
52 Markdown doc _ -> [ Pan.writeHtmlString Pan.def doc ]
53 _ -> []
55extractLink src txt =
56 case (extract src, extract txt) of
57 ([s1],[s2]) -> [ "<a href=\"" ++ s1 ++ "\">" ++ s2 ++ "</a>" ]
58 ( ss1, ss2) -> ss1 ++ ss2
60extractImage src =
61 case extract src of
62 [s] -> ["<img src=\"" ++ s ++ "\">"]
63 ss -> ss
65tag t e = map (\s -> concat [ "<", t, ">", s, "</", t, ">" ]) (extract e)