docx-templates
Advanced tools
Comparing version 2.8.0 to 2.9.0-rc.0
@@ -200,7 +200,15 @@ 'use strict'; | ||
numImages = (0, _keys2.default)(images1).length; | ||
_context2.next = 54; | ||
return processImages(images1, 'document.xml', zip, templatePath); | ||
processImages(images1, 'document.xml', zip, templatePath); | ||
processLinks(links1, 'document.xml', zip, templatePath); | ||
processHtmls(htmls1, 'document.xml', zip, templatePath, xmlOptions); | ||
case 54: | ||
_context2.next = 56; | ||
return processLinks(links1, 'document.xml', zip, templatePath); | ||
case 56: | ||
_context2.next = 58; | ||
return processHtmls(htmls1, 'document.xml', zip, templatePath, xmlOptions); | ||
case 58: | ||
// --------------------------------------------------------- | ||
@@ -220,3 +228,3 @@ // Process all other XML files (they may contain headers, etc.) | ||
if (regex.test(filePath) && filePath !== templatePath + '/document.xml') { | ||
if (regex.test(filePath) && filePath !== templatePath + '/document.xml' && filePath.indexOf(templatePath + '/template') !== 0) { | ||
files.push(filePath); | ||
@@ -243,5 +251,5 @@ } | ||
case 61: | ||
case 64: | ||
if (!(i < files.length)) { | ||
_context2.next = 92; | ||
_context2.next = 98; | ||
break; | ||
@@ -253,17 +261,17 @@ } | ||
DEBUG && log.info('Processing ' + chalk.bold(filePath) + '...'); | ||
_context2.next = 66; | ||
_context2.next = 69; | ||
return (0, _zip.zipGetText)(zip, filePath); | ||
case 66: | ||
case 69: | ||
raw = _context2.sent; | ||
_context2.next = 69; | ||
_context2.next = 72; | ||
return (0, _xml.parseXml)(raw); | ||
case 69: | ||
case 72: | ||
js0 = _context2.sent; | ||
js = (0, _preprocessTemplate2.default)(js0, createOptions); | ||
_context2.next = 73; | ||
_context2.next = 76; | ||
return (0, _processTemplate.produceJsReport)(queryResult, js, createOptions); | ||
case 73: | ||
case 76: | ||
_ref3 = _context2.sent; | ||
@@ -286,15 +294,21 @@ report2 = _ref3.report; | ||
documentComponent = segments[segments.length - 1]; | ||
_context2.next = 91; | ||
return processImages(images2, documentComponent, zip, templatePath); | ||
processImages(images2, documentComponent, zip, templatePath); | ||
processLinks(links2, 'document.xml', zip, templatePath); | ||
processHtmls(htmls2, 'document.xml', zip, templatePath, xmlOptions); | ||
case 91: | ||
_context2.next = 93; | ||
return processLinks(links2, 'document.xml', zip, templatePath); | ||
case 89: | ||
case 93: | ||
_context2.next = 95; | ||
return processHtmls(htmls2, 'document.xml', zip, templatePath, xmlOptions); | ||
case 95: | ||
i++; | ||
_context2.next = 61; | ||
_context2.next = 64; | ||
break; | ||
case 92: | ||
case 98: | ||
if (!numImages) { | ||
_context2.next = 110; | ||
_context2.next = 116; | ||
break; | ||
@@ -305,11 +319,11 @@ } | ||
contentTypesPath = '[Content_Types].xml'; | ||
_context2.next = 97; | ||
_context2.next = 103; | ||
return (0, _zip.zipGetText)(zip, contentTypesPath); | ||
case 97: | ||
case 103: | ||
contentTypesXml = _context2.sent; | ||
_context2.next = 100; | ||
_context2.next = 106; | ||
return (0, _xml.parseXml)(contentTypesXml); | ||
case 100: | ||
case 106: | ||
contentTypes = _context2.sent; | ||
@@ -341,5 +355,5 @@ | ||
case 110: | ||
case 116: | ||
if (!replaceImages) { | ||
_context2.next = 132; | ||
_context2.next = 138; | ||
break; | ||
@@ -351,3 +365,3 @@ } | ||
if (!options.replaceImagesBase64) { | ||
_context2.next = 131; | ||
_context2.next = 137; | ||
break; | ||
@@ -360,5 +374,5 @@ } | ||
case 116: | ||
case 122: | ||
if (!(_i < imgNames.length)) { | ||
_context2.next = 129; | ||
_context2.next = 135; | ||
break; | ||
@@ -371,3 +385,3 @@ } | ||
if ((0, _zip.zipExists)(zip, '' + imgPath)) { | ||
_context2.next = 122; | ||
_context2.next = 128; | ||
break; | ||
@@ -377,24 +391,24 @@ } | ||
console.warn('Image ' + imgName + ' cannot be replaced: destination does not exist'); | ||
return _context2.abrupt('continue', 126); | ||
return _context2.abrupt('continue', 132); | ||
case 122: | ||
case 128: | ||
imgData = replaceImages[imgName]; | ||
DEBUG && log.debug('Replacing ' + imgName + ' with <base64 buffer>...'); | ||
_context2.next = 126; | ||
_context2.next = 132; | ||
return (0, _zip.zipSetBase64)(zip, imgPath, imgData); | ||
case 126: | ||
case 132: | ||
_i++; | ||
_context2.next = 116; | ||
_context2.next = 122; | ||
break; | ||
case 129: | ||
_context2.next = 132; | ||
case 135: | ||
_context2.next = 138; | ||
break; | ||
case 131: | ||
case 137: | ||
console.warn('Unsupported format (path): images can only be replaced in base64 mode'); | ||
case 132: | ||
case 138: | ||
@@ -405,10 +419,10 @@ // --------------------------------------------------------- | ||
DEBUG && log.debug('Zipping...'); | ||
_context2.next = 135; | ||
_context2.next = 141; | ||
return (0, _zip.zipSave)(zip); | ||
case 135: | ||
case 141: | ||
output = _context2.sent; | ||
return _context2.abrupt('return', output); | ||
case 137: | ||
case 143: | ||
case 'end': | ||
@@ -582,3 +596,3 @@ return _context2.stop(); | ||
if (!htmlIds.length) { | ||
_context5.next = 37; | ||
_context5.next = 24; | ||
break; | ||
@@ -596,34 +610,19 @@ } | ||
rels = _context5.sent; | ||
i = 0; | ||
case 10: | ||
if (!(i < htmlIds.length)) { | ||
_context5.next = 23; | ||
break; | ||
} | ||
for (i = 0; i < htmlIds.length; i++) { | ||
htmlId = htmlIds[i]; | ||
htmlData = htmls[htmlId]; | ||
htmlName = 'template_' + documentComponent + '_' + htmlId + '.html'; | ||
htmlId = htmlIds[i]; | ||
htmlData = htmls[htmlId]; | ||
htmlName = 'template_' + documentComponent + '_' + htmlId + '.html'; | ||
DEBUG && log.debug('Writing html ' + htmlId + ' (' + htmlName + ')...'); | ||
htmlPath = templatePath + '/' + htmlName; | ||
DEBUG && log.debug('Writing html ' + htmlId + ' (' + htmlName + ')...'); | ||
htmlPath = templatePath + '/' + htmlName; | ||
htmlFiles.push('/' + htmlPath); | ||
_context5.next = 19; | ||
return (0, _zip.zipSetText)(zip, htmlPath, htmlData); | ||
case 19: | ||
(0, _reportUtils.addChild)(rels, (0, _reportUtils.newNonTextNode)('Relationship', { | ||
Id: htmlId, | ||
Type: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/aFChunk', | ||
Target: '' + htmlName | ||
})); | ||
case 20: | ||
i++; | ||
_context5.next = 10; | ||
break; | ||
case 23: | ||
htmlFiles.push('/' + htmlPath); | ||
(0, _zip.zipSetText)(zip, htmlPath, htmlData); | ||
(0, _reportUtils.addChild)(rels, (0, _reportUtils.newNonTextNode)('Relationship', { | ||
Id: htmlId, | ||
Type: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/aFChunk', | ||
Target: '' + htmlName | ||
})); | ||
} | ||
finalRelsXml = (0, _xml.buildXml)(rels, { | ||
@@ -638,11 +637,11 @@ literalXmlDelimiter: DEFAULT_LITERAL_XML_DELIMITER | ||
contentTypesPath = '[Content_Types].xml'; | ||
_context5.next = 29; | ||
_context5.next = 16; | ||
return (0, _zip.zipGetText)(zip, contentTypesPath); | ||
case 29: | ||
case 16: | ||
contentTypesXml = _context5.sent; | ||
_context5.next = 32; | ||
_context5.next = 19; | ||
return (0, _xml.parseXml)(contentTypesXml); | ||
case 32: | ||
case 19: | ||
contentTypes = _context5.sent; | ||
@@ -671,3 +670,3 @@ | ||
case 37: | ||
case 24: | ||
case 'end': | ||
@@ -674,0 +673,0 @@ return _context5.stop(); |
@@ -6,4 +6,12 @@ 'use strict'; | ||
}); | ||
exports.zipSave = exports.zipSetBase64 = exports.zipSetBinary = exports.zipSetText = exports.zipGetText = exports.zipExists = exports.zipLoad = undefined; | ||
exports._resetCache = exports.zipSave = exports.zipSetBase64 = exports.zipSetBinary = exports.zipSetText = exports.zipGetText = exports.zipExists = exports.zipLoad = undefined; | ||
var _regenerator = require('babel-runtime/regenerator'); | ||
var _regenerator2 = _interopRequireDefault(_regenerator); | ||
var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator'); | ||
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2); | ||
var _jszip = require('jszip'); | ||
@@ -25,12 +33,12 @@ | ||
var zipGetText = function zipGetText(zip, filename) { | ||
return zip.file(filename).async('text'); | ||
return getFile(zip, filename, 'text'); | ||
}; | ||
var zipSetText = function zipSetText(zip, filename, data) { | ||
return zip.file(filename, data); | ||
return setFile(zip, filename, data); | ||
}; | ||
var zipSetBinary = function zipSetBinary(zip, filename, data) { | ||
return zip.file(filename, data, { binary: true }); | ||
return setFile(zip, filename, data, { binary: true }); | ||
}; | ||
var zipSetBase64 = function zipSetBase64(zip, filename, data) { | ||
return zip.file(filename, data, { base64: true }); | ||
return setFile(zip, filename, data, { base64: true }); | ||
}; | ||
@@ -46,2 +54,64 @@ var zipSave = function zipSave(zip) { | ||
// ========================================== | ||
// Cache outputs (so that they can be requested again) | ||
// ========================================== | ||
var cache = {}; | ||
var getFile = function () { | ||
var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(zip, filename, format) { | ||
var out; | ||
return _regenerator2.default.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
if (!(cache[filename] !== undefined)) { | ||
_context.next = 2; | ||
break; | ||
} | ||
return _context.abrupt('return', cache[filename]); | ||
case 2: | ||
out = void 0; | ||
_context.prev = 3; | ||
_context.next = 6; | ||
return zip.file(filename).async(format); | ||
case 6: | ||
out = _context.sent; | ||
_context.next = 12; | ||
break; | ||
case 9: | ||
_context.prev = 9; | ||
_context.t0 = _context['catch'](3); | ||
out = null; | ||
case 12: | ||
cache[filename] = out; | ||
return _context.abrupt('return', out); | ||
case 14: | ||
case 'end': | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee, undefined, [[3, 9]]); | ||
})); | ||
return function getFile(_x, _x2, _x3) { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}(); | ||
var setFile = function setFile(zip, filename, data, options) { | ||
cache[filename] = data; | ||
return zip.file(filename, data, options); | ||
}; | ||
var _resetCache = function _resetCache() { | ||
cache = {}; | ||
}; | ||
// ========================================== | ||
// Public API | ||
@@ -55,2 +125,3 @@ // ========================================== | ||
exports.zipSetBase64 = zipSetBase64; | ||
exports.zipSave = zipSave; | ||
exports.zipSave = zipSave; | ||
exports._resetCache = _resetCache; |
{ | ||
"name": "docx-templates", | ||
"version": "2.8.0", | ||
"version": "2.9.0-rc.0", | ||
"description": "Template-based docx report creation", | ||
@@ -5,0 +5,0 @@ "main": "lib/indexNode.js", |
@@ -73,3 +73,3 @@ # Docx-templates [![Build Status](https://travis-ci.org/guigrpa/docx-templates.svg)](https://travis-ci.org/guigrpa/docx-templates) [![Coverage Status](https://coveralls.io/repos/github/guigrpa/docx-templates/badge.svg?branch=master)](https://coveralls.io/github/guigrpa/docx-templates?branch=master) [![npm version](https://img.shields.io/npm/v/docx-templates.svg)](https://www.npmjs.com/package/docx-templates) | ||
```js | ||
const buffer = createReport({ | ||
const buffer = await createReport({ | ||
output: 'buffer', | ||
@@ -84,4 +84,4 @@ template: 'templates/myTemplate.docx', | ||
```js | ||
const template = // read from db, http etc as Buffer | ||
const buffer = createReport({ | ||
const template = // read from database, HTTP, etc. as a Buffer | ||
const buffer = await createReport({ | ||
output: 'buffer', | ||
@@ -88,0 +88,0 @@ template, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
169833
2281
1