-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
formatter_everyone.go
110 lines (91 loc) · 3.05 KB
/
formatter_everyone.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
// mautrix-discord - A Matrix-Discord puppeting bridge.
// Copyright (C) 2023 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"fmt"
"regexp"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
)
type astDiscordEveryone struct {
ast.BaseInline
onlyHere bool
}
var _ ast.Node = (*astDiscordEveryone)(nil)
var astKindDiscordEveryone = ast.NewNodeKind("DiscordEveryone")
func (n *astDiscordEveryone) Dump(source []byte, level int) {
ast.DumpHelper(n, source, level, nil, nil)
}
func (n *astDiscordEveryone) Kind() ast.NodeKind {
return astKindDiscordEveryone
}
func (n *astDiscordEveryone) String() string {
if n.onlyHere {
return "@here"
}
return "@everyone"
}
type discordEveryoneParser struct{}
var discordEveryoneRegex = regexp.MustCompile(`@(everyone|here)`)
var defaultDiscordEveryoneParser = &discordEveryoneParser{}
func (s *discordEveryoneParser) Trigger() []byte {
return []byte{'@'}
}
func (s *discordEveryoneParser) Parse(parent ast.Node, block text.Reader, pc parser.Context) ast.Node {
line, _ := block.PeekLine()
match := discordEveryoneRegex.FindSubmatch(line)
if match == nil {
return nil
}
block.Advance(len(match[0]))
return &astDiscordEveryone{
onlyHere: string(match[1]) == "here",
}
}
func (s *discordEveryoneParser) CloseBlock(parent ast.Node, pc parser.Context) {
// nothing to do
}
type discordEveryoneHTMLRenderer struct{}
func (r *discordEveryoneHTMLRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
reg.Register(astKindDiscordEveryone, r.renderDiscordEveryone)
}
func (r *discordEveryoneHTMLRenderer) renderDiscordEveryone(w util.BufWriter, source []byte, n ast.Node, entering bool) (status ast.WalkStatus, err error) {
status = ast.WalkContinue
if !entering {
return
}
mention, _ := n.(*astDiscordEveryone)
class := "everyone"
if mention != nil && mention.onlyHere {
class = "here"
}
_, _ = fmt.Fprintf(w, `<span class="discord-mention-%s">@room</span>`, class)
return
}
type discordEveryone struct{}
var ExtDiscordEveryone = &discordEveryone{}
func (e *discordEveryone) Extend(m goldmark.Markdown) {
m.Parser().AddOptions(parser.WithInlineParsers(
util.Prioritized(defaultDiscordEveryoneParser, 600),
))
m.Renderer().AddOptions(renderer.WithNodeRenderers(
util.Prioritized(&discordEveryoneHTMLRenderer{}, 600),
))
}