-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathparse_mode.go
More file actions
292 lines (232 loc) · 7.6 KB
/
parse_mode.go
File metadata and controls
292 lines (232 loc) · 7.6 KB
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
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
// Escapef escapes the format string and then applies fmt.Sprintf with the given args.
// Useful for MarkdownV2 where literal characters like . | ! need escaping,
// while %s args (e.g. from Bold, Italic) pass through unchanged.
Escapef(format string, args ...any) 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
// Mention creates an inline mention of a user by ID
Mention(name string, userID UserID) string
// CustomEmoji inserts a custom emoji by ID with fallback emoji text
CustomEmoji(emoji, emojiID string) string
// Code
Code(v ...string) string
// Preformated text
Pre(v ...string) string
// PreLanguage creates a pre-formatted code block with language specification
PreLanguage(language string, v ...string) string
// Blockquote
Blockquote(v ...string) string
// ExpandableBlockquote creates a collapsible blockquote
ExpandableBlockquote(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>"},
blockquote: parseModeTag{"<blockquote>", "</blockquote>"},
expandableBlockquote: parseModeTag{"<blockquote expandable>", "</blockquote>"},
linkTemplate: `<a href="{url}">{title}</a>`,
customEmojiTemplate: `<tg-emoji emoji-id="{id}">{emoji}</tg-emoji>`,
preLanguageTemplate: `<pre><code class="language-{lang}">{code}</code></pre>`,
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})`,
customEmojiTemplate: `{emoji}`,
preLanguageTemplate: "```{lang}\n{code}```",
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{"```", "```"},
blockquote: parseModeTag{">", ""},
expandableBlockquote: parseModeTag{">", "||"},
lineBlockquote: true,
linkTemplate: `[{title}]({url})`,
customEmojiTemplate: ``,
preLanguageTemplate: "```{lang}\n{code}```",
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
blockquote parseModeTag
expandableBlockquote parseModeTag
linkTemplate string
customEmojiTemplate string
preLanguageTemplate string
lineBlockquote bool
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) Escapef(format string, args ...any) string {
return fmt.Sprintf(pm.escape(format), args...)
}
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, url string) string {
return strings.NewReplacer(
"{title}", title,
"{url}", url,
).Replace(pm.linkTemplate)
}
// Mention creates an inline mention of a user by ID
func (pm parseMode) Mention(name string, userID UserID) string {
return pm.Link(name, fmt.Sprintf("tg://user?id=%d", userID))
}
// CustomEmoji inserts a custom emoji by ID with fallback emoji text
func (pm parseMode) CustomEmoji(emoji, emojiID string) string {
return strings.NewReplacer(
"{emoji}", emoji,
"{id}", emojiID,
).Replace(pm.customEmojiTemplate)
}
// 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))
}
// PreLanguage creates a pre-formatted code block with language specification
func (pm parseMode) PreLanguage(language string, v ...string) string {
code := strings.Join(v, pm.separator)
return strings.NewReplacer(
"{lang}", language,
"{code}", code,
).Replace(pm.preLanguageTemplate)
}
// Blockquote
func (pm parseMode) Blockquote(v ...string) string {
content := strings.Join(v, pm.separator)
if pm.lineBlockquote {
return prefixLines(content, pm.blockquote.start)
}
return pm.blockquote.wrap(content)
}
// ExpandableBlockquote creates a collapsible blockquote
func (pm parseMode) ExpandableBlockquote(v ...string) string {
content := strings.Join(v, pm.separator)
if pm.lineBlockquote {
return prefixLines(content, pm.expandableBlockquote.start) + pm.expandableBlockquote.end
}
return pm.expandableBlockquote.wrap(content)
}
func (pm parseMode) Escape(v string) string {
return pm.escape(v)
}
func prefixLines(content, prefix string) string {
lines := strings.Split(content, "\n")
for i := range lines {
lines[i] = prefix + lines[i]
}
return strings.Join(lines, "\n")
}