-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathTemplate.helpers.go
153 lines (130 loc) · 3.79 KB
/
Template.helpers.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
package docxplate
import (
"bytes"
"encoding/xml"
"log"
"strings"
)
// Convert given bytes to struct of xml nodes
func (t *Template) bytesToXMLStruct(buf []byte) *xmlNode {
// Do not strip <w: entiraly, but keep reference as w-t
// So any string without w: would stay same, but all w- will be replaced again
buf = bytes.ReplaceAll(buf, []byte("<w:"), []byte("<w-"))
buf = bytes.ReplaceAll(buf, []byte("</w:"), []byte("</w-"))
buf = bytes.ReplaceAll(buf, []byte("<v:"), []byte("<v-"))
buf = bytes.ReplaceAll(buf, []byte("</v:"), []byte("</v-"))
xdocNode := &xmlNode{}
if err := xml.Unmarshal(buf, xdocNode); err != nil {
log.Printf("fileToXMLStruct: %v", err)
}
xdocNode.FixNamespaceDuplication()
// Assign parent nodes to all nodes
xdocNode.Walk(func(xnode *xmlNode) {
xnode.FixNamespaceDuplication()
if xnode.Tag() == "w-body" {
xnode.parent = xdocNode
}
if xnode.childFirst != nil {
xnode.childFirst.iterate(func(node *xmlNode) bool {
node.parent = xnode
return false
})
}
})
// log.Printf("%s", structToXMLBytes(n))
return xdocNode
}
// Convert given file (from template.Files) to struct of xml nodes
func (t *Template) fileToXMLStruct(fname string) *xmlNode {
f, ok := t.files[fname]
if !ok {
return nil
}
fr, _ := f.Open()
buf := readerBytes(fr)
return t.bytesToXMLStruct(buf)
}
// wrapper for simple param replace func
func (t *Template) replaceTextParam(xnode *xmlNode, param *Param) {
xnode.Content = param.replaceIn(xnode.Content)
}
// Image placeholder replace
func (t *Template) replaceImageParams(xnode *xmlNode, param *Param) {
// Sometime the placeholder is in the before or middle of the text, but node is appended in the last.
// So, we have to split the text and image into different nodes to achieve cross-display.
contentSlice := bytes.Split(xnode.Content, []byte(param.Placeholder()))
for i, content := range contentSlice {
// text node
if len(content) != 0 {
contentNode := &xmlNode{
XMLName: xml.Name{Space: "", Local: "w-t"},
Content: content,
parent: xnode.parent,
isNew: true,
}
xnode.add(contentNode)
}
// image node
if len(contentSlice)-i > 1 {
imgNode := t.bytesToXMLStruct([]byte(param.Value))
imgNode.parent = xnode.parent
xnode.add(imgNode)
}
}
// Empty the content before deleting to prevent reprocessing when params walk
xnode.Content = []byte("")
xnode.delete()
}
// Check for broken placeholders
func (t *Template) matchBrokenPlaceholder(content string, isLeft bool) bool {
stack := 0
for i := 1; i < len(content); i++ {
if content[i] == '{' && content[i-1] == '{' {
stack++
i++ // Skip next character
continue
}
if content[i] == '}' && content[i-1] == '}' {
if stack > 0 {
stack--
i++ // Skip next character
continue
}
if !isLeft {
return true // Broken right placeholder
}
}
}
return isLeft && stack > 0 // Broken left placeholder
}
// Match left part placeholder `{{`
func (t *Template) matchBrokenLeftPlaceholder(content string) bool {
return t.matchBrokenPlaceholder(content, true)
}
// UNUSED
// // Match right placeholder part `}}`
// func (t *Template) matchBrokenRightPlaceholder(content string) bool {
// return t.matchBrokenPlaceholder(content, false)
// }
// GetAttrParam - extracts and returns substrings enclosed in double curly braces "{{...}}" from the given string
func (t Template) GetAttrParam(attr string) []string {
var ret []string
var record strings.Builder
start := false
length := len(attr)
for i := 1; i < length-1; i++ {
if attr[i] == '{' && attr[i-1] == '{' {
start = true
continue
}
if start && (attr[i] == ' ' || (attr[i] == '}' && length-1 > i && attr[i+1] == '}')) {
ret = append(ret, record.String())
record.Reset()
start = false
}
if start {
record.WriteByte(attr[i])
}
}
return ret
}