0 | module Text.Markdown.Format.Html
 1 |
 2 | import Data.String.Extra
 3 | import Internal.String
 4 | import Text.Markdown.Data
 5 |
 6 | %default total
 7 |
 8 | showProps : List (String, String) -> String
 9 | showProps [] = ""
10 | showProps (p :: ps) = " \{fst p}=\"\{snd p}\"\{showProps ps}"
11 |
12 | htmlElementProps : String -> List (String, String) -> String -> String
13 | htmlElementProps el props contents =
14 |   "<" ++ el ++ (showProps props) ++ ">" ++ contents ++ "</" ++ el ++ ">"
15 |
16 | htmlElement : String -> String -> String
17 | htmlElement el contents =
18 |   htmlElementProps el [] contents
19 |
20 | ||| A HTML void element with no content.
21 | |||
22 | ||| Typical void elements are **br** and **hr**.
23 | |||
24 | ||| Note: the HTML5 syntax is used. Therefore, such elements are rendered as
25 | ||| `<br>`, `<hr>`, etc. without self-closing slash.
26 | htmlVoidElement : String -> String
27 | htmlVoidElement el = "<\{el}>"
28 |
29 | ||| TODO: Escape HTML-type elements
30 | covering
31 | escapeText : String -> String
32 | escapeText =
33 |   (replace "<" "&lt;")
34 |   . (replace ">" "&gt;")
35 |
36 | mutual
37 |   covering
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"
48 |
49 |   covering
50 |   inlinesToHtml : List Inline -> String
51 |   inlinesToHtml inlines =
52 |     join "" (map inlineToHtml inlines)
53 |
54 | covering
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)
59 |
60 | ||| Convert a Markdown value into Html
61 | public export
62 | covering
63 | toHtml : Markdown -> String
64 | toHtml (Doc els) =
65 |   join "\n" (map blockToHtml els)
66 |