-
Notifications
You must be signed in to change notification settings - Fork 0
/
descriptor.go
84 lines (67 loc) · 1.86 KB
/
descriptor.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
package template
import (
"fmt"
"strings"
"text/template"
"github.com/antchfx/xmlquery"
)
type ContentReplaceRule struct {
Value string
ElementXPath string
}
type Descriptor struct {
Attributes []struct {
Key string
}
ContentReplaceRules []ContentReplaceRule
}
type compiledDescriptor struct {
contentReplaceRules []func(svgNode *xmlquery.Node, content map[string]any) error
}
func (d Descriptor) compile() (*compiledDescriptor, error) {
var c compiledDescriptor
for _ruleIndex, _rule := range d.ContentReplaceRules {
ruleFunc, err := _rule.compile()
if err != nil {
return nil, fmt.Errorf("compile rule #%d: %w", _ruleIndex, err)
}
c.contentReplaceRules = append(c.contentReplaceRules, ruleFunc)
}
return &c, nil
}
func (rule ContentReplaceRule) compile() (func(svgNode *xmlquery.Node, content map[string]any) error, error) {
valueTemplate, err := rule.compileValueTemplate()
if err != nil {
return nil, fmt.Errorf("compile value template: %w", err)
}
ruleFunc := func(svgNode *xmlquery.Node, content map[string]any) error {
value, err := valueTemplate(content)
if err != nil {
return fmt.Errorf("get value: %w", err)
}
nodes, err := xmlquery.QueryAll(svgNode, rule.ElementXPath)
for _, node := range nodes {
replaceTextContents(node, value)
}
return nil
}
return ruleFunc, nil
}
func (rule ContentReplaceRule) compileValueTemplate() (func(content map[string]any) (string, error), error) {
valueTemplate, err := template.New("root").Parse(rule.Value)
if err != nil {
return nil, fmt.Errorf("template parse: %w", err)
}
return func(content map[string]any) (string, error) {
var buf strings.Builder
err = valueTemplate.Execute(&buf, struct {
Instance map[string]any
}{
Instance: content,
})
if err != nil {
return "", fmt.Errorf("execute value template: %w", err)
}
return buf.String(), nil
}, nil
}