-
Notifications
You must be signed in to change notification settings - Fork 15
/
Param.go
328 lines (274 loc) · 7.09 KB
/
Param.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
package docxplate
import (
"bytes"
"fmt"
"regexp"
"strings"
)
// ParamPattern - regex pattern to identify params
const ParamPattern = `{{(#|)([\w\.]+?)(| .*?)(| [:a-z]+?)}}`
// ParamType ..
type ParamType int8
// Param types
const (
StringParam ParamType = iota
StructParam
SliceParam
ImageParam
VideoParam
)
// Image - Choose either path or url to set, if both are set, prioritize path.
type Image struct {
Path string
URL string
Width int // dimension:pt
Height int // dimension:pt
}
// Param ..
type Param struct {
Key string
Value string
Type ParamType
Level int
Params ParamList
parent *Param
AbsoluteKey string // Users.1.Name
CompactKey string // Users.Name
Separator string // {{Usernames SEPERATOR}}
Trigger *ParamTrigger
Formatter *ParamFormatter
RowPlaceholder string
Index int // slice data index,expandPlaceholders function needs
}
// NewParam ..
func NewParam(key any) *Param {
p := &Param{
Key: fmt.Sprintf("%v", key),
}
p.AbsoluteKey = p.Key
p.CompactKey = p.Key
return p
}
// NewParamFromRaw ..
func NewParamFromRaw(raw []byte) *Param {
// extract from raw contents
re := regexp.MustCompile(ParamPattern)
matches := re.FindAllSubmatch(raw, -1)
if matches == nil || matches[0] == nil {
return nil
}
p := NewParam(string(matches[0][2]))
p.Separator = strings.TrimSpace(string(matches[0][3]))
p.Trigger = NewParamTrigger(matches[0][4])
p.Formatter = NewFormatter(matches[0][4])
return p
}
// string placeholder replace
func (p *Param) replaceIn(buf []byte) []byte {
buf = bytes.ReplaceAll(buf, []byte(p.Placeholder()), []byte(p.Value))
buf = bytes.ReplaceAll(buf, []byte(p.PlaceholderKey()), []byte(p.Key))
return buf
}
// SetValue - any value to string
func (p *Param) SetValue(val any) {
switch v := val.(type) {
case string:
p.Value = v
default:
p.Value = fmt.Sprintf("%v", val)
}
}
// Placeholder .. {{Key}}
func (p *Param) Placeholder() string {
var formatter, trigger, params string
if p.Formatter != nil {
formatter = p.Formatter.String()
}
if p.Trigger != nil {
trigger = p.Trigger.String()
}
if p.Formatter != nil || p.Trigger != nil {
params = " " + formatter + trigger
}
return "{{" + p.AbsoluteKey + params + "}}"
}
// PlaceholderKey .. {{#Key}}
func (p *Param) PlaceholderKey() string {
var formatter, trigger, params string
if p.Formatter != nil {
formatter = p.Formatter.String()
}
if p.Trigger != nil {
trigger = p.Trigger.String()
}
if p.Formatter != nil || p.Trigger != nil {
params = " " + formatter + trigger
}
return "{{#" + p.AbsoluteKey + params + "}}"
}
// PlaceholderInline .. {{Key ,}}
func (p *Param) PlaceholderInline() string {
return "{{" + p.AbsoluteKey + " " // "{{Key " - space suffix
}
// PlaceholderKeyInline .. {{#Key ,}}
func (p *Param) PlaceholderKeyInline() string {
return "{{#" + p.AbsoluteKey + " " // "{{#Key " - space suffix
}
// PlaceholderPrefix .. {{Key
func (p *Param) PlaceholderPrefix() string {
return "{{" + p.AbsoluteKey // "{{Key"
}
// PlaceholderKeyPrefix .. {{#Key
func (p *Param) PlaceholderKeyPrefix() string {
return "{{#" + p.AbsoluteKey // "{{#Key"
}
// ToCompact - convert AbsoluteKey placeholder to ComplexKey placeholder
// {{Users.0.Name}} --> {{Users.Name}}
func (p *Param) ToCompact(placeholder string) string {
return strings.Replace(placeholder, p.AbsoluteKey, p.CompactKey, 1)
}
// Walk down
func (p *Param) Walk(fn func(*Param), level int) {
for _, p2 := range p.Params {
if p2 == nil {
continue
}
// Assign Level
p2.Level = level
// Assign parent
p2.parent = p
// Absolute key with slice indexes
p2.AbsoluteKey = p.AbsoluteKey + "." + p2.Key
if p.AbsoluteKey == "" {
p2.AbsoluteKey = p.Key + "." + p2.Key
}
// Complex key with no slice indexes
if p2.parent.Type == SliceParam {
p2.CompactKey = p.CompactKey
} else {
p2.CompactKey = p.CompactKey + "." + p2.Key
}
fn(p2)
p2.Walk(fn, level+1)
}
}
// WalkFunc - loop through
func (p *Param) WalkFunc(fn func(*Param)) {
for _, p2 := range p.Params {
if p2 == nil {
continue
}
fn(p2)
p2.WalkFunc(fn)
}
}
// Depth - how many levels param have of child nodes
// {{Users.1.Name}} --> 3
func (p *Param) Depth() int {
return strings.Count(p.AbsoluteKey, ".") + 1
}
// Try to extract trigger from raw contents specific to this param
func (p *Param) extractTriggerFrom(buf []byte) *ParamTrigger {
prefixes := []string{
p.PlaceholderInline(),
p.PlaceholderKeyInline(),
}
for _, pref := range prefixes {
bpref := []byte(pref)
if !bytes.Contains(buf, bpref) {
continue
}
// Get part where trigger is (remove plaheolder prefix)
buf := bytes.SplitN(buf, bpref, 2)[1]
// Remove placeholder suffix and only raw trigger part left
buf = bytes.SplitN(buf, []byte("}}"), 2)[0]
p.Trigger = NewParamTrigger(buf)
return p.Trigger
}
return nil
}
// Try to extract trigger from raw contents specific to this param
func (p *Param) extractFormatter(buf []byte) *ParamFormatter {
prefixes := []string{
p.PlaceholderInline(),
p.PlaceholderKeyInline(),
}
for _, pref := range prefixes {
bpref := []byte(pref)
if !bytes.Contains(buf, bpref) {
continue
}
// Get part where trigger is (remove plaheolder prefix)
buf := bytes.SplitN(buf, bpref, 2)[1]
// Remove placeholder suffix and only raw trigger part left
buf = bytes.SplitN(buf, []byte("}}"), 2)[0]
p.Formatter = NewFormatter(buf)
return p.Formatter
}
return nil
}
// RunTrigger - execute trigger
func (p *Param) RunTrigger(xnode *xmlNode) {
if p == nil || p.Trigger == nil {
return
}
if p.Trigger.On == TriggerOnEmpty && p.Value != "" {
return
}
// 1. Scope - find affected node
var ntypes = NodeSingleTypes
switch p.Trigger.Scope {
case TriggerScopeCell:
ntypes = NodeCellTypes
case TriggerScopeRow:
ntypes = NodeRowTypes
case TriggerScopeList:
ntypes = []string{"w-p"} // list items have w-p > w-pPr > w-numPr item
case TriggerScopeTable:
ntypes = []string{"w-tbl"}
case TriggerScopeSection:
ntypes = NodeSectionTypes
}
n := xnode.closestUp(ntypes)
if n == nil {
return
}
isListItem, listID := n.IsListItem()
// Whole lists: special case
isListRemove := p.Trigger.Scope == TriggerScopeList // :list
isListRemove = isListRemove || (isListItem && p.Trigger.Scope == TriggerScopeSection) // :section
if isListRemove && isListItem {
// find all list items as this
n.parent.childFirst.iterate(func(wpNode *xmlNode) bool {
isitem, listid := wpNode.IsListItem()
if !isitem || listid != listID {
return false
}
if p.Trigger.Command == TriggerCommandRemove {
wpNode.delete()
}
return false
})
return
}
// Simple cases
if p.Trigger.Command == TriggerCommandRemove {
n.delete()
return
}
if p.Trigger.Command == TriggerCommandClear {
n.Content = nil
n.Walk(func(n2 *xmlNode) {
n2.Content = nil
})
return
}
}
// String - compact debug information as string
func (p *Param) String() string {
s := fmt.Sprintf("%34s=%-20s", p.AbsoluteKey, p.Value)
s += fmt.Sprintf("\tSeparator[%s]", p.Separator)
s += fmt.Sprintf("\tFormatter[%s]", p.Formatter)
s += fmt.Sprintf("\tTrigger[%s]", p.Trigger)
return s
}