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