0 | module Text.Markdown.Format.Html
2 | import Data.String.Extra
3 | import Text.Markdown.Data
8 | showProps : List (String, String) -> String
10 | showProps (p :: ps) = " \{fst p}=\"\{snd p}\"\{showProps ps}"
13 | htmlElementProps : String -> List (String, String) -> String -> String
14 | htmlElementProps el props contents =
15 | "<\{el}\{showProps props}>\{contents}</\{el}>"
18 | htmlElement : String -> String -> String
19 | htmlElement el contents = htmlElementProps el [] contents
28 | htmlVoidElement : String -> String
29 | htmlVoidElement el = "<\{el}>"
35 | escapeText : String -> String
36 | escapeText = pack . escapeHelper . unpack
38 | escapeHelper : List Char -> List Char
39 | escapeHelper [] = []
40 | escapeHelper ('"' :: cs) = unpack """ ++ escapeHelper cs
41 | escapeHelper ('\'' :: cs) = unpack "'" ++ escapeHelper cs
42 | escapeHelper ('&' :: cs) = unpack "&" ++ escapeHelper cs
43 | escapeHelper ('<' :: cs) = unpack "<" ++ escapeHelper cs
44 | escapeHelper ('>' :: cs) = unpack ">" ++ escapeHelper cs
45 | escapeHelper (c :: cs) = (c :: escapeHelper cs)
50 | inlineToHtml : Inline -> String
51 | inlineToHtml (Text text) = text
52 | inlineToHtml (Pre text) = htmlElement "code" (escapeText text)
53 | inlineToHtml (Italics inlines) = htmlElement "em" (inlinesToHtml inlines)
54 | inlineToHtml (Bold inlines) = htmlElement "strong" (inlinesToHtml inlines)
55 | inlineToHtml (Image alt src) = htmlElementProps
57 | [("src", src), ("alt", alt)]
59 | inlineToHtml (Link desc href) = htmlElementProps
62 | (inlinesToHtml desc)
63 | inlineToHtml (Html tag inlines) = htmlElement tag (inlinesToHtml inlines)
67 | inlinesToHtml : List Inline -> String
68 | inlinesToHtml inlines = join "" (map inlineToHtml inlines)
73 | listItemsToHtml : List ListItem -> String
74 | listItemsToHtml [] = ""
75 | listItemsToHtml (x :: xs) =
76 | htmlElement "li" (inlinesToHtml x) <+> listItemsToHtml xs
80 | blockToHtml : Block -> String
81 | blockToHtml (Header level inlines) = htmlElement
82 | ("h" ++ (show level))
83 | (inlinesToHtml inlines)
84 | blockToHtml (UList items) = htmlElement "ul" (listItemsToHtml items)
85 | blockToHtml (CodeBlock text _) = htmlElement
87 | (htmlElement "code" $
escapeText text)
88 | blockToHtml HorizontalRules = htmlVoidElement "hr"
89 | blockToHtml (Paragraph inlines) = htmlElement "p" (inlinesToHtml inlines)
94 | toHtml : Markdown -> String
95 | toHtml (Doc els) = join "\n" (map blockToHtml els)