docx-templates
Advanced tools
Comparing version 4.6.3 to 4.6.4
@@ -0,1 +1,4 @@ | ||
## 4.6.4 (2021-07-02) | ||
* Fixed bug that caused IMAGE commands in the document body to be ignored when a shape or image was already present in the document's header or footer. ([#217](https://github.com/guigrpa/docx-templates/issues/217)). | ||
## 4.6.3 (2021-06-05) | ||
@@ -2,0 +5,0 @@ * Allow arrays as the result of INS commands again ([#214](https://github.com/guigrpa/docx-templates/issues/214)). Thanks @LexAckson for pointing this out. |
156
lib/main.js
@@ -38,2 +38,7 @@ "use strict"; | ||
}; | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from) { | ||
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++) | ||
to[j] = from[i]; | ||
return to; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -56,2 +61,3 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
var TEMPLATE_PATH = 'word'; | ||
var XML_FILE_REGEX = new RegExp(TEMPLATE_PATH + "\\/[^\\/]+\\.xml"); | ||
function parseTemplate(template) { | ||
@@ -99,6 +105,6 @@ return __awaiter(this, void 0, void 0, function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var template, data, queryVars, literalXmlDelimiter, createOptions, xmlOptions, _a, jsTemplate, mainDocument, zip, contentTypes, finalTemplate, queryResult, query, result, report1, images1, links1, htmls1, reportXml, numImages, numHtmls, files, images, links, htmls, i, filePath, raw, js0, js, result_1, report2, images2, links2, htmls2, xml, segments, documentComponent, ensureContentType, finalContentTypesXml, output; | ||
var template, data, queryVars, literalXmlDelimiter, createOptions, xmlOptions, _a, jsTemplate, mainDocument, zip, contentTypes, prepped_template, queryResult, query, secondary_xml_files, prepped_secondaries, _i, secondary_xml_files_1, f, raw, js0, js, highest_img_id, ctx, result, report1, images1, links1, htmls1, reportXml, numImages, numHtmls, images, links, htmls, _b, prepped_secondaries_1, _c, js, filePath, result_1, report2, images2, links2, htmls2, xml, segments, documentComponent, ensureContentType, finalContentTypesXml, output; | ||
var _this = this; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: | ||
@@ -123,20 +129,56 @@ debug_1.logger.debug('Report options:', { attach: options }); | ||
case 1: | ||
_a = _b.sent(), jsTemplate = _a.jsTemplate, mainDocument = _a.mainDocument, zip = _a.zip, contentTypes = _a.contentTypes; | ||
_a = _d.sent(), jsTemplate = _a.jsTemplate, mainDocument = _a.mainDocument, zip = _a.zip, contentTypes = _a.contentTypes; | ||
debug_1.logger.debug('Preprocessing template...'); | ||
finalTemplate = preprocessTemplate_1.default(jsTemplate, createOptions.cmdDelimiter); | ||
prepped_template = preprocessTemplate_1.default(jsTemplate, createOptions.cmdDelimiter); | ||
queryResult = null; | ||
if (!(typeof data === 'function')) return [3 /*break*/, 4]; | ||
debug_1.logger.debug('Looking for the query in the template...'); | ||
return [4 /*yield*/, processTemplate_1.extractQuery(finalTemplate, createOptions)]; | ||
return [4 /*yield*/, processTemplate_1.extractQuery(prepped_template, createOptions)]; | ||
case 2: | ||
query = _b.sent(); | ||
query = _d.sent(); | ||
debug_1.logger.debug("Query: " + (query || 'no query found')); | ||
return [4 /*yield*/, data(query, queryVars)]; | ||
case 3: | ||
queryResult = _b.sent(); | ||
queryResult = _d.sent(); | ||
return [3 /*break*/, 5]; | ||
case 4: | ||
queryResult = data; | ||
_b.label = 5; | ||
_d.label = 5; | ||
case 5: | ||
secondary_xml_files = []; | ||
zip.forEach(function (filePath) { return __awaiter(_this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
if (XML_FILE_REGEX.test(filePath) && | ||
filePath !== TEMPLATE_PATH + "/" + mainDocument && | ||
filePath.indexOf(TEMPLATE_PATH + "/template") !== 0) { | ||
secondary_xml_files.push(filePath); | ||
} | ||
return [2 /*return*/]; | ||
}); | ||
}); }); | ||
prepped_secondaries = []; | ||
_i = 0, secondary_xml_files_1 = secondary_xml_files; | ||
_d.label = 6; | ||
case 6: | ||
if (!(_i < secondary_xml_files_1.length)) return [3 /*break*/, 10]; | ||
f = secondary_xml_files_1[_i]; | ||
return [4 /*yield*/, zip_1.zipGetText(zip, f)]; | ||
case 7: | ||
raw = _d.sent(); | ||
if (raw == null) | ||
throw new errors_1.TemplateParseError(f + " could not be read"); | ||
return [4 /*yield*/, xml_1.parseXml(raw)]; | ||
case 8: | ||
js0 = _d.sent(); | ||
js = preprocessTemplate_1.default(js0, createOptions.cmdDelimiter); | ||
prepped_secondaries.push([js, f]); | ||
_d.label = 9; | ||
case 9: | ||
_i++; | ||
return [3 /*break*/, 6]; | ||
case 10: | ||
highest_img_id = Math.max.apply(Math, __spreadArray(__spreadArray([], prepped_secondaries.map(function (_a) { | ||
var s = _a[0], _ = _a[1]; | ||
return processTemplate_1.findHighestImgId(s); | ||
})), [processTemplate_1.findHighestImgId(prepped_template)])); | ||
// Process document.xml: | ||
@@ -147,5 +189,6 @@ // - Generate the report | ||
debug_1.logger.debug('Generating report...'); | ||
return [4 /*yield*/, processTemplate_1.produceJsReport(queryResult, finalTemplate, createOptions)]; | ||
case 6: | ||
result = _b.sent(); | ||
ctx = processTemplate_1.newContext(createOptions, highest_img_id); | ||
return [4 /*yield*/, processTemplate_1.produceJsReport(queryResult, prepped_template, ctx)]; | ||
case 11: | ||
result = _d.sent(); | ||
if (result.status === 'errors') { | ||
@@ -166,44 +209,21 @@ throw result.errors; | ||
return [4 /*yield*/, processImages(images1, mainDocument, zip, TEMPLATE_PATH)]; | ||
case 7: | ||
_b.sent(); | ||
case 12: | ||
_d.sent(); | ||
return [4 /*yield*/, processLinks(links1, mainDocument, zip, TEMPLATE_PATH)]; | ||
case 8: | ||
_b.sent(); | ||
case 13: | ||
_d.sent(); | ||
return [4 /*yield*/, processHtmls(htmls1, mainDocument, zip, TEMPLATE_PATH)]; | ||
case 9: | ||
_b.sent(); | ||
files = []; | ||
zip.forEach(function (filePath) { return __awaiter(_this, void 0, void 0, function () { | ||
var regex; | ||
return __generator(this, function (_a) { | ||
regex = new RegExp(TEMPLATE_PATH + "\\/[^\\/]+\\.xml"); | ||
if (regex.test(filePath) && | ||
filePath !== TEMPLATE_PATH + "/" + mainDocument && | ||
filePath.indexOf(TEMPLATE_PATH + "/template") !== 0) { | ||
files.push(filePath); | ||
} | ||
return [2 /*return*/]; | ||
}); | ||
}); }); | ||
case 14: | ||
_d.sent(); | ||
images = images1; | ||
links = links1; | ||
htmls = htmls1; | ||
i = 0; | ||
_b.label = 10; | ||
case 10: | ||
if (!(i < files.length)) return [3 /*break*/, 18]; | ||
filePath = files[i]; | ||
debug_1.logger.debug("Processing " + filePath + "..."); | ||
return [4 /*yield*/, zip_1.zipGetText(zip, filePath)]; | ||
case 11: | ||
raw = _b.sent(); | ||
if (raw == null) | ||
throw new errors_1.TemplateParseError(filePath + " could not be read"); | ||
return [4 /*yield*/, xml_1.parseXml(raw)]; | ||
case 12: | ||
js0 = _b.sent(); | ||
js = preprocessTemplate_1.default(js0, createOptions.cmdDelimiter); | ||
return [4 /*yield*/, processTemplate_1.produceJsReport(queryResult, js, createOptions)]; | ||
case 13: | ||
result_1 = _b.sent(); | ||
_b = 0, prepped_secondaries_1 = prepped_secondaries; | ||
_d.label = 15; | ||
case 15: | ||
if (!(_b < prepped_secondaries_1.length)) return [3 /*break*/, 21]; | ||
_c = prepped_secondaries_1[_b], js = _c[0], filePath = _c[1]; | ||
return [4 /*yield*/, processTemplate_1.produceJsReport(queryResult, js, ctx)]; | ||
case 16: | ||
result_1 = _d.sent(); | ||
if (result_1.status === 'errors') { | ||
@@ -223,15 +243,15 @@ throw result_1.errors; | ||
return [4 /*yield*/, processImages(images2, documentComponent, zip, TEMPLATE_PATH)]; | ||
case 14: | ||
_b.sent(); | ||
case 17: | ||
_d.sent(); | ||
return [4 /*yield*/, processLinks(links2, mainDocument, zip, TEMPLATE_PATH)]; | ||
case 15: | ||
_b.sent(); | ||
case 18: | ||
_d.sent(); | ||
return [4 /*yield*/, processHtmls(htmls2, mainDocument, zip, TEMPLATE_PATH)]; | ||
case 16: | ||
_b.sent(); | ||
_b.label = 17; | ||
case 17: | ||
i++; | ||
return [3 /*break*/, 10]; | ||
case 18: | ||
case 19: | ||
_d.sent(); | ||
_d.label = 20; | ||
case 20: | ||
_b++; | ||
return [3 /*break*/, 15]; | ||
case 21: | ||
// Process [Content_Types].xml | ||
@@ -269,4 +289,4 @@ if (numImages || numHtmls) { | ||
return [4 /*yield*/, zip_1.zipSave(zip)]; | ||
case 19: | ||
output = _b.sent(); | ||
case 22: | ||
output = _d.sent(); | ||
return [2 /*return*/, output]; | ||
@@ -514,3 +534,3 @@ } | ||
var processLinks = function (links, documentComponent, zip, templatePath) { return __awaiter(void 0, void 0, void 0, function () { | ||
var linkIds, relsPath, rels, i, linkId, url, finalRelsXml; | ||
var linkIds, relsPath, rels, _i, linkIds_1, linkId, url, finalRelsXml; | ||
return __generator(this, function (_a) { | ||
@@ -527,4 +547,4 @@ switch (_a.label) { | ||
rels = _a.sent(); | ||
for (i = 0; i < linkIds.length; i++) { | ||
linkId = linkIds[i]; | ||
for (_i = 0, linkIds_1 = linkIds; _i < linkIds_1.length; _i++) { | ||
linkId = linkIds_1[_i]; | ||
url = links[linkId].url; | ||
@@ -548,3 +568,3 @@ reportUtils_1.addChild(rels, reportUtils_1.newNonTextNode('Relationship', { | ||
var processHtmls = function (htmls, documentComponent, zip, templatePath) { return __awaiter(void 0, void 0, void 0, function () { | ||
var htmlIds, htmlFiles, relsPath, rels, i, htmlId, htmlData, htmlName, htmlPath, finalRelsXml; | ||
var htmlIds, htmlFiles, relsPath, rels, _i, htmlIds_1, htmlId, htmlData, htmlName, htmlPath, finalRelsXml; | ||
return __generator(this, function (_a) { | ||
@@ -563,4 +583,4 @@ switch (_a.label) { | ||
rels = _a.sent(); | ||
for (i = 0; i < htmlIds.length; i++) { | ||
htmlId = htmlIds[i]; | ||
for (_i = 0, htmlIds_1 = htmlIds; _i < htmlIds_1.length; _i++) { | ||
htmlId = htmlIds_1[_i]; | ||
htmlData = htmls[htmlId]; | ||
@@ -567,0 +587,0 @@ htmlName = "template_" + documentComponent + "_" + htmlId + ".html"; |
@@ -14,3 +14,3 @@ import { Node, ReportData, Context, CreateReportOptions, Images, Links, Htmls } from './types'; | ||
}; | ||
export declare function produceJsReport(data: ReportData | undefined, template: Node, options: CreateReportOptions): Promise<ReportOutput>; | ||
export declare function produceJsReport(data: ReportData | undefined, template: Node, ctx: Context): Promise<ReportOutput>; | ||
export declare function findHighestImgId(mainDoc: Node): number; | ||
@@ -17,0 +17,0 @@ export declare function walkTemplate(data: ReportData | undefined, template: Node, ctx: Context, processor: CommandProcessor): Promise<ReportOutput>; |
@@ -124,8 +124,5 @@ "use strict"; | ||
exports.extractQuery = extractQuery; | ||
function produceJsReport(data, template, options) { | ||
function produceJsReport(data, template, ctx) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var highestImgId, ctx; | ||
return __generator(this, function (_a) { | ||
highestImgId = findHighestImgId(template); | ||
ctx = newContext(options, highestImgId); | ||
return [2 /*return*/, walkTemplate(data, template, ctx, processCmd)]; | ||
@@ -132,0 +129,0 @@ }); |
{ | ||
"name": "docx-templates", | ||
"version": "4.6.3", | ||
"version": "4.6.4", | ||
"description": "Template-based docx report creation", | ||
@@ -89,2 +89,2 @@ "main": "lib/index.js", | ||
} | ||
} | ||
} |
155235
2595