0 | module Text.PrettyPrint.Prettyprinter.Render.HTML
 1 |
 2 | import Data.String
 3 |
 4 | %default covering
 5 |
 6 | export
 7 | htmlEscape : String -> String
 8 | htmlEscape s = concat $ reverse $ go [] s
 9 |   where
10 |     isSafe : Char -> Bool
11 |     isSafe '"' = False
12 |     isSafe '<' = False
13 |     isSafe '>' = False
14 |     isSafe '&' = False
15 |     isSafe '\'' = False
16 |     isSafe '\t' = True
17 |     isSafe '\n' = True
18 |     isSafe '\r' = True
19 |     isSafe c = (c >= ' ' && c <= '~')
20 |
21 |     htmlQuote : Char -> String
22 |     htmlQuote '"' = "&quot;"
23 |     htmlQuote '<' = "&lt;"
24 |     htmlQuote '>' = "&gt;"
25 |     htmlQuote '&' = "&amp;"
26 |     htmlQuote '\'' = "&apos;"
27 |     htmlQuote c = "&#" ++ (show $ ord c) ++ ";"
28 |
29 |     go : List String -> String -> List String
30 |     go acc "" = acc
31 |     go acc s =
32 |       case span isSafe s of
33 |            (safe, "") => safe::acc
34 |            (safe, rest) => let c = assert_total (strIndex rest 0)
35 |                                escaped = htmlQuote c in
36 |                                go (escaped::safe::acc) (assert_total $ strTail rest)
37 |