This repository has been archived by the owner on Nov 24, 2022. It is now read-only.
/
formatting.go
355 lines (334 loc) · 12.3 KB
/
formatting.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
package main
import (
"fmt"
"html"
"regexp"
"strings"
"github.com/bwmarrin/discordgo"
"github.com/mvdan/xurls"
)
func formatMentions(c string, m *discordgo.MessageCreate) (content string) {
content = c
for _, user := range m.Mentions {
if user.ID == ses.State.User.ID {
content = strings.NewReplacer(
"<@"+user.ID+">", "<div class='selfmention'>@"+html.EscapeString(user.Username)+"</div>",
"<@!"+user.ID+">", "<div class='selfmention'>@"+html.EscapeString(user.Username)+"</div>",
).Replace(content)
continue
}
content = strings.NewReplacer(
"<@"+user.ID+">", "<div class='mention'>@"+html.EscapeString(user.Username)+"</div>",
"<@!"+user.ID+">", "<div class='mention'>@"+html.EscapeString(user.Username)+"</div>",
).Replace(content)
}
content = strings.NewReplacer(
"@everyone", "<div class='selfmention'>@everyone</div>",
"@here", "<div class='selfmention'>@here</div>",
).Replace(content)
return
}
func formatMoreMentions(s *discordgo.Session, c string, m *discordgo.MessageCreate) (content string, err error) {
var patternChannels = regexp.MustCompile("<#\\d*>")
content = c
if !s.StateEnabled {
content = formatMentions(c, m)
return
}
channel, err := s.State.Channel(m.ChannelID)
if err != nil {
content = formatMentions(c, m)
return
}
for _, user := range m.Mentions {
nick := user.Username
member, err := s.State.Member(channel.GuildID, user.ID)
if err == nil && member.Nick != "" {
nick = member.Nick
}
if user.ID == ses.State.User.ID {
content = strings.NewReplacer(
"<@"+user.ID+">", "<div class='selfmention'>@"+html.EscapeString(user.Username)+"</div>",
"<@!"+user.ID+">", "<div class='selfmention'>@"+html.EscapeString(nick)+"</div>",
).Replace(content)
continue
}
content = strings.NewReplacer(
"<@"+user.ID+">", "<div class='mention'>@"+html.EscapeString(user.Username)+"</div>",
"<@!"+user.ID+">", "<div class='mention'>@"+html.EscapeString(nick)+"</div>",
).Replace(content)
}
member, _ := s.State.Member(currentServer, s.State.User.ID)
for _, roleID := range m.MentionRoles {
role, err := s.State.Role(channel.GuildID, roleID)
if err != nil || !role.Mentionable {
continue
}
for _, v := range member.Roles {
if v == roleID {
content = strings.Replace(content, "<@&"+role.ID+">", "<div class='selfmention'>@"+html.EscapeString(role.Name)+"</div>", -1)
continue
}
}
content = strings.Replace(content, "<@&"+role.ID+">", "<div class='mention'>@"+html.EscapeString(role.Name)+"</div>", -1)
}
content = patternChannels.ReplaceAllStringFunc(content, func(mention string) string {
channel, err := s.State.Channel(mention[5 : len(mention)-4])
if err != nil || channel.Type == discordgo.ChannelTypeGuildVoice {
return mention
}
return "<div class='mention'>#" + html.EscapeString(channel.Name) + "</div>"
})
content = strings.NewReplacer(
"@everyone", "<div class='selfmention'>@everyone</div>",
"@here", "<div class='selfmention'>@here</div>",
).Replace(content)
return
}
var underline = regexp.MustCompile("__.*__")
var bold = regexp.MustCompile("\\*\\*.*\\*\\*")
var italics = regexp.MustCompile("_.*_")
var italicsalt = regexp.MustCompile("\\*.*\\*")
var strikethrough = regexp.MustCompile("~~.*~~")
func processStyles(c string) (content string) {
content = c
var rep = underline.FindAllString(content, -1)
for _, v := range rep {
content = strings.Replace(content, v, "<u>"+v[2:len(v)-2]+"</u>", 1)
}
rep = bold.FindAllString(content, -1)
for _, v := range rep {
content = strings.Replace(content, v, "<b>"+v[2:len(v)-2]+"</b>", 1)
}
rep = italics.FindAllString(content, -1)
rep = append(rep, italicsalt.FindAllString(content, -1)...)
for _, v := range rep {
content = strings.Replace(content, v, "<i>"+v[1:len(v)-1]+"</i>", 1)
}
rep = strikethrough.FindAllString(content, -1)
for _, v := range rep {
content = strings.Replace(content, v, "<s>"+v[2:len(v)-2]+"</s>", 1)
}
rep = strings.Split(content, "\n")
for _, v := range rep {
if strings.HasPrefix(v, "> ") {
content = strings.Replace(content, v, "<div class='quoteblock'></div>"+v[4:], 1)
}
}
return
}
var cblockwithlang = regexp.MustCompile("\\x60\\x60\\x60\\w+\\n(.|\\n)+\\x60\\x60\\x60")
var cblock = regexp.MustCompile("\\x60\\x60\\x60(.|\\n)+\\x60\\x60\\x60")
var cblockinline = regexp.MustCompile("\\x60(.|\\n)+\\x60")
func processCodeblocks(c string) (content string) {
content = c
var rep = cblockwithlang.FindAllString(content, -1)
for _, v := range rep {
syntaxLang := strings.Split(v, "\n")[0][3:]
content = strings.Replace(content, v, "<pre><code class='"+syntaxLang+"'>"+strings.SplitN(v[:len(v)-3], "\n", 2)[1]+"</code></pre>", 1)
}
rep = cblock.FindAllString(content, -1)
for _, v := range rep {
content = strings.Replace(content, v, "<pre><code class='plaintext'>"+strings.TrimSuffix(strings.TrimPrefix(v[3:len(v)-3], "\n"), "\n")+"</code></pre>", 1)
}
rep = cblockinline.FindAllString(content, -1)
for _, v := range rep {
content = strings.Replace(content, v, "<pre style='display: inline;'><code class='plaintext' style='display: inline; padding: 0; border-radius: 0;'>"+v[1:len(v)-1]+"</code></pre>", -1)
}
return
}
var customemoji = regexp.MustCompile("<:.*>")
var aliasedemoji = regexp.MustCompile(":(\\w)+:")
func processNonUnicodeEmoji(c string) (content string) {
content = c
var rep = customemoji.FindAllString(content, -1)
for _, v := range rep {
emoji := "https://cdn.discordapp.com/emojis/" + strings.TrimSuffix(strings.Split(v, ":")[2], ">")
content = strings.Replace(content, v, "<img src='"+emoji+"' class='customemoji'>", 1)
}
rep = aliasedemoji.FindAllString(content, -1)
for _, v := range rep {
alias := v[1 : len(v)-1]
z := false
for _, x := range eAliases.Emojis {
for _, y := range x.Aliases {
if y == alias {
content = strings.Replace(content, v, x.Unicode, 1)
z = true
break
}
}
if z {
break
}
}
}
return
}
var processedCblock = regexp.MustCompile("<pre(.|\\n)+pre>")
func parseMarkdownAndMentions(m *discordgo.MessageCreate) (content string) {
content = processCodeblocks(html.EscapeString(m.Content))
markdownstrings := processedCblock.Split(content, -1)
for _, v := range markdownstrings {
content = strings.Replace(content, v, processStyles(v), 1)
}
urlStrings := processedCblock.Split(content, -1)
for _, v := range urlStrings {
rep := xurls.Strict.FindAllString(v, -1)
for _, x := range rep {
content = strings.Replace(content, x, `<div class='link' onclick=openURL('`+x+`')>`+x+`</div>`, -1)
}
}
mentionstrings := processedCblock.Split(content, -1)
for _, v := range mentionstrings {
mention, err := formatMoreMentions(ses, v, m)
if err != nil {
mention = formatMentions(v, m)
}
content = strings.Replace(content, v, mention, 1)
}
emojistrings := processedCblock.Split(content, -1)
for _, v := range emojistrings {
content = strings.Replace(content, v, processNonUnicodeEmoji(v), 1)
}
return
}
func processEmbed(z *discordgo.MessageEmbed, m *discordgo.MessageCreate) (c string) {
c = `var div = document.createElement("div");
div.classList.add("embed");
div.style.borderLeft = "4px solid #` + fmt.Sprintf("%06x", z.Color) + `";
`
if z.Provider != nil {
c += `var provider = document.createElement("div");
provider.className = "provider";
provider.innerHTML = "` + html.EscapeString(z.Provider.Name) + `";
provider.setAttribute("onclick", "wv(JSON.stringify({'type': 'openURL', 'content': '` + html.EscapeString(z.Provider.URL) + `'}));");
div.appendChild(provider);
`
}
if z.Author != nil {
c += `var author = document.createElement("div");
author.className = "author";
author.innerHTML = "` + html.EscapeString(z.Author.Name) + `";
author.setAttribute("onclick", "wv(JSON.stringify({'type': 'openURL', 'content': '` + html.EscapeString(z.Author.URL) + `'}));");
div.appendChild(author);
`
}
if z.Title != "" {
c += `var title = document.createElement("div");
title.className = "title";
title.innerHTML = "` + html.EscapeString(z.Title) + `";
title.setAttribute("onclick", "wv(JSON.stringify({'type': 'openURL', 'content': '` + html.EscapeString(z.URL) + `'}));");
div.appendChild(title);
`
}
if z.Image != nil {
c += `var imageattach = document.createElement("div");
imageattach.className = "imageattachment";
var img = document.createElement("img");
img.src = "` + html.EscapeString(z.Image.URL) + `";
img.setAttribute("onclick", "wv(JSON.stringify({'type': 'openURL', 'content': '` + html.EscapeString(z.Image.URL) + `'}));");
imageattach.appendChild(img);
div.appendChild(img);
`
}
if z.Video != nil {
c += `var vid = document.createElement("iframe");
vid.src = "` + html.EscapeString(z.Video.URL) + `"
vid.setAttribute("allowfullscreen", "");
div.appendChild(vid);
`
}
if z.Video == nil && z.Image == nil && z.Thumbnail != nil && z.Description != "" {
c += `var imageattach = document.createElement("div");
imageattach.className = "imageattachment";
var img = document.createElement("img");
img.style.maxHeight = "80px";
img.style.maxWidth = "80px";
img.style.display = "inline-block";
img.src = "` + html.EscapeString(z.Thumbnail.URL) + `";
img.setAttribute("onclick", "wv(JSON.stringify({'type': 'openURL', 'content': '` + html.EscapeString(z.Thumbnail.URL) + `'}));");
imageattach.appendChild(img);
div.appendChild(img);
`
}
if z.Video == nil && z.Image == nil && z.Thumbnail != nil && z.Description == "" {
c += `var imageattach = document.createElement("div");
imageattach.className = "imageattachment";
var img = document.createElement("img");
img.src = "` + html.EscapeString(z.Thumbnail.URL) + `";
img.setAttribute("onclick", "wv(JSON.stringify({'type': 'openURL', 'content': '` + html.EscapeString(z.Thumbnail.URL) + `'}));");
imageattach.appendChild(img);
div.appendChild(img);
`
}
if z.Video == nil && z.Image == nil && z.Description != "" {
description := processCodeblocks(html.EscapeString(z.Description))
markdownstrings := processedCblock.Split(description, -1)
for _, v := range markdownstrings {
description = strings.Replace(description, v, processStyles(v), 1)
}
urlStrings := processedCblock.Split(description, -1)
for _, v := range urlStrings {
rep := xurls.Strict.FindAllString(v, -1)
for _, x := range rep {
description = strings.Replace(description, x, `<div class='link' onclick=openURL('`+x+`')>`+x+`</div>`, -1)
}
}
mentionstrings := processedCblock.Split(description, -1)
for _, v := range mentionstrings {
mention, err := formatMoreMentions(ses, v, m)
if err != nil {
mention = formatMentions(v, m)
}
description = strings.Replace(description, v, mention, -1)
}
emojistrings := processedCblock.Split(description, -1)
for _, v := range emojistrings {
description = strings.Replace(description, v, processNonUnicodeEmoji(v), 1)
}
c += `var descrip = document.createElement("div");
descrip.className = "descrip";
descrip.innerHTML = "` + strings.ReplaceAll(description, "\n", "<br />") + `";
`
if z.Thumbnail != nil {
c += `descrip.style.width = "calc(100% - 90px)";
`
}
c += `
div.appendChild(descrip);
`
}
if z.Footer != nil {
footer := processCodeblocks(html.EscapeString(z.Footer.Text))
markdownstrings := processedCblock.Split(footer, -1)
for _, v := range markdownstrings {
footer = strings.Replace(footer, v, processStyles(v), 1)
}
urlStrings := processedCblock.Split(footer, -1)
for _, v := range urlStrings {
rep := xurls.Strict.FindAllString(v, -1)
for _, x := range rep {
footer = strings.Replace(footer, x, `<div class='link' onclick=openURL('`+x+`')>`+x+`</div>`, -1)
}
}
mentionstrings := processedCblock.Split(footer, -1)
for _, v := range mentionstrings {
mention, err := formatMoreMentions(ses, v, m)
if err != nil {
mention = formatMentions(v, m)
}
footer = strings.Replace(footer, v, mention, -1)
}
emojistrings := processedCblock.Split(footer, -1)
for _, v := range emojistrings {
footer = strings.Replace(footer, v, processNonUnicodeEmoji(v), 1)
}
c += `var footer = document.createElement("div");
footer.className = "footer";
footer.innerHTML = "` + strings.ReplaceAll(footer, "\n", "<br />") + `";
div.appendChild(footer);
`
}
return
}