docx-templates
Advanced tools
Comparing version
@@ -60,3 +60,3 @@ 'use strict'; | ||
createOptions = { | ||
cmdDelimiter: options.cmdDelimiter || DEFAULT_CMD_DELIMITER, | ||
cmdDelimiter: getCmdDelimiter(options.cmdDelimiter), | ||
literalXmlDelimiter: literalXmlDelimiter, | ||
@@ -623,4 +623,13 @@ processLineBreaks: options.processLineBreaks != null ? options.processLineBreaks : true, | ||
// ========================================== | ||
// Miscellaneous | ||
// ========================================== | ||
var getCmdDelimiter = function getCmdDelimiter(delimiter) { | ||
if (!delimiter) return [DEFAULT_CMD_DELIMITER, DEFAULT_CMD_DELIMITER]; | ||
if (typeof delimiter === 'string') return [delimiter, delimiter]; | ||
return delimiter; | ||
}; | ||
// ========================================== | ||
// Public API | ||
// ========================================== | ||
exports.default = createReport; |
@@ -19,3 +19,3 @@ 'use strict'; | ||
var idxDelimiter = 0; | ||
var placeholderCmd = delimiter + 'CMD_NODE' + delimiter; | ||
var placeholderCmd = delimiter[0] + 'CMD_NODE' + delimiter[1]; | ||
@@ -42,4 +42,7 @@ while (node != null) { | ||
// What's the current expected delimiter | ||
var currentDelimiter = fCmd ? delimiter[1] : delimiter[0]; | ||
// Matches the expected delimiter character | ||
if (c === delimiter[idxDelimiter]) { | ||
if (c === currentDelimiter[idxDelimiter]) { | ||
idxDelimiter += 1; | ||
@@ -50,3 +53,3 @@ | ||
// depending on the case | ||
if (idxDelimiter === delimiter.length) { | ||
if (idxDelimiter === currentDelimiter.length) { | ||
fCmd = !fCmd; | ||
@@ -58,3 +61,3 @@ var fNodesMatch = node === openNode; | ||
} | ||
openNode._text += delimiter; | ||
openNode._text += currentDelimiter; | ||
if (!fCmd && i < textIn.length - 1) { | ||
@@ -70,3 +73,3 @@ openNode = (0, _reportUtils.insertTextSiblingAfter)(openNode); | ||
} else if (idxDelimiter) { | ||
openNode._text += delimiter.slice(0, idxDelimiter); | ||
openNode._text += currentDelimiter.slice(0, idxDelimiter); | ||
idxDelimiter = 0; | ||
@@ -73,0 +76,0 @@ if (!fCmd) openNode = node; |
@@ -547,3 +547,7 @@ 'use strict'; | ||
case 4: | ||
segments = text.split(cmdDelimiter); | ||
segments = text.split(cmdDelimiter[0]).map(function (s) { | ||
return s.split(cmdDelimiter[1]); | ||
}).reduce(function (x, y) { | ||
return x.concat(y); | ||
}); | ||
outText = ''; | ||
@@ -559,3 +563,3 @@ idx = 0; | ||
// Include the separators in the `buffers` field (used for deleting paragraphs if appropriate) | ||
if (idx > 0) appendTextToTagBuffers(cmdDelimiter, ctx, { fCmd: true }); | ||
if (idx > 0) appendTextToTagBuffers(cmdDelimiter[0], ctx, { fCmd: true }); | ||
@@ -884,2 +888,9 @@ // Append segment either to the `ctx.cmd` buffer (to be executed), if we are in "command mode", | ||
var builtInCommands = ['QUERY', 'CMD_NODE', 'ALIAS', 'FOR', 'END-FOR', 'IF', 'END-IF', 'INS', 'EXEC', 'IMAGE', 'LINK', 'HTML']; | ||
var notBuiltIns = function notBuiltIns(cmd) { | ||
return !builtInCommands.some(function (word) { | ||
return new RegExp('^' + word).test(cmd.toUpperCase()); | ||
}); | ||
}; | ||
var getCommand = function getCommand(ctx) { | ||
@@ -897,2 +908,4 @@ var cmd = ctx.cmd; | ||
cmd = 'EXEC ' + cmd.slice(1).trim(); | ||
} else if (notBuiltIns(cmd) && /^[a-zA-Z$`'"]/.test(cmd)) { | ||
cmd = 'INS ' + cmd.trim(); | ||
} | ||
@@ -899,0 +912,0 @@ ctx.cmd = ''; |
{ | ||
"name": "docx-templates", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "Template-based docx report creation", | ||
@@ -47,5 +47,5 @@ "main": "lib/indexNode.js", | ||
"fs-extra": "^3.0.1", | ||
"jszip": "^3.1.5", | ||
"jszip": "^3.2.2", | ||
"sax": "1.2.4", | ||
"timm": "^1.6.1" | ||
"timm": "^1.6.2" | ||
}, | ||
@@ -63,5 +63,5 @@ "devDependencies": { | ||
"md5": "^2.2.1", | ||
"mockdate": "^2.0.2", | ||
"mockdate": "^2.0.5", | ||
"nyc": "10.0.0", | ||
"prettier": "^1.14.2", | ||
"prettier": "^1.18.2", | ||
"storyboard": "^3.1.4", | ||
@@ -68,0 +68,0 @@ "storyboard-listener-console": "^3.1.4", |
@@ -11,3 +11,3 @@ # Docx-templates [](https://travis-ci.org/guigrpa/docx-templates) [](https://coveralls.io/github/guigrpa/docx-templates?branch=master) [](https://www.npmjs.com/package/docx-templates) | ||
* **Execute JavaScript snippets** (`EXEC` command, or `!` for short) | ||
* **Insert the result of JavaScript snippets** in your document (`INS`, or `=` for short) | ||
* **Insert the result of JavaScript snippets** in your document (`INS`, `=` or just *nothing*) | ||
* **Embed images, hyperlinks and even HTML dynamically** (`IMAGE`, `LINK`, `HTML`). Dynamic images can be great for on-the-fly QR codes, downloading photos straight to your reports, charts… even maps! | ||
@@ -105,2 +105,3 @@ * Add **loops** with `FOR`/`END-FOR` commands, with support for table rows, nested loops, and JavaScript processing of elements (filter, sort, etc) | ||
cmdDelimiter: '+++', | ||
/* default for backwards compatibility; but even better: ['{', '}'] */ | ||
literalXmlDelimiter: '||', | ||
@@ -112,2 +113,15 @@ processLineBreaks: true, | ||
You can use different **left/right command delimiters** by passing an array to `cmdDelimiter`: | ||
```js | ||
createReport({ | ||
// ... | ||
cmdDelimiter: ['{', '}'], | ||
}) | ||
``` | ||
This allows much cleaner-looking templates! | ||
Then you can add commands and JS snippets in your template like this: `{foo}`, `{project.name}` `{QUERY ...}`, `{FOR ...}`. | ||
Check out the [Node examples folder](https://github.com/guigrpa/docx-templates/tree/master/packages/example-node). | ||
@@ -207,3 +221,3 @@ | ||
### `INS` (`=`) | ||
### `INS` (`=`, or nothing at all) | ||
@@ -235,2 +249,8 @@ Inserts the result of a given JavaScript snippet: | ||
Even shorter (and with custom `cmdDelimiter: ['{', '}']`): | ||
``` | ||
{project.name} ({project.details.year}) | ||
``` | ||
You can also access functions in the `additionalJsContext` parameter to `createReport()`, which may even return a Promise. The resolved value of the Promise will be inserted in the document. | ||
@@ -237,0 +257,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
163770
1.54%2204
1.01%437
4.8%Updated
Updated