on 46df5ff help sign in
elk/elm/src/Gen/ExtractNoscript.hs
35 lines 1.2 kB history raw
2module ExtractNoscript (extract) where
4import Ast
6extract (Module _ _ _ stmts) =
7 concatMap (\s -> "<p>" ++ s ++ "</p>") (concatMap stmtExtract stmts)
8 where stmtExtract (Def _ _ e) = extract' e
9 stmtExtract _ = []
11extract' expr =
12 let f = extract' in
13 case expr of
14 Str s -> [s]
15 Binop op e1 e2 -> case (op, f e1, f e2) of
16 ("++", [s1], [s2]) -> [s1 ++ s2]
17 (_ , ss1 , ss2 ) -> ss1 ++ ss2
18 Lambda v e -> f e
19 App (App (Var "link") src) txt ->
20 case (f src, f txt) of
21 ([s1],[s2]) -> [ "<a href=\"" ++ s1 ++ "\">" ++ s2 ++ "</a>" ]
22 ( ss1, ss2) -> ss1 ++ ss2
23 App (Var "header") e -> tag "h1" e
24 App (Var "bold") e -> tag "b" e
25 App (Var "italic") e -> tag "i" e
26 App (Var "monospace") e -> tag "code" e
27 App e1 e2 -> f e1 ++ f e2
28 If eb et ef -> f et ++ f ef
29 Let defs e -> concatMap (\(Definition _ _ d) -> f d) defs ++ f e
30 Var _ -> []
31 Case e cases -> concatMap (f . snd) cases
32 Data _ es -> concatMap f es
33 _ -> []
35tag t e = map (\s -> concat [ "<", t, ">", s, "</", t, ">" ]) (extract' e)