-
Notifications
You must be signed in to change notification settings - Fork 354
/
Copy pathdocxtemplater.js
611 lines (584 loc) · 16.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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
const DocUtils = require("./doc-utils.js");
DocUtils.traits = require("./traits.js");
DocUtils.moduleWrapper = require("./module-wrapper.js");
const createScope = require("./scope-manager.js");
const Lexer = require("./lexer.js");
const commonModule = require("./modules/common.js");
function deprecatedMessage(obj, message) {
if (obj.hideDeprecations === true) {
return;
}
// eslint-disable-next-line no-console
console.warn(message);
}
function deprecatedMethod(obj, method) {
if (obj.hideDeprecations === true) {
return;
}
return deprecatedMessage(
obj,
`Deprecated method ".${method}", view upgrade guide : https://docxtemplater.com/docs/api/#upgrade-guide, stack : ${new Error().stack}`
);
}
const {
throwMultiError,
throwResolveBeforeCompile,
throwRenderInvalidTemplate,
throwRenderTwice,
XTInternalError,
throwFileTypeNotIdentified,
throwFileTypeNotHandled,
throwApiVersionError,
} = require("./errors.js");
const logErrors = require("./error-logger.js");
const collectContentTypes = require("./collect-content-types.js");
const {
getDefaults,
str2xml,
xml2str,
moduleWrapper,
concatArrays,
uniq,
getDuplicates,
stableSort,
pushArray,
} = DocUtils;
const ctXML = "[Content_Types].xml";
const relsFile = "_rels/.rels";
const currentModuleApiVersion = [3, 43, 0];
function dropUnsupportedFileTypesModules(doc) {
doc.modules = doc.modules.filter((module) => {
if (!module.supportedFileTypes) {
return true;
}
if (!Array.isArray(module.supportedFileTypes)) {
throw new Error(
"The supportedFileTypes field of the module must be an array"
);
}
const isSupportedModule = module.supportedFileTypes.includes(doc.fileType);
if (!isSupportedModule) {
module.on("detached");
}
return isSupportedModule;
});
}
function verifyErrors(doc) {
const compiled = doc.compiled;
doc.errors = concatArrays(
Object.keys(compiled).map((name) => compiled[name].allErrors)
);
if (doc.errors.length !== 0) {
if (doc.options.errorLogging) {
logErrors(doc.errors, doc.options.errorLogging);
}
throwMultiError(doc.errors);
}
}
const Docxtemplater = class Docxtemplater {
constructor(zip, { modules = [], ...options } = {}) {
this.targets = [];
this.rendered = false;
this.scopeManagers = {};
this.compiled = {};
this.modules = [commonModule()];
this.xmlDocuments = {};
if (arguments.length === 0) {
deprecatedMessage(
this,
`Deprecated docxtemplater constructor with no arguments, view upgrade guide : https://docxtemplater.com/docs/api/#upgrade-guide, stack : ${new Error().stack}`
);
this.hideDeprecations = true;
this.setOptions(options);
} else {
this.hideDeprecations = true;
this.setOptions(options);
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)"
);
}
if (!Array.isArray(modules)) {
throw new Error(
"The modules argument of docxtemplater's constructor must be an array"
);
}
for (const module of modules) {
this.attachModule(module);
}
this.loadZip(zip);
this.compile();
this.v4Constructor = true;
}
this.hideDeprecations = false;
}
verifyApiVersion(neededVersion) {
neededVersion = neededVersion.split(".").map((i) => 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) {
for (const module of this.modules) {
module.set(obj);
}
}
sendEvent(eventName) {
for (const module of this.modules) {
module.on(eventName);
}
}
attachModule(module) {
if (this.v4Constructor) {
throw new XTInternalError(
"attachModule() should not be called manually when using the v4 constructor"
);
}
deprecatedMethod(this, "attachModule");
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"
);
}
deprecatedMethod(this, "setOptions");
if (!options) {
throw new Error(
"setOptions should be called with an object as first parameter"
);
}
this.options = {};
const defaults = getDefaults();
for (const key in defaults) {
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"
);
}
deprecatedMethod(this, "loadZip");
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((moduleFunction) => moduleFunction()),
this.modules,
]);
for (const module of this.modules) {
module.zip = this.zip;
module.docxtemplater = this;
}
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) {
this.scopeManagers[to] ||= createScope({
tags,
parser: this.parser,
cachedParsers: currentFile.cachedParsers,
});
return this.scopeManagers[to];
}
resolveData(data) {
deprecatedMethod(this, "resolveData");
const errors = [];
if (!Object.keys(this.compiled).length) {
throwResolveBeforeCompile();
}
return Promise.resolve(data).then((data) => {
this.data = data;
this.setModules({
data: this.data,
Lexer,
});
this.mapper = this.modules.reduce(
(value, module) => 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(
(result) => {
currentFile.scopeManager.finishedResolving = true;
return result;
},
(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() {
/**
* Modules will be sorted according to priority.
*
* Input example:
* [
* { priority: 1, name: "FooMod" },
* { priority: -1, name: "XMod" },
* { priority: 4, name: "OtherMod" }
* ]
*
* Output example (sorted by priority in descending order):
* [
* { priority: 4, name: "OtherMod" },
* { priority: 1, name: "FooMod" },
* { priority: -1, name: "XMod" }
* ]
*/
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() {
deprecatedMethod(this, "compile");
this.updateFileTypeConfig();
this.throwIfDuplicateModules();
this.reorderModules();
if (Object.keys(this.compiled).length) {
return this;
}
this.options = this.modules.reduce(
(options, module) => module.optionsTransformer(options, this),
this.options
);
this.options.xmlFileNames = uniq(this.options.xmlFileNames);
for (const fileName of this.options.xmlFileNames) {
const content = this.zip.files[fileName].asText();
this.xmlDocuments[fileName] = str2xml(content);
}
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)
*/
for (const fileName of this.templatedFiles) {
if (this.zip.files[fileName] != null) {
this.precompileFile(fileName);
}
}
for (const fileName of this.templatedFiles) {
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 (const relation of rootRelationships) {
relsTypes[relation.getAttribute("Target")] =
relation.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,
});
}
for (const module of this.modules) {
fileType =
module.getFileType({
zip: this.zip,
contentTypes,
contentTypeXml,
overrides,
defaults,
doc: this,
}) || fileType;
}
if (fileType === "odt") {
throwFileTypeNotHandled(fileType);
}
if (!fileType) {
throwFileTypeNotIdentified(this.zip);
}
for (const module of this.modules) {
for (const contentType of module.xmlContentTypes || []) {
pushArray(
this.options.xmlFileNames,
this.invertedContentTypes[contentType] || []
);
}
}
this.fileType = fileType;
dropUnsupportedFileTypesModules(this);
this.fileTypeConfig =
this.options.fileTypeConfig ||
this.fileTypeConfig ||
Docxtemplater.FileTypeConfig[this.fileType]();
return this;
}
renderAsync(data) {
this.hideDeprecations = true;
const promise = this.resolveData(data);
this.hideDeprecations = false;
return promise.then(() => this.render());
}
render(data) {
if (this.rendered) {
throwRenderTwice();
}
this.rendered = true;
if (Object.keys(this.compiled).length === 0) {
this.compile();
}
if (this.errors.length > 0) {
throwRenderInvalidTemplate();
}
if (arguments.length > 0) {
this.data = data;
}
this.setModules({
data: this.data,
Lexer,
});
this.mapper ||= this.modules.reduce(
(value, module) => module.getRenderedMap(value),
{}
);
const output = [];
for (const to in this.mapper) {
const { from, data } = this.mapper[to];
const currentFile = this.compiled[from];
currentFile.scopeManager = this.getScopeManager(to, currentFile, data);
currentFile.render(to);
output.push([to, currentFile.content, currentFile]);
delete currentFile.content;
}
for (const outputPart of output) {
const [, content, currentFile] = outputPart;
for (const module of this.modules) {
if (module.preZip) {
const result = module.preZip(content, currentFile);
if (typeof result === "string") {
outputPart[1] = result;
}
}
}
}
for (const [to, content] of output) {
this.zip.file(to, 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() {
for (const fileName in this.xmlDocuments) {
this.zip.remove(fileName);
const content = xml2str(this.xmlDocuments[fileName]);
this.zip.file(fileName, content, { createFolders: true });
}
}
setData(data) {
deprecatedMethod(this, "setData");
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],
};
const defaults = getDefaults();
const defaultKeys = pushArray(Object.keys(defaults), [
"filesContentTypes",
"fileTypeConfig",
"fileType",
"modules",
]);
for (const key of defaultKeys) {
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);
pushArray(this.templatedFiles, this.targets);
this.templatedFiles = uniq(this.templatedFiles);
return this.templatedFiles;
}
};
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;