0 | module Text.Markdown.Format.Html
2 | import Data.String.Extra
3 | import Internal.String
4 | import Text.Markdown.Data
8 | showProps : List (String, String) -> String
10 | showProps (p :: ps) = " \{fst p}=\"\{snd p}\"\{showProps ps}"
12 | htmlElementProps : String -> List (String, String) -> String -> String
13 | htmlElementProps el props contents =
14 | "<" ++ el ++ (showProps props) ++ ">" ++ contents ++ "</" ++ el ++ ">"
16 | htmlElement : String -> String -> String
17 | htmlElement el contents =
18 | htmlElementProps el [] contents
26 | htmlVoidElement : String -> String
27 | htmlVoidElement el = "<\{el}>"
31 | escapeText : String -> String
33 | (replace "<" "<")
34 | . (replace ">" ">")
38 | inlineToHtml : Inline -> String
39 | inlineToHtml (Text text) = text
40 | inlineToHtml (Pre text) = htmlElement "code" (escapeText text)
41 | inlineToHtml (CodeBlock text _) = htmlElement "pre" (escapeText text)
42 | inlineToHtml (Italics inlines) = htmlElement "em" (inlinesToHtml inlines)
43 | inlineToHtml (Bold inlines) = htmlElement "strong" (inlinesToHtml inlines)
44 | inlineToHtml (Image alt src) = htmlElementProps "img" [("src", src), ("alt", alt)] ""
45 | inlineToHtml (Link desc href) = htmlElementProps "a" [("href", href)] desc
46 | inlineToHtml (Html tag inlines) = htmlElement tag (inlinesToHtml inlines)
47 | inlineToHtml NewLine = "\n"
50 | inlinesToHtml : List Inline -> String
51 | inlinesToHtml inlines =
52 | join "" (map inlineToHtml inlines)
55 | blockToHtml : Block -> String
56 | blockToHtml (Header level inlines) = htmlElement ("h" ++ (show level)) (inlinesToHtml inlines)
57 | blockToHtml HorizontalRules = htmlVoidElement "hr"
58 | blockToHtml (Paragraph inlines) = htmlElement "p" (inlinesToHtml inlines)
63 | toHtml : Markdown -> String
65 | join "\n" (map blockToHtml els)