-
Notifications
You must be signed in to change notification settings - Fork 3
/
markup.go
110 lines (99 loc) · 3.2 KB
/
markup.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
package wikipedia
import (
"regexp"
"strings"
)
type RegexReplacer struct {
r *regexp.Regexp
s string
}
func (r *RegexReplacer) Replace(str string) string {
return r.r.ReplaceAllString(str, r.s)
}
func NewRegexReplacer(reg string, substitute string) *RegexReplacer {
return &RegexReplacer{
r: regexp.MustCompile(reg),
s: substitute,
}
}
func RenderMarkupToHTML(str string) string {
replacers := []*RegexReplacer{
NewRegexReplacer(`(?s){{[^{}]*}}`, ""),
NewRegexReplacer(`<ref[^>]*>([^<]*)</ref>`, ""),
NewRegexReplacer(`<ref[^>]*/>`, ""),
NewRegexReplacer(`(?s)\{\|.+\|\}`, ""),
NewRegexReplacer(`(?m)^======(.*)======`, "<br /><h6>$1</h6>"),
NewRegexReplacer(`(?m)^=====(.*)=====`, "<br /><h5>$1</h5>"),
NewRegexReplacer(`(?m)^====(.*)====`, "<br /><h4>$1</h4>"),
NewRegexReplacer(`(?m)^===(.*)===`, "<br /><h3>$1</h3>"),
NewRegexReplacer(`(?m)^==(.*)==`, "<br /><h2>$1</h2>"),
NewRegexReplacer(`(?m)^=(.*)=`, "<br /><h1>$1</h1>"),
NewRegexReplacer(`----`, "<hr />"),
NewRegexReplacer(`__FORCETOC__`, ""),
NewRegexReplacer(`__TOC__`, ""),
NewRegexReplacer(`__NOTOC__`, ""),
NewRegexReplacer(`(?m)^\*+(.*)$`, "<li>$1</li>"),
NewRegexReplacer(`(?m)^#+(.*)$`, "<li>$1</li>"),
NewRegexReplacer(`'''''(.*)'''''`, "<strong><i>$1</i></strong>"),
NewRegexReplacer(`'''(.*)'''`, "<strong>$1</strong>"),
NewRegexReplacer(`''(.*)''`, "<i>$1</i>"),
NewRegexReplacer(`\[(https?://[^\[\]]+)\s([^\[\]]+)\]`, "$2"),
NewRegexReplacer(`\[\[([^\[\]]+)\|([^\[\]]+)\]\]`, "$2"),
NewRegexReplacer(`\[\[([^\[\]]+)\|\]\]`, "$1"),
NewRegexReplacer(`\[\[([^\[\]]*)\]\]`, "$1"),
}
for _, replacer := range replacers {
for {
newstr := replacer.Replace(str)
if newstr == str {
break
}
str = newstr
}
}
str = strings.TrimSpace(str)
str = NewRegexReplacer(`\n\n`, "<br />").Replace(str)
return str
}
func RenderMarkupToText(str string) string {
replacers := []*RegexReplacer{
NewRegexReplacer(`(?s){{[^{}]*}}`, ""),
NewRegexReplacer(`<ref[^>]*>([^<]*)</ref>`, ""),
NewRegexReplacer(`<ref[^>]*/>`, ""),
NewRegexReplacer(`(?s)\{\|.+\|\}`, ""),
NewRegexReplacer(`(?m)^======(.*)======`, "\n$1"),
NewRegexReplacer(`(?m)^=====(.*)=====`, "\n$1"),
NewRegexReplacer(`(?m)^====(.*)====`, "\n$1"),
NewRegexReplacer(`(?m)^===(.*)===`, "\n$1"),
NewRegexReplacer(`(?m)^==(.*)==`, "\n$1"),
NewRegexReplacer(`(?m)^=(.*)=`, "\n$1"),
NewRegexReplacer(`----`, ""),
NewRegexReplacer(`__FORCETOC__`, ""),
NewRegexReplacer(`__TOC__`, ""),
NewRegexReplacer(`__NOTOC__`, ""),
NewRegexReplacer(`(?m)^\*+(.*)$`, "$1"),
NewRegexReplacer(`(?m)^#+(.*)$`, "$1"),
NewRegexReplacer(`'''''(.*)'''''`, "$1"),
NewRegexReplacer(`'''(.*)'''`, "$1"),
NewRegexReplacer(`''(.*)''`, "$1"),
NewRegexReplacer(`\[(https?://[^\[\]]+)\s([^\[\]]+)\]`, "$2"),
NewRegexReplacer(`\[\[([^\[\]]+)\|([^\[\]]+)\]\]`, "$2"),
NewRegexReplacer(`\[\[([^\[\]]+)\|\]\]`, "$1"),
NewRegexReplacer(`\[\[([^\[\]]*)\]\]`, "$1"),
}
for _, replacer := range replacers {
for {
newstr := replacer.Replace(str)
if newstr == str {
break
}
str = newstr
}
}
str = strings.TrimSpace(str)
return str
}
func FirstParagraph(str string) string {
str = RenderMarkupToText(str)
return strings.Split(str, "\n\n")[0]
}