-
Notifications
You must be signed in to change notification settings - Fork 0
/
md2html.go
252 lines (217 loc) · 6.17 KB
/
md2html.go
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package main
import (
"regexp"
"strings"
)
var reBold, reItalic, reStrike, reLink, reCode, reImg, reImgHtml *regexp.Regexp
type MarkdownParser struct {
markdown string // original Markdown
html string // resulting HTML
title string // internal
descriptions []string // internal
pre bool // internal
quote bool // internal
li bool // internal
}
func init() {
// text **in bold**
reBold = regexp.MustCompile("(\\*\\*)(.*?)(\\*\\*)")
// text *in italic*
reItalic = regexp.MustCompile("(\\*)(.*?)(\\*)")
// ~~striked text~~
reStrike = regexp.MustCompile("(~~)(.*?)(~~)")
// [some text](http://somewhere.org)
// \\[ starts with [
// ([^\\]]*?) followed by any character except ] using lazy match
// \\] followed by an ending ]
// \\( followed by (
// (.*?) followed by any character using lazy match
// \\) ends with )
//
// Using ([^\\]]*?) instead of (.*?) to prevent the parser from
// picking up text in brackets that is not an URL, for example:
// [1] [book x](http://link/to/bookx)
reLink = regexp.MustCompile("\\[([^\\]]*?)\\]\\((.*?)\\)")
// `hello world`
reCode = regexp.MustCompile("(`)(.*?)(`)")
// ![image caption](http://somewhere.org/image.png)
reImg = regexp.MustCompile("!\\[([^\\]]*?)\\]\\((.*?)\\)")
// <img ... />
reImgHtml = regexp.MustCompile(`<img (.*) />`)
}
func NewMarkdownParser(markdown string) MarkdownParser {
parser := MarkdownParser{markdown: markdown}
parser.processMarkdown()
return parser
}
func (p *MarkdownParser) processMarkdown() {
p.html = ""
p.pre = false
p.quote = false
p.li = false
p.descriptions = []string{}
p.title = ""
lines := strings.Split(p.markdown, "\n")
for _, line := range lines {
if p.isQuote(line) {
if p.quote {
// already in blockquote
} else {
// start a new blockquote
p.html += "<blockquote>\n"
p.quote = true
}
} else {
if p.quote {
// end current blockquote
p.html += "</blockquote>\n"
p.quote = false
}
}
if p.isListItem(line) {
if p.li {
// already inside a list
} else {
// start a new list
p.html += "<ul>\n"
p.li = true
}
} else {
if p.li {
// end current list
p.html += "</ul>\n"
p.li = false
}
}
l := strings.TrimSpace(line)
if p.isH1(l) {
p.html += "<h1>" + substr(l, 2) + "</h1>\n"
if p.title == "" {
p.title = chomp(strings.TrimPrefix(l, "# "))
}
} else if p.isH2(l) {
p.html += "<h2>" + substr(l, 3) + "</h2>\n"
} else if p.isH3(l) {
p.html += "<h3>" + substr(l, 4) + "</h3>\n"
} else if p.isPreTerminal(l) {
p.html += "<pre class=\"terminal\">\n"
p.pre = true
} else if p.isPreCode(l) {
p.html += "<pre class=\"code\">\n"
p.pre = true
} else if p.isPre(l) {
if p.pre {
p.html += "</pre>\n"
p.pre = false
} else {
p.html += "<pre>\n"
p.pre = true
}
} else if l == "" {
// html += "<br/>\n"
p.html += "\n"
} else {
if p.pre {
// we use the original line in pre to preserve spaces
p.html += p.parsedLine(line, true) + "\n"
} else if p.quote {
p.html += p.parsedLine(substr(l, 2), false) + "<br/>\n"
} else if p.li {
p.html += "<li>" + p.parsedLine(substr(l, 2), false) + "\n"
} else {
// A regular paragraph
p.html += "<p>" + p.parsedLine(l, false) + "</p>\n"
// Aggregate the first few paragraps to use as the description
if len(p.descriptions) < 3 {
p.descriptions = append(p.descriptions, p.cleanLine(l))
}
}
}
}
}
func (p MarkdownParser) Html() string {
return p.html
}
func (p MarkdownParser) Title() string {
return p.title
}
func (p MarkdownParser) Description() string {
return strings.Join(p.descriptions, " ")
}
func (p MarkdownParser) isH1(line string) bool {
if p.pre {
return false
}
return strings.HasPrefix(line, "# ")
}
func (p MarkdownParser) isH2(line string) bool {
if p.pre {
return false
}
return strings.HasPrefix(line, "## ")
}
func (p MarkdownParser) isH3(line string) bool {
if p.pre {
return false
}
return strings.HasPrefix(line, "### ")
}
func substr(line string, i int) string {
if i >= len(line) {
return ""
}
return line[i:]
}
func chomp(text string) string {
text = strings.TrimSuffix(text, "\r\n")
text = strings.TrimSuffix(text, "\r")
text = strings.TrimSuffix(text, "\n")
return text
}
func (p MarkdownParser) isPreTerminal(line string) bool {
return strings.HasPrefix(line, "```terminal")
}
func (p MarkdownParser) isPreCode(line string) bool {
return strings.HasPrefix(line, "```code")
}
func (p MarkdownParser) isPre(line string) bool {
return strings.HasPrefix(line, "```")
}
func (p MarkdownParser) isQuote(line string) bool {
return strings.HasPrefix(line, "> ") || line == ">"
}
func (p MarkdownParser) isListItem(line string) bool {
return strings.HasPrefix(line, "* ")
}
func (p MarkdownParser) cleanLine(line string) string {
// Ditch HTML image tags (although we allow them in the Markdown
// we don't want it on the "clean lines")
line = reImgHtml.ReplaceAllString(line, " ")
line = strings.Replace(line, "<", "<", -1)
line = strings.Replace(line, ">", ">", -1)
line = strings.Replace(line, "\"", " ", -1)
return line
}
func (p MarkdownParser) parsedLine(line string, pre bool) string {
// Encode the < and > characters in the original Markdown...
line = strings.Replace(line, "<", "<", -1)
line = strings.Replace(line, ">", ">", -1)
// and then allow for <img ... />
line = strings.Replace(line, "<img ", "<img ", -1)
line = strings.Replace(line, "/>", "/>", -1)
// allow for <sup> </sup>
line = strings.Replace(line, "<sup>", "<sup>", -1)
line = strings.Replace(line, "</sup>", "</sup>", -1)
if pre {
// don't do any extra processing if we are on code block
return line
}
// and finally convert the Markdown to HTML
line = reImg.ReplaceAllString(line, "<img src=\"$2\" alt=\"$1\" title=\"$1\" />")
line = reBold.ReplaceAllString(line, "<b>$2</b>")
line = reItalic.ReplaceAllString(line, "<i>$2</i>")
line = reStrike.ReplaceAllString(line, "<strike>$2</strike>")
line = reLink.ReplaceAllString(line, "<a href=\"$2\">$1</a>")
line = reCode.ReplaceAllString(line, "<code>$2</code>")
return line
}