Add Text.PrettyPrint.Prettyprinter.Render.HTML to contrib as well

This commit is contained in:
Johann Rudloff 2021-04-29 13:49:04 +02:00
parent 9591b147a4
commit 8d21a292d0
2 changed files with 37 additions and 0 deletions

View File

@ -0,0 +1,36 @@
module Text.PrettyPrint.Prettyprinter.Render.HTML
import Data.List
import Data.Strings
export
htmlEscape : String -> String
htmlEscape s = fastAppend $ reverse $ go [] s
where
isSafe : Char -> Bool
isSafe '"' = False
isSafe '<' = False
isSafe '>' = False
isSafe '&' = False
isSafe '\'' = False
isSafe '\t' = True
isSafe '\n' = True
isSafe '\r' = True
isSafe c = (c >= ' ' && c <= '~')
htmlQuote : Char -> String
htmlQuote '"' = "&quot;"
htmlQuote '<' = "&lt;"
htmlQuote '>' = "&gt;"
htmlQuote '&' = "&amp;"
htmlQuote '\'' = "&apos;"
htmlQuote c = "&#" ++ (show $ ord c) ++ ";"
go : List String -> String -> List String
go acc "" = acc
go acc s =
case span isSafe s of
(safe, "") => safe::acc
(safe, rest) => let c = assert_total (strIndex rest 0)
escaped = htmlQuote c in
go (escaped::safe::acc) (assert_total $ strTail rest)

View File

@ -161,5 +161,6 @@ modules = Control.ANSI,
Text.PrettyPrint.Prettyprinter.Symbols,
Text.PrettyPrint.Prettyprinter.Util,
Text.PrettyPrint.Prettyprinter.SimpleDocTree,
Text.PrettyPrint.Prettyprinter.Render.HTML,
Text.PrettyPrint.Prettyprinter.Render.String,
Text.PrettyPrint.Prettyprinter.Render.Terminal