-
Notifications
You must be signed in to change notification settings - Fork 15
/
Template.go
320 lines (262 loc) · 7.29 KB
/
Template.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
package docxplate
import (
"archive/zip"
"bytes"
"context"
"fmt"
"io"
"log"
"os"
"path"
"reflect"
"regexp"
"strings"
)
const mainDocFname = "word/document.xml"
// filename allowed to check/modify for params
// which have keyword in it
var modFileNamesLike = []string{
"word/footer",
mainDocFname,
"word/header",
}
var t *Template
// Template ..
type Template struct {
path string
// file *os.File
// zipw *zip.Writer // zip writer
zipr *zip.Reader // zip reader
// save all zip files here so we can build it again
files map[string]*zip.File
// content type document file
documentContentTypes *zip.File
// document relations
documentRels map[string]*zip.File
// only added files (converted to []byte) save here
added map[string][]byte
// only modified files (converted to []byte) save here
modified map[string][]byte
// hold all parsed params:values here
params ParamList
}
// OpenTemplate - docpath local file
func OpenTemplate(docpath string) (*Template, error) {
var err error
docBytes, err := os.ReadFile(docpath) // #nosec G304 - allowed filename variable here
if err != nil {
return nil, err
}
t, err := OpenTemplateWithBytes(docBytes)
if err != nil {
return nil, err
}
t.path = docpath
return t, nil
}
// OpenTemplateWithBytes - template from bytes
// Credits to @dreamph for implementing this function
func OpenTemplateWithBytes(docBytes []byte) (*Template, error) {
var err error
// Init doc template
t = &Template{
files: map[string]*zip.File{},
documentRels: map[string]*zip.File{},
added: map[string][]byte{},
modified: map[string][]byte{},
}
// Unzip
if t.zipr, err = zip.NewReader(bytes.NewReader(docBytes), int64(len(docBytes))); err != nil {
return nil, err
}
// Get main document
for _, f := range t.zipr.File {
t.files[f.Name] = f
if f.Name == "[Content_Types].xml" {
t.documentContentTypes = f
continue
}
if path.Ext(f.Name) == ".rels" {
t.documentRels[f.Name] = f
continue
}
}
if t.files[mainDocFname] == nil {
return nil, fmt.Errorf("mandatory [ %s ] not found", mainDocFname)
}
return t, nil
}
// OpenTemplateWithURL .. docpath is remote url
func OpenTemplateWithURL(docurl string) (tpl *Template, err error) {
docpath, err := DefaultDownloader.DownloadFile(context.Background(), docurl)
if err != nil {
return nil, err
}
defer func() {
if err := os.Remove(docpath); err != nil {
log.Printf("open url: remove: %s", err)
}
}()
tpl, err = OpenTemplate(docpath)
if err != nil {
return nil, err
}
return
}
// Expand some placeholders to enable row replacer replace them
// Note: Currently only struct type support image replacement
// Users: []User{ User{Name:AAA}, User{Name:BBB} }
// {{Users.Name}} -->
// {{Users.1.Name}}
// {{Users.2.Name}}
// Params - replace template placeholders with params
// "Hello {{ Name }}!"" --> "Hello World!""
func (t *Template) Params(v any) {
// t.params = collectParams("", v)
switch val := v.(type) {
case map[string]any:
t.params = mapToParams(val)
case string:
t.params = JSONToParams([]byte(val))
case []byte:
t.params = JSONToParams(val)
default:
if reflect.ValueOf(v).Kind() == reflect.Struct {
t.params = StructToParams(val)
} else {
// any other type try to convert
t.params = AnyToParams(val)
}
}
for _, f := range t.files {
for _, keyword := range modFileNamesLike {
if !strings.Contains(f.Name, keyword) {
continue
}
xnode := t.fileToXMLStruct(f.Name)
// Enhance some markup (removed when building XML in the end)
// so easier to find some element
t.enhanceMarkup(xnode)
// While formating docx sometimes same style node is split to
// multiple same style nodes and different content
// Merge them so placeholders are in the same node
t.fixBrokenPlaceholders(xnode)
// Complex placeholders with more depth needs to be expanded
// for correct replace
t.expandPlaceholders(xnode)
// Replace params
t.replaceSingleParams(xnode, false)
// Collect placeholders with trigger but unset in `t.params`
// Placeholders with trigger `:empty` must be triggered
// otherwise they are left
t.triggerMissingParams(xnode)
// After all done with placeholders, modify contents
// - new lines to docx new lines
t.enhanceContent(xnode)
// Save []bytes
t.modified[f.Name] = structToXMLBytes(xnode)
}
}
}
// Bytes - create docx archive but return only bytes of it
// do not save it anywhere
func (t *Template) Bytes() ([]byte, error) {
var err error
bufw := new(bytes.Buffer)
zipw := zip.NewWriter(bufw)
// Loop existing files to build docx archive again
for _, f := range t.files {
// Read contents of single file inside zip
var fr io.ReadCloser
if fr, err = f.Open(); err != nil {
log.Printf("Error reading [ %s ] from archive", f.Name)
continue
}
fbuf := new(bytes.Buffer)
if _, err := fbuf.ReadFrom(fr); err != nil {
log.Printf("[%s] read file: %s", f.Name, err)
}
if err := fr.Close(); err != nil {
log.Printf("[%s] file close: %s", f.Name, err)
}
// Write contents as single file inside zip
var fw io.Writer
if fw, err = zipw.Create(f.Name); err != nil {
log.Printf("Error writing [ %s ] to archive", f.Name)
continue
}
// Move/Write struct-saved file to docx archive file back
if buf, isModified := t.modified[f.Name]; isModified {
if _, err := fw.Write(buf); err != nil {
log.Printf("[%s] write error: %s", f.Name, err)
}
continue
}
if _, err := fw.Write(fbuf.Bytes()); err != nil {
log.Printf("[%s] write error: %s", f.Name, err)
}
}
// Loop new added files to build docx archive
for fName, buf := range t.added {
var fw io.Writer
if fw, err = zipw.Create(fName); err != nil {
log.Printf("Error writing [ %s ] to archive", fName)
continue
}
_, _ = fw.Write(buf)
}
zipErr := zipw.Close()
return bufw.Bytes(), zipErr
}
// ExportDocx - save new/modified docx based on template
func (t *Template) ExportDocx(path string) error {
buf, err := t.Bytes()
if err != nil {
return err
}
err = os.WriteFile(path, buf, 0640) // #nosec G306
return err
}
// Placeholders - get list of used params placeholders in template
// If you already replaced params with values then you will not get all placeholders.
// Or use it after replace and see how many placeholders left.
func (t *Template) Placeholders() []string {
var arr []string
plaintext := t.Plaintext()
re := regexp.MustCompile(ParamPattern)
arr = re.FindAllString(plaintext, -1)
return arr
}
// Plaintext - return as plaintext
func (t *Template) Plaintext() string {
if len(t.params) == 0 {
// if params not set yet we init process with empty params
// and mark content as changed so we can return plaintext with placeholders
// not replaced yet
t.Params(nil)
}
plaintext := ""
// for fpath, f := range t.files {
// for _, keyword := range modFileNamesLike {
// if strings.Contains(f.Name, keyword) {
// log.Printf("%-30s %v", fpath, f.Name)
// fmt.Printf("=============== %v", t.modified[f.Name])
// }
// }
// }
// header and footer must be printed in plaintext
for _, f := range t.modified {
xnode := t.bytesToXMLStruct(f)
xnode.Walk(func(n *xmlNode) {
if n.Tag() != "w-p" {
return
}
s := string(n.AllContents())
plaintext += s
if s != "" {
plaintext += "\n"
}
})
}
return plaintext
}