-
Notifications
You must be signed in to change notification settings - Fork 341
/
docxtemplater.js
527 lines (511 loc) · 14.3 KB
/
docxtemplater.js
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
const DocUtils = require("./doc-utils.js");
DocUtils.traits = require("./traits.js");
DocUtils.moduleWrapper = require("./module-wrapper.js");
const createScope = require("./scope-manager.js");
const {
throwMultiError,
throwResolveBeforeCompile,
throwRenderInvalidTemplate,
throwRenderTwice,
} = require("./errors.js");
const logErrors = require("./error-logger.js");
const collectContentTypes = require("./collect-content-types.js");
const ctXML = "[Content_Types].xml";
const relsFile = "_rels/.rels";
const commonModule = require("./modules/common.js");
const Lexer = require("./lexer.js");
const {
defaults,
str2xml,
xml2str,
moduleWrapper,
concatArrays,
uniq,
getDuplicates,
stableSort,
} = DocUtils;
const {
XTInternalError,
throwFileTypeNotIdentified,
throwFileTypeNotHandled,
throwApiVersionError,
} = require("./errors.js");
const currentModuleApiVersion = [3, 40, 0];
function dropUnsupportedFileTypesModules(dx) {
dx.modules = dx.modules.filter((module) => {
if (module.supportedFileTypes) {
if (!Array.isArray(module.supportedFileTypes)) {
throw new Error(
"The supportedFileTypes field of the module must be an array"
);
}
const isSupportedModule =
module.supportedFileTypes.indexOf(dx.fileType) !== -1;
if (!isSupportedModule) {
module.on("detached");
}
return isSupportedModule;
}
return true;
});
}
const Docxtemplater = class Docxtemplater {
constructor(zip, { modules = [], ...options } = {}) {
if (!Array.isArray(modules)) {
throw new Error(
"The modules argument of docxtemplater's constructor must be an array"
);
}
this.targets = [];
this.rendered = false;
this.scopeManagers = {};
this.compiled = {};
this.modules = [commonModule()];
this.setOptions(options);
modules.forEach((module) => {
this.attachModule(module);
});
if (arguments.length > 0) {
if (!zip || !zip.files || typeof zip.file !== "function") {
throw new Error(
"The first argument of docxtemplater's constructor must be a valid zip file (jszip v2 or pizzip v3)"
);
}
this.loadZip(zip);
this.compile();
this.v4Constructor = true;
}
}
verifyApiVersion(neededVersion) {
neededVersion = neededVersion.split(".").map(function (i) {
return parseInt(i, 10);
});
if (neededVersion.length !== 3) {
throwApiVersionError("neededVersion is not a valid version", {
neededVersion,
explanation: "the neededVersion must be an array of length 3",
});
}
if (neededVersion[0] !== currentModuleApiVersion[0]) {
throwApiVersionError(
"The major api version do not match, you probably have to update docxtemplater with npm install --save docxtemplater",
{
neededVersion,
currentModuleApiVersion,
explanation: `moduleAPIVersionMismatch : needed=${neededVersion.join(
"."
)}, current=${currentModuleApiVersion.join(".")}`,
}
);
}
if (neededVersion[1] > currentModuleApiVersion[1]) {
throwApiVersionError(
"The minor api version is not uptodate, you probably have to update docxtemplater with npm install --save docxtemplater",
{
neededVersion,
currentModuleApiVersion,
explanation: `moduleAPIVersionMismatch : needed=${neededVersion.join(
"."
)}, current=${currentModuleApiVersion.join(".")}`,
}
);
}
if (
neededVersion[1] === currentModuleApiVersion[1] &&
neededVersion[2] > currentModuleApiVersion[2]
) {
throwApiVersionError(
"The patch api version is not uptodate, you probably have to update docxtemplater with npm install --save docxtemplater",
{
neededVersion,
currentModuleApiVersion,
explanation: `moduleAPIVersionMismatch : needed=${neededVersion.join(
"."
)}, current=${currentModuleApiVersion.join(".")}`,
}
);
}
return true;
}
setModules(obj) {
this.modules.forEach((module) => {
module.set(obj);
});
}
sendEvent(eventName) {
this.modules.forEach((module) => {
module.on(eventName);
});
}
attachModule(module) {
if (this.v4Constructor) {
throw new XTInternalError(
"attachModule() should not be called manually when using the v4 constructor"
);
}
const moduleType = typeof module;
if (moduleType === "function") {
throw new XTInternalError(
"Cannot attach a class/function as a module. Most probably you forgot to instantiate the module by using `new` on the module."
);
}
if (!module || moduleType !== "object") {
throw new XTInternalError("Cannot attachModule with a falsy value");
}
if (module.requiredAPIVersion) {
this.verifyApiVersion(module.requiredAPIVersion);
}
if (module.attached === true) {
if (typeof module.clone === "function") {
module = module.clone();
} else {
throw new Error(
`Cannot attach a module that was already attached : "${module.name}". The most likely cause is that you are instantiating the module at the root level, and using it for multiple instances of Docxtemplater`
);
}
}
module.attached = true;
const wrappedModule = moduleWrapper(module);
this.modules.push(wrappedModule);
wrappedModule.on("attached");
if (this.fileType) {
dropUnsupportedFileTypesModules(this);
}
return this;
}
setOptions(options) {
if (this.v4Constructor) {
throw new Error(
"setOptions() should not be called manually when using the v4 constructor"
);
}
if (!options) {
throw new Error(
"setOptions should be called with an object as first parameter"
);
}
this.options = {};
Object.keys(defaults).forEach((key) => {
const defaultValue = defaults[key];
this.options[key] =
options[key] != null ? options[key] : this[key] || defaultValue;
this[key] = this.options[key];
});
this.delimiters.start = DocUtils.utf8ToWord(this.delimiters.start);
this.delimiters.end = DocUtils.utf8ToWord(this.delimiters.end);
return this;
}
loadZip(zip) {
if (this.v4Constructor) {
throw new Error(
"loadZip() should not be called manually when using the v4 constructor"
);
}
if (zip.loadAsync) {
throw new XTInternalError(
"Docxtemplater doesn't handle JSZip version >=3, please use pizzip"
);
}
this.zip = zip;
this.updateFileTypeConfig();
this.modules = concatArrays([
this.fileTypeConfig.baseModules.map(function (moduleFunction) {
return moduleFunction();
}),
this.modules,
]);
dropUnsupportedFileTypesModules(this);
return this;
}
precompileFile(fileName) {
const currentFile = this.createTemplateClass(fileName);
currentFile.preparse();
this.compiled[fileName] = currentFile;
}
compileFile(fileName) {
this.compiled[fileName].parse();
}
getScopeManager(to, currentFile, tags) {
if (!this.scopeManagers[to]) {
this.scopeManagers[to] = createScope({
tags,
parser: this.parser,
cachedParsers: currentFile.cachedParsers,
});
}
return this.scopeManagers[to];
}
resolveData(data) {
const errors = [];
if (!Object.keys(this.compiled).length) {
throwResolveBeforeCompile();
}
return Promise.resolve(data).then((data) => {
this.setData(data);
this.setModules({
data: this.data,
Lexer,
});
this.mapper = this.modules.reduce(function (value, module) {
return module.getRenderedMap(value);
}, {});
return Promise.all(
Object.keys(this.mapper).map((to) => {
const { from, data } = this.mapper[to];
return Promise.resolve(data).then((data) => {
const currentFile = this.compiled[from];
currentFile.filePath = to;
currentFile.scopeManager = this.getScopeManager(
to,
currentFile,
data
);
return currentFile.resolveTags(data).then(
function (result) {
currentFile.scopeManager.finishedResolving = true;
return result;
},
function (errs) {
Array.prototype.push.apply(errors, errs);
}
);
});
})
).then((resolved) => {
if (errors.length !== 0) {
if (this.options.errorLogging) {
logErrors(errors, this.options.errorLogging);
}
throwMultiError(errors);
}
return concatArrays(resolved);
});
});
}
reorderModules() {
this.modules = stableSort(
this.modules,
(m1, m2) => (m2.priority || 0) - (m1.priority || 0)
);
}
throwIfDuplicateModules() {
const duplicates = getDuplicates(this.modules.map(({ name }) => name));
if (duplicates.length > 0) {
throw new XTInternalError(`Detected duplicate module "${duplicates[0]}"`);
}
}
compile() {
this.updateFileTypeConfig();
this.throwIfDuplicateModules();
this.reorderModules();
if (Object.keys(this.compiled).length) {
return this;
}
this.options = this.modules.reduce((options, module) => {
return module.optionsTransformer(options, this);
}, this.options);
this.options.xmlFileNames = uniq(this.options.xmlFileNames);
this.xmlDocuments = this.options.xmlFileNames.reduce(
(xmlDocuments, fileName) => {
const content = this.zip.files[fileName].asText();
xmlDocuments[fileName] = str2xml(content);
return xmlDocuments;
},
{}
);
this.setModules({
zip: this.zip,
xmlDocuments: this.xmlDocuments,
});
this.getTemplatedFiles();
// Loop inside all templatedFiles (ie xml files with content).
// Sometimes they don't exist (footer.xml for example)
this.templatedFiles.forEach((fileName) => {
if (this.zip.files[fileName] != null) {
this.precompileFile(fileName);
}
});
this.templatedFiles.forEach((fileName) => {
if (this.zip.files[fileName] != null) {
this.compileFile(fileName);
}
});
this.setModules({ compiled: this.compiled });
verifyErrors(this);
return this;
}
getRelsTypes() {
const rootRels = this.zip.files[relsFile];
const rootRelsXml = rootRels ? str2xml(rootRels.asText()) : null;
const rootRelationships = rootRelsXml
? rootRelsXml.getElementsByTagName("Relationship")
: [];
const relsTypes = {};
for (let i = 0, len = rootRelationships.length; i < len; i++) {
const r = rootRelationships[i];
relsTypes[r.getAttribute("Target")] = r.getAttribute("Type");
}
return relsTypes;
}
getContentTypes() {
const contentTypes = this.zip.files[ctXML];
const contentTypeXml = contentTypes ? str2xml(contentTypes.asText()) : null;
const overrides = contentTypeXml
? contentTypeXml.getElementsByTagName("Override")
: null;
const defaults = contentTypeXml
? contentTypeXml.getElementsByTagName("Default")
: null;
return { overrides, defaults, contentTypes, contentTypeXml };
}
updateFileTypeConfig() {
let fileType;
if (this.zip.files.mimetype) {
fileType = "odt";
}
this.relsTypes = this.getRelsTypes();
const { overrides, defaults, contentTypes, contentTypeXml } =
this.getContentTypes();
if (contentTypeXml) {
this.filesContentTypes = collectContentTypes(
overrides,
defaults,
this.zip
);
this.invertedContentTypes = DocUtils.invertMap(this.filesContentTypes);
this.setModules({
contentTypes: this.contentTypes,
invertedContentTypes: this.invertedContentTypes,
});
}
this.modules.forEach((module) => {
fileType =
module.getFileType({
zip: this.zip,
contentTypes,
contentTypeXml,
overrides,
defaults,
doc: this,
}) || fileType;
});
if (fileType === "odt") {
throwFileTypeNotHandled(fileType);
}
if (!fileType) {
throwFileTypeNotIdentified(this.zip);
}
this.fileType = fileType;
dropUnsupportedFileTypesModules(this);
this.fileTypeConfig =
this.options.fileTypeConfig ||
this.fileTypeConfig ||
Docxtemplater.FileTypeConfig[this.fileType]();
return this;
}
renderAsync(data) {
return this.resolveData(data).then(() => {
return this.render();
});
}
render(data) {
if (this.rendered) {
throwRenderTwice();
}
this.rendered = true;
this.compile();
if (this.errors.length > 0) {
throwRenderInvalidTemplate();
}
if (data) {
this.setData(data);
}
this.setModules({
data: this.data,
Lexer,
});
this.mapper =
this.mapper ||
this.modules.reduce(function (value, module) {
return module.getRenderedMap(value);
}, {});
Object.keys(this.mapper).forEach((to) => {
const { from, data } = this.mapper[to];
const currentFile = this.compiled[from];
currentFile.scopeManager = this.getScopeManager(to, currentFile, data);
currentFile.render(to);
this.zip.file(to, currentFile.content, { createFolders: true });
});
verifyErrors(this);
this.sendEvent("syncing-zip");
this.syncZip();
// The synced-zip event is used in the subtemplate module for example
this.sendEvent("synced-zip");
return this;
}
syncZip() {
Object.keys(this.xmlDocuments).forEach((fileName) => {
this.zip.remove(fileName);
const content = xml2str(this.xmlDocuments[fileName]);
return this.zip.file(fileName, content, { createFolders: true });
});
}
setData(data) {
this.data = data;
return this;
}
getZip() {
return this.zip;
}
createTemplateClass(path) {
const content = this.zip.files[path].asText();
return this.createTemplateClassFromContent(content, path);
}
createTemplateClassFromContent(content, filePath) {
const xmltOptions = {
filePath,
contentType: this.filesContentTypes[filePath],
relsType: this.relsTypes[filePath],
};
Object.keys(defaults)
.concat(["filesContentTypes", "fileTypeConfig", "fileType", "modules"])
.forEach((key) => {
xmltOptions[key] = this[key];
});
return new Docxtemplater.XmlTemplater(content, xmltOptions);
}
getFullText(path) {
return this.createTemplateClass(
path || this.fileTypeConfig.textPath(this)
).getFullText();
}
getTemplatedFiles() {
this.templatedFiles = this.fileTypeConfig.getTemplatedFiles(this.zip);
this.targets.forEach((target) => {
this.templatedFiles.push(target);
});
this.templatedFiles = uniq(this.templatedFiles);
return this.templatedFiles;
}
};
function verifyErrors(doc) {
const compiled = doc.compiled;
doc.errors = concatArrays(
Object.keys(compiled).map((name) => {
return compiled[name].allErrors;
})
);
if (doc.errors.length !== 0) {
if (doc.options.errorLogging) {
logErrors(doc.errors, doc.options.errorLogging);
}
throwMultiError(doc.errors);
}
}
Docxtemplater.DocUtils = DocUtils;
Docxtemplater.Errors = require("./errors.js");
Docxtemplater.XmlTemplater = require("./xml-templater.js");
Docxtemplater.FileTypeConfig = require("./file-type-config.js");
Docxtemplater.XmlMatcher = require("./xml-matcher.js");
module.exports = Docxtemplater;
module.exports.default = Docxtemplater;