-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmpl.go
217 lines (172 loc) · 5.33 KB
/
tmpl.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
package tmpl
import (
"fmt"
"regexp"
"slices"
)
type Template struct {
content string
slotMatchCache map[string][]SlotMatch
}
type ReplaceSlotOption int
const (
ReplaceSlotOptionAll ReplaceSlotOption = iota
ReplaceSlotOptionPreserveTags
)
type SlotMatch struct {
StartIndex int
EndIndex int
InnerStartIndex int
InnerEndIndex int
Identifier string
InnerContent string
OuterContent string
}
func NewTemplate(template string) *Template {
return &Template{
content: template,
slotMatchCache: make(map[string][]SlotMatch),
}
}
func (t *Template) GetContent() string {
return t.content
}
func (t *Template) GetSlotMatches() []SlotMatch {
if len(t.slotMatchCache) > 0 {
var matches []SlotMatch
for _, match := range t.slotMatchCache {
matches = append(matches, match...)
}
return matches
}
startPattern := regexp.MustCompile(`/\*<slot\s*(\w+)>\*/`)
endPattern := regexp.MustCompile(`/\*<\/\s*slot>\*/`)
tmp := t.content
var matches []SlotMatch
offset := 0
for {
startMatch := startPattern.FindStringSubmatchIndex(tmp)
if startMatch == nil {
break
}
endMatch := endPattern.FindStringSubmatchIndex(tmp)
if endMatch == nil {
break
}
match := SlotMatch{
StartIndex: startMatch[0] + offset,
EndIndex: endMatch[1] + offset,
InnerStartIndex: startMatch[1] + offset,
InnerEndIndex: endMatch[0] + offset,
Identifier: tmp[startMatch[2]:startMatch[3]],
InnerContent: tmp[startMatch[1]:endMatch[0]],
OuterContent: tmp[startMatch[0]:endMatch[1]],
}
matches = append(matches, match)
t.slotMatchCache[match.Identifier] = append(t.slotMatchCache[match.Identifier], match)
tmp = tmp[endMatch[1]:]
offset += endMatch[1]
}
return matches
}
func (t *Template) GetSlotMatchesReverseOrder() []SlotMatch {
matches := t.GetSlotMatches()
slices.SortFunc(matches, reverseCompare)
return matches
}
func (t *Template) GetSlotMatch(identifier string) (*SlotMatch, error) {
if len(t.slotMatchCache) == 0 {
t.GetSlotMatches()
}
if match, ok := t.slotMatchCache[identifier]; ok {
return &match[0], nil
}
return nil, fmt.Errorf("slot with identifier %s not found", identifier)
}
func (t *Template) GetSlotsByIdentifier(identifier string) []SlotMatch {
if len(t.slotMatchCache) == 0 {
t.GetSlotMatches()
}
if match, ok := t.slotMatchCache[identifier]; ok {
return match
}
return nil
}
// ReplaceSlot replaces the content of a slot with the given identifier.
// It returns a new Template with the slot replaced.
// e.g. content = "Hello, my name is /*<slot name>*/John Doe/*</slot>*/"
//
// ReplaceSlot("name", "Jane Doe") -> "Hello, my name is Jane Doe"
//
// default behavior is to replace the first slot found.
// options:
// - ReplaceSlotOptionAll: replace all slots with the given identifier
func (t *Template) ReplaceSlot(identifier, content string, options ...ReplaceSlotOption) (*Template, error) {
matches := t.GetSlotMatches()
tmp := t.content
if slices.Contains(options, ReplaceSlotOptionAll) {
slices.SortFunc(matches, reverseCompare)
for _, match := range matches {
if match.Identifier == identifier {
tmp = tmp[:match.StartIndex] + content + tmp[match.EndIndex:]
}
}
return NewTemplate(tmp), nil
} else {
for _, match := range matches {
if match.Identifier == identifier {
tmp = tmp[:match.StartIndex] + content + tmp[match.EndIndex:]
return NewTemplate(tmp), nil
}
}
}
return nil, fmt.Errorf("slot with identifier %s not found", identifier)
}
func (t *Template) ReplaceSlotMust(identifier, content string, options ...ReplaceSlotOption) *Template {
newTemplate, err := t.ReplaceSlot(identifier, content, options...)
if err != nil {
panic(err)
}
return newTemplate
}
func (t *Template) ReplaceSlotMap(replacementMap map[string]string, options ...ReplaceSlotOption) (*Template, error) {
matches := t.GetSlotMatches()
slices.SortFunc(matches, reverseCompare)
content := t.content
for _, match := range matches {
if replacement, ok := replacementMap[match.Identifier]; ok {
if slices.Contains(options, ReplaceSlotOptionPreserveTags) {
content = content[:match.InnerStartIndex] + replacement + content[match.InnerEndIndex:]
} else {
content = content[:match.StartIndex] + replacement + content[match.EndIndex:]
}
}
}
return NewTemplate(content), nil
}
func (t *Template) ReplaceSlotFunc(replacementFunc func(identifier, innerContent, outerContent string) string) *Template {
matches := t.GetSlotMatches()
slices.SortFunc(matches, reverseCompare)
content := t.content
for _, match := range matches {
replacement := replacementFunc(match.Identifier, match.InnerContent, match.OuterContent)
content = content[:match.StartIndex] + replacement + content[match.EndIndex:]
}
return NewTemplate(content)
}
func (t *Template) ReplaceSlotAny(replacement string, options ...ReplaceSlotOption) *Template {
matches := t.GetSlotMatches()
slices.SortFunc(matches, reverseCompare)
content := t.content
for _, match := range matches {
if slices.Contains(options, ReplaceSlotOptionPreserveTags) {
content = content[:match.InnerStartIndex] + replacement + content[match.InnerEndIndex:]
} else {
content = content[:match.StartIndex] + replacement + content[match.EndIndex:]
}
}
return NewTemplate(content)
}
func reverseCompare(i, j SlotMatch) int {
return j.StartIndex - i.StartIndex
}