This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
84 lines (74 loc) · 2.89 KB
/
parse.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
package main
import (
"bufio"
"bytes"
"io"
"regexp"
)
var (
boldItalicReg = regexp.MustCompile(`\*\*\*(.*?)\*\*\*`)
boldReg = regexp.MustCompile(`\*\*(.*?)\*\*`)
italicReg = regexp.MustCompile(`\*(.*?)\*`)
strikeReg = regexp.MustCompile(`\~\~(.*?)\~\~`)
underscoreReg = regexp.MustCompile(`__(.*?)__`)
anchorReg = regexp.MustCompile(`\[(.*?)\]\((.*?)\)[^\)]`)
escapeReg = regexp.MustCompile(`^\>(\s|)`)
blockquoteReg = regexp.MustCompile(`\>\;(.*?)$`)
backtipReg = regexp.MustCompile("`(.*?)`")
h1Reg = regexp.MustCompile(`^#(\s|)(.*?)$`)
h2Reg = regexp.MustCompile(`^##(\s|)(.*?)$`)
h3Reg = regexp.MustCompile(`^###(\s|)(.*?)$`)
h4Reg = regexp.MustCompile(`^####(\s|)(.*?)$`)
h5Reg = regexp.MustCompile(`^#####(\s|)(.*?)$`)
h6Reg = regexp.MustCompile(`^######(\s|)(.*?)$`)
)
//Md2Html : convert Markdown to Html
func Md2Html(input io.Reader) string {
buf := bytes.NewBuffer(nil)
scanner := bufio.NewScanner(input)
for scanner.Scan() {
line := bytes.TrimSpace(scanner.Bytes())
if len(line) == 0 {
buf.WriteByte('\n')
continue
}
// wrap bold and italic text in "<b>" and "<i>" elements
line = boldItalicReg.ReplaceAll(line, []byte(`<b><i>$1</i></b>`))
line = boldReg.ReplaceAll(line, []byte(`<b>$1</b>`))
line = italicReg.ReplaceAll(line, []byte(`<i>$1</i>`))
// wrap strikethrough text in "<s>" tags
line = strikeReg.ReplaceAll(line, []byte(`<s>$1</s>`))
// wrap underscored text in "<u>" tags
line = underscoreReg.ReplaceAll(line, []byte(`<u>$1</u>`))
// convert links to anchor tags
line = anchorReg.ReplaceAll(line, []byte(`<a href="$2">$1</a>`))
// escape and wrap blockquotes in "<blockquote>" tags
line = escapeReg.ReplaceAll(line, []byte(`>`))
line = blockquoteReg.ReplaceAll(line, []byte(`<blockquote>$1</blockquote>`))
// wrap the content of backticks inside of "<code>" tags
line = backtipReg.ReplaceAll(line, []byte(`<code>$1</code>`))
// convert headings
if line[0] == '#' {
// remove ending #
line = bytes.TrimRight(line," #")
count := bytes.Count(line, []byte(`#`))
switch count {
case 1:
line = h1Reg.ReplaceAll(line, []byte(`<h1>$2</h1>`))
case 2:
line = h2Reg.ReplaceAll(line, []byte(`<h2>$2</h2>`))
case 3:
line = h3Reg.ReplaceAll(line, []byte(`<h3>$2</h3>`))
case 4:
line = h4Reg.ReplaceAll(line, []byte(`<h4>$2</h4>`))
case 5:
line = h5Reg.ReplaceAll(line, []byte(`<h5>$2</h5>`))
case 6:
line = h6Reg.ReplaceAll(line, []byte(`<h6>$2</h6>`))
}
}
buf.Write(line)
buf.WriteByte('\n')
}
return buf.String()
}