on e33d329 help sign in
elk/src/ExtractNoscript.hs
43 lines 1.4 kB history raw
2module ExtractNoscript (extract) where
4import Ast
5import Data.List (isPrefixOf)
7extract = extract'
9extract' expr = makeLinks terms where
10 terms =
11 case expr of
12 Number _ -> []
13 Chr _ -> []
14 Boolean _ -> []
15 Range _ _ -> []
16 Access _ _ -> []
17 Binop op e1 e2 -> case (op, extract' e1, extract' e2) of
18 ("++", [s1], [s2]) -> [ s1 ++ s2 ]
19 (_ , ss1 , ss2 ) -> ss1 ++ ss2
20 Lambda v e -> extract' e
21 App e1 e2 -> extract' e1 ++ extract' e2
22 If eb et ef -> extract' et ++ extract' ef
23 Lift f es -> extract' f ++ concatMap extract es
24 Fold e1 e2 e3 -> concatMap extract' [e1,e2,e3]
25 Async e -> extract' e
26 Input _ -> []
27 Let defs e -> concatMap (extract' . snd) defs ++ extract' e
28 Var _ -> []
29 Case e cases -> concatMap (extract' . snd) cases
30 Data _ _ -> [toString expr]
32toString (Data "Cons" [Chr c, t]) = c : toString t
33toString _ = []
35makeLinks (a:b:[]) = linkify a b
36makeLinks xs = xs
38linkish s = not ("<a" `isPrefixOf` s) && notElem ' ' s && elem '/' s
40linkify a b
41 | linkish a = [ "<a href=\"" ++ a ++ "\">" ++ b ++ "</a>" ]
42 | linkish b = [ "<a href=\"" ++ b ++ "\">" ++ a ++ "</a>" ]
43 | otherwise = [a,b]