-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTMLFormat.fs
60 lines (47 loc) · 2.17 KB
/
HTMLFormat.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
namespace MarkdownWithFs
open Markdown
open System.Text
module HTMLFormat =
let appendLine s (sb:StringBuilder) = sb.AppendLine s
let writeTag (builder:StringBuilder) tag (body:StringBuilder -> StringBuilder) =
builder.Append(sprintf "<%s>" tag)
|> body
|> appendLine (sprintf "</%s>" tag)
let writeTag' (builder:StringBuilder) tag attr (body:StringBuilder -> StringBuilder) =
builder.Append(sprintf "<%s" tag)
|> fun x -> attr |> Seq.fold (fun s (k,v) -> x.Append(sprintf " %s=\"%s\"" k v)) x
|> fun x -> x.Append(">")
|> body
|> appendLine (sprintf "</%s>" tag)
let markdownSpansToHTML (spans:MarkdownSpans) (builder) =
let rec loop xs (sb:StringBuilder) =
match xs with
| Literal s :: xs ->
sb.Append(s) |> loop xs
| InlineCode s :: xs ->
writeTag sb "code" (appendLine s) |> loop xs
| Strong s :: xs ->
writeTag sb "strong" (loop s) |> loop xs
| Emphasis s :: xs ->
writeTag sb "em" (loop s) |> loop xs
| HyperLink(s,link) :: xs ->
writeTag' sb "a" [("href",link)] (loop s) |> loop xs
| HardLineBreak :: xs ->
writeTag sb "br" (id) |> loop xs
| [] -> sb
loop spans builder
let markdownToHTML (md:MarkdownDocument) (builder) =
let rec loop xs sb =
match xs with
| Heading(v, spans) :: xs ->
writeTag sb (sprintf "h%i" v) (markdownSpansToHTML spans) |> loop xs
| Paragraph spans :: xs ->
writeTag sb "p" (markdownSpansToHTML spans) |> loop xs
| CodeBlock code :: xs ->
writeTag sb "pre" (fun strBuilder -> writeTag strBuilder "code" (fun x -> code |> List.fold (fun s el -> s.AppendLine(el)) x)) |> loop xs
| BlockQuote md :: xs ->
writeTag sb "blockquote" (loop md) |> loop xs
| [] -> sb
loop md builder
let exportToHTML (md:MarkdownDocument) =
markdownToHTML md (new StringBuilder()) |> (fun x -> x.ToString())