-
Notifications
You must be signed in to change notification settings - Fork 11
/
parse_mode.go
204 lines (157 loc) · 4.33 KB
/
parse_mode.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
package tg
import (
"encoding"
"fmt"
"html"
"regexp"
"strings"
)
type ParseMode interface {
encoding.TextMarshaler
fmt.Stringer
// Change separator for next calls
Sep(v string) ParseMode
// Text joins the given strings with new line seprator
Text(v ...string) string
// Line joins the given strings with space seprator
Line(v ...string) string
// Bold
Bold(v ...string) string
// Italic
Italic(v ...string) string
// Underline
Underline(v ...string) string
// Deleted (Striketrough)
Strike(v ...string) string
// Spoiler
Spoiler(v ...string) string
// Link
Link(title, url string) string
// Code
Code(v ...string) string
// Preformated text
Pre(v ...string) string
// Escape
Escape(v string) string
}
func regexpReplacer(re *regexp.Regexp, repl string) func(string) string {
return func(s string) string {
return re.ReplaceAllString(s, repl)
}
}
var (
// HTML is ParseMode that uses HTML tags
HTML ParseMode = parseMode{
name: "HTML",
separator: " ",
bold: parseModeTag{"<b>", "</b>"},
italic: parseModeTag{"<i>", "</i>"},
underline: parseModeTag{"<u>", "</u>"},
strike: parseModeTag{"<s>", "</s>"},
spoiler: parseModeTag{"<tg-spoiler>", "</tg-spoiler>"},
code: parseModeTag{"<code>", "</code>"},
pre: parseModeTag{"<pre>", "</pre>"},
linkTemplate: `<a href="{url}">{title}</a>`,
escape: html.EscapeString,
}
// MD is ParseMode that uses Markdown tags.
// Warning: this is legacy mode, use MarkdownV2 (MD2) instead.
MD ParseMode = parseMode{
name: "Markdown",
separator: " ",
bold: parseModeTag{"*", "*"},
italic: parseModeTag{"_", "_"},
code: parseModeTag{"`", "`"},
pre: parseModeTag{"```", "```"},
linkTemplate: `[{title}]({url})`,
escape: regexpReplacer(regexp.MustCompile(`([_*\x60\[])`), `\$1`),
}
// MD2 is ParseMode that uses MarkdownV2 tags.
MD2 ParseMode = parseMode{
name: "MarkdownV2",
separator: " ",
bold: parseModeTag{"*", "*"},
italic: parseModeTag{"_", "_"},
underline: parseModeTag{"__", "__"},
strike: parseModeTag{"~", "~"},
spoiler: parseModeTag{"||", "||"},
code: parseModeTag{"`", "`"},
pre: parseModeTag{"```", "```"},
linkTemplate: `[{title}]({url})`,
escape: regexpReplacer(regexp.MustCompile(`([_*\[\]()~\x60>#\+\-=|{}.!])`), `\$1`),
}
)
type parseMode struct {
separator string
name string
bold parseModeTag
italic parseModeTag
underline parseModeTag
strike parseModeTag
spoiler parseModeTag
code parseModeTag
pre parseModeTag
linkTemplate string
escape func(string) string
}
type parseModeTag struct {
start string
end string
}
func (pmt parseModeTag) wrap(content string) string {
return pmt.start + content + pmt.end
}
func (pm parseMode) Text(v ...string) string {
return strings.Join(v, "\n")
}
func (pm parseMode) Line(v ...string) string {
return strings.Join(v, " ")
}
func (pm parseMode) MarshalText() ([]byte, error) {
return []byte(pm.String()), nil
}
func (pm parseMode) String() string {
return pm.name
}
func (pm parseMode) Sep(sep string) ParseMode {
pm.separator = sep
return pm
}
// Bold
func (pm parseMode) Bold(v ...string) string {
return pm.bold.wrap(strings.Join(v, pm.separator))
}
// Italic
func (pm parseMode) Italic(v ...string) string {
return pm.italic.wrap(strings.Join(v, pm.separator))
}
// Underline. Warning: this is not supported by Markdown, use MarkdownV2.
func (pm parseMode) Underline(v ...string) string {
return pm.underline.wrap(strings.Join(v, pm.separator))
}
// Strike. Warning: this is not supported by Markdown, use MarkdownV2.
func (pm parseMode) Strike(v ...string) string {
return pm.strike.wrap(strings.Join(v, pm.separator))
}
// Spoiler. Warning: this is not supported by Markdown, use MarkdownV2.
func (pm parseMode) Spoiler(v ...string) string {
return pm.spoiler.wrap(strings.Join(v, pm.separator))
}
// Link
func (pm parseMode) Link(title string, url string) string {
return strings.NewReplacer(
"{title}", title,
"{url}", url,
).Replace(pm.linkTemplate)
}
// Code
func (pm parseMode) Code(v ...string) string {
return pm.code.wrap(strings.Join(v, pm.separator))
}
// Preformated text
func (pm parseMode) Pre(v ...string) string {
return pm.pre.wrap(strings.Join(v, pm.separator))
}
func (pm parseMode) Escape(v string) string {
return pm.escape(v)
}