-
Notifications
You must be signed in to change notification settings - Fork 58
/
autolink.go
163 lines (146 loc) · 3.82 KB
/
autolink.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
package autolink
import (
"fmt"
"regexp"
)
// Autolink represents a pattern to autolink.
type Autolink struct {
Name string `json:"Name"`
Disabled bool `json:"Disabled"`
Pattern string `json:"Pattern"`
Template string `json:"Template"`
Scope []string `json:"Scope"`
WordMatch bool `json:"WordMatch"`
DisableNonWordPrefix bool `json:"DisableNonWordPrefix"`
DisableNonWordSuffix bool `json:"DisableNonWordSuffix"`
ProcessBotPosts bool `json:"ProcessBotPosts"`
template string
re *regexp.Regexp
canReplaceAll bool
}
func (l Autolink) Equals(x Autolink) bool {
if l.Disabled != x.Disabled ||
l.DisableNonWordPrefix != x.DisableNonWordPrefix ||
l.DisableNonWordSuffix != x.DisableNonWordSuffix ||
l.ProcessBotPosts != x.ProcessBotPosts ||
l.Name != x.Name ||
l.Pattern != x.Pattern ||
len(l.Scope) != len(x.Scope) ||
l.Template != x.Template ||
l.WordMatch != x.WordMatch {
return false
}
for i, scope := range l.Scope {
if scope != x.Scope[i] {
return false
}
}
return true
}
// DisplayName returns a display name for the link.
func (l Autolink) DisplayName() string {
if l.Name != "" {
return l.Name
}
return l.Pattern
}
// Compile compiles the link's regular expression
func (l *Autolink) Compile() error {
if l.Disabled || len(l.Pattern) == 0 || len(l.Template) == 0 {
return nil
}
// `\b` can be used with ReplaceAll since it does not consume characters,
// custom patterns can not and need to be processed one at a time.
canReplaceAll := false
pattern := l.Pattern
template := l.Template
if !l.DisableNonWordPrefix {
if l.WordMatch {
pattern = `\b` + pattern
canReplaceAll = true
} else {
pattern = `(?P<MattermostNonWordPrefix>(^|\s))` + pattern
template = `${MattermostNonWordPrefix}` + template
}
}
if !l.DisableNonWordSuffix {
if l.WordMatch {
pattern += `\b`
canReplaceAll = true
} else {
pattern += `(?P<MattermostNonWordSuffix>$|[\s\.\!\?\,\)])`
template += `${MattermostNonWordSuffix}`
}
}
re, err := regexp.Compile(pattern)
if err != nil {
return err
}
l.re = re
l.template = template
l.canReplaceAll = canReplaceAll
return nil
}
// Replace will subsitute the regex's with the supplied links
func (l Autolink) Replace(message string) string {
if l.re == nil {
return message
}
// Since they don't consume, `\b`s require no special handling, can just ReplaceAll
if l.canReplaceAll {
return l.re.ReplaceAllString(message, l.template)
}
// Replace one at a time
in := []byte(message)
out := []byte{}
for {
if len(in) == 0 {
break
}
submatch := l.re.FindSubmatchIndex(in)
if submatch == nil {
break
}
out = append(out, in[:submatch[0]]...)
out = l.re.Expand(out, []byte(l.template), in, submatch)
in = in[submatch[1]:]
}
out = append(out, in...)
return string(out)
}
// ToMarkdown prints a Link as a markdown list element
func (l Autolink) ToMarkdown(i int) string {
text := "- "
if i > 0 {
text += fmt.Sprintf("%v: ", i)
}
if l.Name != "" {
if l.Disabled {
text += fmt.Sprintf("~~%s~~", l.Name)
} else {
text += l.Name
}
}
if l.Disabled {
text += " **Disabled**"
}
text += "\n"
text += fmt.Sprintf(" - Pattern: `%s`\n", l.Pattern)
text += fmt.Sprintf(" - Template: `%s`\n", l.Template)
if l.DisableNonWordPrefix {
text += fmt.Sprintf(" - DisableNonWordPrefix: `%v`\n", l.DisableNonWordPrefix)
}
if l.DisableNonWordSuffix {
text += fmt.Sprintf(" - DisableNonWordSuffix: `%v`\n", l.DisableNonWordSuffix)
}
if l.ProcessBotPosts {
text += fmt.Sprintf(" - ProcessBotPosts: `%v`\n", l.ProcessBotPosts)
}
if len(l.Scope) != 0 {
text += fmt.Sprintf(" - Scope: `%v`\n", l.Scope)
}
if l.WordMatch {
text += fmt.Sprintf(" - WordMatch: `%v`\n", l.WordMatch)
}
return text
}