-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgom2h.go
293 lines (251 loc) · 7.17 KB
/
gom2h.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package gom2h
import (
"bytes"
"fmt"
"regexp"
"strings"
)
var nl = []byte("\n")
// main entry point
func Run(input []byte) ([]byte, error) {
input = bytes.TrimSpace(input)
conved := make([]Line, 0)
for _, line := range bytes.Split(input, nl) {
if len(line) == 0 {
continue
}
l, err := conv(line)
if err != nil {
return nil, err
}
conved = append(conved, l)
}
return render(conved), nil
}
// convert markdown line to Line
type LineType int
const (
Header LineType = iota
Blockquote
List
CodeFence
Paragraph
HTMLBlock
NewLine
)
type Line struct {
ty LineType
lv int
val []byte
dep int
}
// https://play.golang.org/p/igrR4P6blOD
var (
headerExp = regexp.MustCompile(`^(#){1,6} (.+)`)
blockquoteExp = regexp.MustCompile(`^(>+)(.+)`)
emphasisExp = regexp.MustCompile(`.*([\*]([^\*]+)[\*]).*|.*([_]([^_]+)[_]).*`)
strongExp = regexp.MustCompile(`.*([\*]{2}([^\*]+)[\*]{2}).*|.*([_]{2}([^_]+)[_]{2}).*`)
imageExp = regexp.MustCompile(`^!.*(\[.+\])(\(.+\)).*`)
linkExp = regexp.MustCompile(`.*(\[.+\])(\(.+\)).*`)
listExp = regexp.MustCompile(`^ *(- )(.+)`)
codespanExp = regexp.MustCompile("[^`]*`([^`]+)`[^`]*")
codefenceExp = regexp.MustCompile("^```(.*)")
)
var inCodeBlock = false
func conv(line []byte) (Line, error) {
inCodeSpan := false
// raw html
// TODO: support other html tags
if bytes.HasPrefix(line, []byte("<blockquote")) {
return Line{HTMLBlock, 0, line, 0}, nil
}
// inline
for codespanExp.Match(line) {
loc := codespanExp.FindSubmatchIndex(line)
// This is `cs sample`.
// -> line[loc[2]:loc[3]] // cs sample
line = []byte(fmt.Sprintf(`%s<code>%s</code>%s`, line[:loc[2]-1], line[loc[2]:loc[3]], line[loc[3]+1:]))
inCodeSpan = true
}
if !inCodeSpan && !inCodeBlock {
for strongExp.Match(line) {
loc := strongExp.FindSubmatchIndex(line)
// This is *em* sample
// -> line[loc[2]:loc[3]] // **st**
// -> line[loc[4]:loc[5]] // st
// -> line[loc[6]:loc[7]] // __st__
// -> line[loc[8]:loc[9]] // em
s := loc[2]
c := loc[3]
ts := loc[4]
tc := loc[5]
if s == -1 && c == -1 && ts == -1 && tc == -1 {
s = loc[6]
c = loc[7]
ts = loc[8]
tc = loc[9]
}
bef := []byte(fmt.Sprintf(`%s`, line[loc[0]:s]))
target := []byte(fmt.Sprintf(`<strong>%s</strong>`, line[ts:tc]))
aft := []byte(fmt.Sprintf(`%s`, line[c:]))
line = append(bef, append(target, aft...)...)
}
for emphasisExp.Match(line) {
loc := emphasisExp.FindSubmatchIndex(line)
// This is *em* sample
// -> line[loc[2]:loc[3]] // *em*
// -> line[loc[4]:loc[5]] // em
// -> line[loc[6]:loc[7]] // _em_
// -> line[loc[8]:loc[9]] // em
s := loc[2]
c := loc[3]
ts := loc[4]
tc := loc[5]
if s == -1 && c == -1 && ts == -1 && tc == -1 {
s = loc[6]
c = loc[7]
ts = loc[8]
tc = loc[9]
}
bef := []byte(fmt.Sprintf(`%s`, line[loc[0]:s]))
target := []byte(fmt.Sprintf(`<em>%s</em>`, line[ts:tc]))
aft := []byte(fmt.Sprintf(`%s`, line[c:]))
line = append(bef, append(target, aft...)...)
}
for imageExp.Match(line) {
loc := linkExp.FindSubmatchIndex(line)
// ![image](/path/to/image)
// -> line[loc[2]:loc[3]] // [image]
// -> line[loc[4]:loc[5]] // (/path/to/image)
line = []byte(fmt.Sprintf(`<img src="%s" alt="%s" />`, line[loc[4]+1:loc[5]-1], line[loc[2]+1:loc[3]-1]))
}
for linkExp.Match(line) {
loc := linkExp.FindSubmatchIndex(line)
// This is [link](https://example.org/)
// -> line[loc[2]:loc[3]] // [link]
// -> line[loc[4]:loc[5]] // (https://example.org/)
bef := []byte(fmt.Sprintf(`%s`, line[loc[0]:loc[2]]))
target := []byte(fmt.Sprintf(`<a href="%s">%s</a>`, line[loc[4]+1:loc[5]-1], line[loc[2]+1:loc[3]-1]))
aft := []byte(fmt.Sprintf(`%s`, line[loc[5]:]))
line = append(bef, append(target, aft...)...)
}
}
// block
if headerExp.Match(line) {
loc := headerExp.FindSubmatchIndex(line)
// ## Header2
// -> line[loc[0]:loc[3]] // ##
// -> line[loc[4]:loc[5]] // Header2
return Line{Header, loc[3], line[loc[4]:loc[5]], 0}, nil
}
if blockquoteExp.Match(line) {
loc := blockquoteExp.FindSubmatchIndex(line)
// > quote
// -> line[loc[0]:loc[3]] // >
// -> line[loc[4]:loc[4]] // quote
return Line{Blockquote, loc[3], line[loc[4]:loc[5]], 0}, nil
}
if listExp.Match(line) {
loc := listExp.FindSubmatchIndex(line)
// - list
// -> line[loc[4]:loc[5]] // list
return Line{List, 0, line[loc[4]:loc[5]], loc[2] / 2}, nil
}
if codefenceExp.Match(line) {
loc := codefenceExp.FindSubmatchIndex(line)
// ```go
// -> line[loc[2]:loc[3]] // go
var lang []byte
if loc[2] != loc[3] {
lang = line[loc[2]:loc[3]]
}
inCodeBlock = !inCodeBlock
return Line{CodeFence, 0, lang, 0}, nil
}
return Line{Paragraph, 0, line, 0}, nil
}
// render html from Line
func render(lines []Line) []byte {
ret := make([]byte, 0)
inCodeFence := false
for idx, line := range lines {
// render html
switch line.ty {
case Header:
if inCodeFence {
ret = append(ret, []byte(fmt.Sprintf(`%s %s`, strings.Repeat("#", line.lv), line.val))...)
} else {
ret = append(ret, []byte(fmt.Sprintf(`<h%d>%s</h%d>`, line.lv, line.val, line.lv))...)
}
case Blockquote:
if inCodeFence {
ret = append(ret, []byte(fmt.Sprintf(`%s%s`, strings.Repeat(">", line.lv), line.val))...)
} else {
var stag string
var ctag string
for i := 0; i < line.lv; i++ {
stag = fmt.Sprintf(`<blockquote>%s`, stag)
ctag = fmt.Sprintf(`%s</blockquote>`, ctag)
}
ret = append(ret, []byte(fmt.Sprintf(`%s<p>%s</p>%s`, stag, bytes.TrimSpace(line.val), ctag))...)
}
case List:
if inCodeFence {
ret = append(ret, []byte(fmt.Sprintf(`- %s`, line.val))...)
} else {
if (idx > 0 && lines[idx-1].ty != List) || idx == 0 {
ret = append(ret, []byte(`<ul>`)...)
ret = newline(ret)
}
if idx > 0 && lines[idx-1].dep < line.dep {
ret = append(ret, []byte(`<ul>`)...)
ret = newline(ret)
}
ret = append(ret, []byte(fmt.Sprintf(`<li>%s</li>`, line.val))...)
if idx < len(lines)-1 && line.dep > lines[idx+1].dep {
for d := line.dep - lines[idx+1].dep; d > 0; d-- {
ret = newline(ret)
ret = append(ret, []byte(`</ul>`)...)
}
}
if idx == len(lines)-1 && line.dep > 0 {
for d := line.dep; d > 0; d-- {
ret = newline(ret)
ret = append(ret, []byte(`</ul>`)...)
}
}
if (idx < len(lines)-1 && lines[idx+1].ty != List) || idx == len(lines)-1 {
ret = newline(ret)
ret = append(ret, []byte(`</ul>`)...)
}
}
case CodeFence:
if !inCodeFence {
if line.val != nil {
ret = append(ret, []byte(fmt.Sprintf(`<pre><code class="%s">`, line.val))...)
} else {
ret = append(ret, []byte(`<pre><code>`)...)
}
} else {
ret = append(ret, []byte(`</code></pre>`)...)
}
inCodeFence = !inCodeFence
continue
case HTMLBlock:
ret = append(ret, line.val...)
case Paragraph:
if !inCodeFence {
ret = append(ret, []byte(fmt.Sprintf(`<p>%s</p>`, line.val))...)
} else {
ret = append(ret, []byte(fmt.Sprintf(`%s`, line.val))...)
}
}
if idx != len(lines)-1 {
ret = newline(ret)
}
}
return ret
}
func newline(ret []byte) []byte {
return append(ret, nl...)
}