forked from PaulSonOfLars/gotg_md2html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
md2html.go
419 lines (371 loc) · 9.72 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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package main
import (
"html"
"strings"
)
var (
tagHTML = map[rune]string{
'_': "i",
'*': "b",
'`': "code",
}
allMdChars = []rune{'_', '*', '`', '[', ']', '(', ')', '\\'}
)
const (
btnURLPrefix = "buttonurl:"
sameLineSuffix = ":same"
)
var defaultConverter = Converter{
BtnPrefix: btnURLPrefix,
SameLineSuffix: sameLineSuffix,
}
type Button struct {
Name string
Content string
SameLine bool
}
type Converter struct {
BtnPrefix string
SameLineSuffix string
}
func New() *Converter {
return &Converter{
BtnPrefix: btnURLPrefix,
SameLineSuffix: sameLineSuffix,
}
}
func MD2HTML(input string) string {
text, _ := defaultConverter.md2html([]rune(html.EscapeString(input)), false)
return text
}
func (cv *Converter) MD2HTML(input string) string {
text, _ := cv.md2html([]rune(html.EscapeString(input)), false)
return text
}
func MD2HTMLButtons(input string) (string, []Button) {
return defaultConverter.md2html([]rune(html.EscapeString(input)), true)
}
func (cv *Converter) MD2HTMLButtons(input string) (string, []Button) {
return cv.md2html([]rune(html.EscapeString(input)), true)
}
func Reverse(s string, buttons []Button) string {
return defaultConverter.Reverse(s, buttons)
}
func (cv *Converter) Reverse(s string, buttons []Button) string {
return cv.reverse([]rune(s), buttons)
}
func StripMD(s string) string {
return defaultConverter.StripMD(s)
}
func (cv *Converter) StripMD(s string) string {
text, _ := cv.MD2HTMLButtons(s)
return cv.stripHTML([]rune(text))
}
func StripHTML(s string) string {
return defaultConverter.stripHTML([]rune(s))
}
func (cv *Converter) StripHTML(s string) string {
return cv.stripHTML([]rune(s))
}
// todo: ``` support? -> add \n char to md chars and hence on \n, skip
func (cv *Converter) md2html(input []rune, buttons bool) (string, []Button) {
var output strings.Builder
v := map[rune][]int{}
var containedMDChars []rune
escaped := false
offset := 0
lastSync := 0
var newInput []rune
for pos, char := range input {
if escaped {
escaped = false
continue
}
switch char {
case '_', '*', '`', '[', ']', '(', ')':
v[char] = append(v[char], pos-offset)
containedMDChars = append(containedMDChars, char)
case '\\':
if len(input) <= pos+1 || !contains(input[pos+1], allMdChars) {
continue
}
escaped = true
newInput = append(newInput, input[lastSync:pos]...)
offset++
lastSync = pos + 1
}
}
input = append(newInput, input[lastSync:]...)
prev := 0
var btnPairs []Button
for i := 0; i < len(containedMDChars) && prev < len(input); i++ {
currChar := containedMDChars[i] // check current character
posArr := v[currChar] // get the list of positions for relevant character
if len(posArr) <= 0 { // if no positions left, pass (sanity check)
continue
}
// if we're past the currChar position, pass and update
if posArr[0] < prev {
v[currChar] = posArr[1:] // more sanity checks; if position is past, move on
continue
}
switch currChar {
case '_', '*', '`':
// if fewer than 2 chars left, pass
if len(posArr) < 2 {
continue
}
bkp := map[rune][]int{}
cnt := i // copy i to avoid changing if false
// skip i to next same char (hence jumping all inbetween) (could be done with a normal range and continues?)
// todo: OOB check on +1?
for _, val := range containedMDChars[cnt+1:] {
cnt++
if val == currChar {
break
}
if x, ok := bkp[val]; ok {
bkp[val] = x[1:]
} else {
bkp[val] = v[val][1:]
}
}
// pop currChar
fstPos, rest := posArr[0], posArr[1:]
v[currChar] = rest
if !validStart(fstPos, input) {
continue
}
ok := false
var skipped int
var sndPos int
for idx, tmpSndPos := range rest {
if validEnd(tmpSndPos, input) {
rest = rest[idx+1:]
sndPos = tmpSndPos
ok = true
break
}
skipped++
}
if !ok {
continue
}
bkp[currChar] = rest
output.WriteString(string(input[prev:fstPos]))
output.WriteString("<" + tagHTML[currChar] + ">")
output.WriteString(string(input[fstPos+1 : sndPos]))
output.WriteString("</" + tagHTML[currChar] + ">")
prev = sndPos + 1
// ensure that items skipped for sndpos balance out with the count
for _, m := range containedMDChars[cnt+1:] {
if skipped <= 0 {
break
}
if m == currChar {
skipped--
}
cnt++
}
i = cnt // set i to copy
for x, y := range bkp {
v[x] = y
}
case '[':
nameOpen, rest := posArr[0], posArr[1:]
v['['] = rest
if len(v[']']) < 1 || len(v['(']) < 1 || len(v[')']) < 1 || nameOpen < prev {
continue
}
var nextNameClose int
var nextLinkOpen int
var nextLinkClose int
wastedLinkClose := v[')']
LinkLabel:
for _, closeLinkPos := range v[')'] {
if closeLinkPos > nameOpen {
wastedLinkOpen := v['(']
for _, openLinkpos := range v['('] {
if openLinkpos > nameOpen && openLinkpos < closeLinkPos {
wastedNameClose := v[']']
for _, closeNamePos := range v[']'] {
if closeNamePos == openLinkpos-1 {
nextNameClose = closeNamePos
nextLinkOpen = openLinkpos
nextLinkClose = closeLinkPos
v[']'] = wastedNameClose
v['('] = wastedLinkOpen
v[')'] = wastedLinkClose
break LinkLabel
}
wastedNameClose = wastedNameClose[1:]
}
}
wastedLinkOpen = wastedLinkOpen[1:]
}
}
wastedLinkClose = wastedLinkClose[1:]
}
if nextLinkClose == 0 {
// invalid
continue
}
output.WriteString(string(input[prev:nameOpen]))
link := string(input[nextLinkOpen+1 : nextLinkClose])
name := string(input[nameOpen+1 : nextNameClose])
if buttons && strings.HasPrefix(link, cv.BtnPrefix) {
// is a button
sameline := strings.HasSuffix(link, cv.SameLineSuffix)
if sameline {
link = link[:len(link)-len(cv.SameLineSuffix)]
}
btnPairs = append(btnPairs, Button{
Name: html.UnescapeString(name),
Content: strings.TrimLeft(strings.TrimSpace(link[len(cv.BtnPrefix):]), "/"),
SameLine: sameline,
})
} else {
output.WriteString(`<a href="` + link + `">` + name + `</a>`)
}
prev = nextLinkClose + 1
}
}
output.WriteString(string(input[prev:]))
return strings.TrimSpace(output.String()), btnPairs
}
// NOTE: If this gets edited, make sure to edit the strip() method too
// TODO: this needs to return string, error to handle bad parsing
func (cv *Converter) reverse(r []rune, buttons []Button) string {
prev := 0
out := strings.Builder{}
for i := 0; i < len(r); i++ {
switch r[i] {
case '<':
closeTag := 0
for ix, c := range r[i+1:] {
if c == '>' {
closeTag = i + ix + 1
break
}
}
if closeTag == 0 {
// "no close tag"
return ""
}
closingOpen := 0
for ix, c := range r[closeTag:] {
if c == '<' {
closingOpen = closeTag + ix
break
}
}
if closingOpen == 0 {
// "no closing open"
return ""
}
closingClose := 0
for ix, c := range r[closingOpen:] {
if c == '>' {
closingClose = closingOpen + ix
break
}
}
if closingClose == 0 {
// "no closing close"
return ""
}
tag := string(r[i+1 : closeTag])
// todo: check expected closing tag
out.WriteString(html.UnescapeString(string(r[prev:i])))
if link.MatchString(tag) {
matches := link.FindStringSubmatch(tag)
out.WriteString("[" + html.UnescapeString(EscapeMarkdown(r[closeTag+1:closingOpen], []rune{'[', ']', '(', ')'})) + "](" + matches[1] + ")")
} else if tag == "b" {
out.WriteString("*" + html.UnescapeString(EscapeMarkdown(r[closeTag+1:closingOpen], []rune{'*'})) + "*")
} else if tag == "i" {
out.WriteString("_" + html.UnescapeString(EscapeMarkdown(r[closeTag+1:closingOpen], []rune{'_'})) + "_")
} else if tag == "code" {
out.WriteString("`" + html.UnescapeString(EscapeMarkdown(r[closeTag+1:closingOpen], []rune{'`'})) + "`")
} else {
// unknown tag
return ""
}
prev = closingClose + 1
i = closingClose
case '\\', '_', '*', '`', '[', ']', '(', ')': // these all need to be escaped to ensure we retain the same message
out.WriteString(html.UnescapeString(string(r[prev:i])))
out.WriteRune('\\')
out.WriteRune(r[i])
prev = i + 1
}
}
out.WriteString(html.UnescapeString(string(r[prev:])))
for _, btn := range buttons {
out.WriteString("\n[" + btn.Name + "](buttonurl://" + html.UnescapeString(btn.Content))
if btn.SameLine {
out.WriteString(":same")
}
out.WriteString(")")
}
return out.String()
}
func (cv *Converter) stripHTML(r []rune) string {
prev := 0
out := strings.Builder{}
for i := 0; i < len(r); i++ {
switch r[i] {
case '<':
closeTag := 0
for ix, c := range r[i+1:] {
if c == '>' {
closeTag = i + ix + 1
break
}
}
if closeTag == 0 {
// "no close tag"
return ""
}
closingOpen := 0
for ix, c := range r[closeTag:] {
if c == '<' {
closingOpen = closeTag + ix
break
}
}
if closingOpen == 0 {
// "no closing open"
return ""
}
closingClose := 0
for ix, c := range r[closingOpen:] {
if c == '>' {
closingClose = closingOpen + ix
break
}
}
if closingClose == 0 {
// "no closing close"
return ""
}
out.WriteString(html.UnescapeString(string(r[prev:i])))
out.WriteString(html.UnescapeString(string(r[closeTag+1 : closingOpen])))
prev = closingClose + 1
i = closingClose
}
}
out.WriteString(html.UnescapeString(string(r[prev:])))
return out.String()
}
func EscapeMarkdown(r []rune, toEscape []rune) string {
out := strings.Builder{}
for i, x := range r {
if contains(x, toEscape) {
if i == 0 || i == len(r)-1 || validEnd(i, r) || validStart(i, r) {
out.WriteRune('\\')
}
}
out.WriteRune(x)
}
return out.String()
}