aboutsummaryrefslogtreecommitdiffstats
path: root/FMark/src/Common/HTMLGen/HTMLGen.fs
blob: c15f34aefb238821b8ea2087f404ad9a2ac28700 (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
module HTMLGen

open Types
open Shared
open Logger
open HTMLGenHelpers

/// convert TFrmtedString to string, with HTML tags where necessary
let rec strFStr fStr =
    match fStr with
    | Literal str -> str
    | Code str -> str |> attachSimpleTag "code"
    | Strong a -> strInlineElements a |> attachSimpleTag "strong"
    | Emphasis e -> strInlineElements e |> attachSimpleTag "em"
    | Line l -> strInlineElements l
    | Strike l -> strInlineElements l |> attachSimpleTag "del"

/// convert InlineElement list to string, with HTML tags where necessary
/// not tail recursive because the code looks cleaner this way
and strInlineElements eles =
    let convertHtml pStr ele =
        let refPrint foot (ht,id) = 
            ht
            |> strFStr
            |> attachHTMLTag ("a", [("href", "#"+id)], true)
            |> (fun r -> if foot then attachSimpleTag "sup" r else r) 
        pStr +
        match ele with
        | FrmtedString fStr -> strFStr fStr
        | Link (ht, url) -> strFStr ht |> attachHTMLTag ("a", [("href", url)], true)
        | Picture (alt, url) ->
            let attrs = [("src", url); ("alt", alt)]
            attachHTMLTag ("img", attrs, false) ""
        // style for inline referencing the footnotes and citations in the end
        | InlineCitation (ht, id) -> refPrint false (ht,id)
        | InlineFootnote (ht, id) -> refPrint true (ht,id)
    List.fold convertHtml "" eles

/// process Markdown paragraph
let strParagraph lines =
    let folder pLinesStr line =
        pLinesStr + strInlineElements line + NewLineStr
    List.fold folder "" lines
    |> (fun x -> x.Trim()) // remove leading and trailing whitespaces and CRLFs
    |> attachSimpleTag "p"


/// process Markdown Table
let strTable (rows: PRow list) =
    // filter out table header
    let containHeader (row: PRow) =
        //let PCells(_, isHeader) = row
        match row with
        | PCells(_, isHeader) ->
            isHeader
    let takeoutCells = List.map (fun pRow -> match pRow with | PCells(cells,_) -> cells)
    let headerRows = List.filter (containHeader) rows |> takeoutCells
    let bodyRows = List.filter (containHeader >> not) rows |> takeoutCells
    let foldCells row =
        let cellsFolder pStr cell =
            match cell with
            | CellLine(line, isHeader, align) ->
                let tagName = if isHeader then "th" else "td"
                let cellContent = strInlineElements line
                let alignAttr =
                    match align with
                    | Centre -> ("align", "center")
                    | Right -> ("align", "right")
                    | Left -> ("align", "left")
                    | NoAlign -> ("","")
                pStr + attachHTMLTag (tagName, [alignAttr], true) cellContent
        List.fold cellsFolder "" row
    let foldRows rows =
        let rowsFolder pStr row =
            foldCells row
            |> attachSimpleTag "tr"
            |> fun s -> pStr + s
        List.fold rowsFolder "" rows
    foldRows headerRows |> attachSimpleTag "thead"
    |> fun s ->
        s + (foldRows bodyRows |> attachSimpleTag "tbody")
    |> attachSimpleTag "table"


/// recursively process a list
let rec strList list =
    let strListItem pStr li =
        pStr + (
            match li with
            | StringItem(line) -> strInlineElements line |> attachSimpleTag "li"
            | NestedList(list) -> strList list
            )
    match list with
    | {ListType=lt; ListItem=liS} ->
        match lt with
        | UL ->
            List.fold strListItem "" liS
            |> attachSimpleTag "ul"
        | OL startNo ->
            List.fold strListItem "" liS
            |> attachHTMLTag ("ol", ["start", startNo|>string], true)
        

/// process header
let strHeader (header,id) =
    match header with
    | {HeaderName=line;Level=lv} ->
        let tagName = "h" + string(lv)
        line
        |> strInlineElements
        |> attachHTMLTag (tagName, ["id", id], true)

/// process references
/// id: the href id used in inline referencing
/// content: of TLine type, to be displayed at the end of HTML doc
let strRef (id, content) =
    "["+id+"] " + strInlineElements content
    |> attachHTMLTag ("p", ["id", id], true)

let (|MatchHeaderAndSubHeader|_|) hds =
    match hds with
    | fstHd::sndHd::_ ->
        let {Level=fstLv} = fstHd
        let {Level=sndLv} =sndHd
        if sndLv > fstLv then (List.head hds, List.tail hds) |> Some else None
    | _ -> None

/// process table of contents

let strToC (toc: Ttoc) =
    let excludeSelfSkip x = match x with | None -> None | Some 1 -> None | Some n -> Some (n-1)
    /// get all list items in current item level and sub lists
    let rec getCurrentHeaders currentLv hdListItems headers =
        match headers with
        | header:: reHeaders ->
            match header.Level with
            | hdLv when currentLv <= hdLv -> // list item and sub list item
                getCurrentHeaders currentLv (header::hdListItems) reHeaders
            | _ -> hdListItems |> List.rev
        | [] -> hdListItems |> List.rev

    let rec parseHdsIntoList level (headers: THeader list) =
        let depth = (List.head headers).Level
        let headerFolder (currentLv, listItems, (skipNo: int option), currentHdNo) header =
            match skipNo with
            | None ->
                match header.Level with
                | hdLv when hdLv=currentLv ->
                    (currentLv, StringItem(header.HeaderName)::listItems, None, currentHdNo+1)
                | hdLv when hdLv>currentLv ->
                    let (listItem, skip) =
                        xOnwards currentHdNo headers
                        |> getCurrentHeaders (currentLv+1) []
                        |> parseHdsIntoList (currentLv+1)
                    (currentLv, NestedList(listItem)::listItems, skip |> excludeSelfSkip, currentHdNo+1)
                | _ -> failwith "list item level < current level, not possible"
            | Some skip ->
                match skip with
                | 1 -> (currentLv, listItems, None, currentHdNo+1)
                | n when n>1 -> (currentLv, listItems, Some (n-1), currentHdNo+1)
                | _ -> failwith "negative or zero skip number, not possible"
        List.fold headerFolder (level, [], None, 0) headers
        |> (fun (_, lis, _, _) ->
            let doSkip =
                match List.length headers with
                | 0 -> None
                | n -> Some n
            {ListType=OL 1; ListItem=lis |> List.rev; Depth=depth}, doSkip)
    toc.HeaderLst
    |> parseHdsIntoList 1
    |> fst
    |> strList

/// process HTML body part
let strBody pObjs =
    let folder pStr pObj =
        pStr +
        match pObj with
        | Paragraph p -> strParagraph p
        | Quote q -> strInlineElements q |> attachSimpleTag "q"
        | CodeBlock (c, l) -> attachHTMLTag ("code", [("language", mapLang l)], true) c
        | Table rows -> strTable rows
        | List l -> strList l
        | Header (h,s) -> strHeader (h,s)
        | Footnote (i,s) -> strRef ((string i), s)
        | Citation (i,_,s) -> strRef (i, s)
        | ContentTable toc -> strToC toc
        | _ -> sprintf "%A is not implemented" pObj
    List.fold folder "" pObjs


/// generate HTML head
let genHead htmlTitle =
    let metaData =
        [
            [("name", "viewport");("content", "width=device-width")]
        ]
    let genMetadata pStr md =
        pStr + attachMetaTag "meta" md
    List.fold genMetadata "" metaData
    + attachSimpleTag "title" htmlTitle
    
    |> attachSimpleTag "head"

/// generate HTML body
let genBody (pObjs) =
    strBody pObjs
    // insert javascript in the end of HTML doc to make page rendering faster
    +
    attachHTMLTag ("script",
        [
            ("type", "text/javascript");
            ("async", "");
            ("src", "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js?config=TeX-MML-AM_CHTML")
        ], true) ""
    |> attachSimpleTag "body"


/// top level HTMLGen
let genHTML (htmlTitle, pObjs) =
    attachMetaTag "!DOCTYPE" ["html", ""]
    + genHead htmlTitle
    + genBody (pObjs)