260 lines
9.1 KiB
JavaScript
260 lines
9.1 KiB
JavaScript
"use strict";
|
|
|
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
|
|
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
|
|
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
|
|
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
var _require = require("./doc-utils.js"),
|
|
pushArray = _require.pushArray,
|
|
wordToUtf8 = _require.wordToUtf8,
|
|
convertSpaces = _require.convertSpaces;
|
|
var xmlMatcher = require("./xml-matcher.js");
|
|
var Lexer = require("./lexer.js");
|
|
var Parser = require("./parser.js");
|
|
var _render = require("./render.js");
|
|
var postrender = require("./postrender.js");
|
|
var resolve = require("./resolve.js");
|
|
var joinUncorrupt = require("./join-uncorrupt.js");
|
|
function _getFullText(content, tagsXmlArray) {
|
|
var matcher = xmlMatcher(content, tagsXmlArray);
|
|
var result = matcher.matches.map(function (match) {
|
|
return match.array[2];
|
|
});
|
|
return wordToUtf8(convertSpaces(result.join("")));
|
|
}
|
|
module.exports = /*#__PURE__*/function () {
|
|
function XmlTemplater(content, options) {
|
|
_classCallCheck(this, XmlTemplater);
|
|
this.cachedParsers = {};
|
|
this.content = content;
|
|
for (var key in options) {
|
|
this[key] = options[key];
|
|
}
|
|
this.setModules({
|
|
inspect: {
|
|
filePath: options.filePath
|
|
}
|
|
});
|
|
}
|
|
return _createClass(XmlTemplater, [{
|
|
key: "resolveTags",
|
|
value: function resolveTags(tags) {
|
|
var _this = this;
|
|
this.tags = tags;
|
|
var options = this.getOptions();
|
|
var filePath = this.filePath;
|
|
options.scopeManager = this.scopeManager;
|
|
options.resolve = resolve;
|
|
var errors = [];
|
|
return Promise.all(this.modules.map(function (module) {
|
|
return Promise.resolve(module.preResolve(options))["catch"](function (e) {
|
|
errors.push(e);
|
|
});
|
|
})).then(function () {
|
|
if (errors.length !== 0) {
|
|
throw errors;
|
|
}
|
|
return resolve(options).then(function (_ref) {
|
|
var resolved = _ref.resolved,
|
|
errors = _ref.errors;
|
|
errors = errors.map(function (error) {
|
|
var _error;
|
|
// If a string is thrown, convert it to a real Error
|
|
if (!(error instanceof Error)) {
|
|
error = new Error(error);
|
|
}
|
|
/*
|
|
* error properties might not be defined if some foreign error
|
|
* (unhandled error not thrown by docxtemplater willingly) is
|
|
* thrown.
|
|
*/
|
|
(_error = error).properties || (_error.properties = {});
|
|
error.properties.file = filePath;
|
|
return error;
|
|
});
|
|
if (errors.length !== 0) {
|
|
throw errors;
|
|
}
|
|
return Promise.all(resolved).then(function (resolved) {
|
|
options.scopeManager.root.finishedResolving = true;
|
|
options.scopeManager.resolved = resolved;
|
|
_this.setModules({
|
|
inspect: {
|
|
resolved: resolved,
|
|
filePath: filePath
|
|
}
|
|
});
|
|
return resolved;
|
|
});
|
|
})["catch"](function (error) {
|
|
_this.errorChecker(error);
|
|
throw error;
|
|
});
|
|
});
|
|
}
|
|
}, {
|
|
key: "getFullText",
|
|
value: function getFullText() {
|
|
return _getFullText(this.content, this.fileTypeConfig.tagsXmlTextArray);
|
|
}
|
|
}, {
|
|
key: "setModules",
|
|
value: function setModules(obj) {
|
|
for (var _i2 = 0, _this$modules2 = this.modules; _i2 < _this$modules2.length; _i2++) {
|
|
var _module = _this$modules2[_i2];
|
|
_module.set(obj);
|
|
}
|
|
}
|
|
}, {
|
|
key: "preparse",
|
|
value: function preparse() {
|
|
this.allErrors = [];
|
|
this.xmllexed = Lexer.xmlparse(this.content, {
|
|
text: this.fileTypeConfig.tagsXmlTextArray,
|
|
other: this.fileTypeConfig.tagsXmlLexedArray
|
|
});
|
|
this.setModules({
|
|
inspect: {
|
|
filePath: this.filePath,
|
|
xmllexed: this.xmllexed
|
|
}
|
|
});
|
|
var _Lexer$parse = Lexer.parse(this.xmllexed, this.delimiters, this.syntax, this.fileType),
|
|
lexed = _Lexer$parse.lexed,
|
|
lexerErrors = _Lexer$parse.errors;
|
|
pushArray(this.allErrors, lexerErrors);
|
|
this.lexed = lexed;
|
|
this.setModules({
|
|
inspect: {
|
|
filePath: this.filePath,
|
|
lexed: this.lexed
|
|
}
|
|
});
|
|
var options = this.getOptions();
|
|
this.lexed = Parser.preparse(this.lexed, this.modules, options);
|
|
}
|
|
}, {
|
|
key: "parse",
|
|
value: function parse() {
|
|
var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
noPostParse = _ref2.noPostParse;
|
|
this.setModules({
|
|
inspect: {
|
|
filePath: this.filePath
|
|
}
|
|
});
|
|
var options = this.getOptions();
|
|
this.parsed = Parser.parse(this.lexed, this.modules, options);
|
|
this.setModules({
|
|
inspect: {
|
|
filePath: this.filePath,
|
|
parsed: this.parsed
|
|
}
|
|
});
|
|
if (noPostParse) {
|
|
return this;
|
|
}
|
|
// In v4, we could remove this "this.postparse()" so that users have to call this manually.
|
|
return this.postparse();
|
|
}
|
|
}, {
|
|
key: "postparse",
|
|
value: function postparse() {
|
|
var options = this.getOptions();
|
|
var _Parser$postparse = Parser.postparse(this.parsed, this.modules, options),
|
|
postparsed = _Parser$postparse.postparsed,
|
|
postparsedErrors = _Parser$postparse.errors;
|
|
this.postparsed = postparsed;
|
|
this.setModules({
|
|
inspect: {
|
|
filePath: this.filePath,
|
|
postparsed: this.postparsed
|
|
}
|
|
});
|
|
pushArray(this.allErrors, postparsedErrors);
|
|
this.errorChecker(this.allErrors);
|
|
return this;
|
|
}
|
|
}, {
|
|
key: "errorChecker",
|
|
value: function errorChecker(errors) {
|
|
for (var _i4 = 0, _errors2 = errors; _i4 < _errors2.length; _i4++) {
|
|
var error = _errors2[_i4];
|
|
/*
|
|
* error properties might not be defined if some foreign
|
|
* (unhandled error not thrown by docxtemplater willingly) is
|
|
* thrown.
|
|
*/
|
|
error.properties || (error.properties = {});
|
|
error.properties.file = this.filePath;
|
|
}
|
|
for (var _i6 = 0, _this$modules4 = this.modules; _i6 < _this$modules4.length; _i6++) {
|
|
var _module2 = _this$modules4[_i6];
|
|
errors = _module2.errorsTransformer(errors);
|
|
}
|
|
}
|
|
}, {
|
|
key: "baseNullGetter",
|
|
value: function baseNullGetter(part, sm) {
|
|
var value = null;
|
|
for (var _i8 = 0, _this$modules6 = this.modules; _i8 < _this$modules6.length; _i8++) {
|
|
var _module3 = _this$modules6[_i8];
|
|
if (value != null) {
|
|
continue;
|
|
}
|
|
value = _module3.nullGetter(part, sm, this);
|
|
}
|
|
if (value != null) {
|
|
return value;
|
|
}
|
|
return this.nullGetter(part, sm);
|
|
}
|
|
}, {
|
|
key: "getOptions",
|
|
value: function getOptions() {
|
|
return {
|
|
compiled: this.postparsed,
|
|
cachedParsers: this.cachedParsers,
|
|
tags: this.tags,
|
|
modules: this.modules,
|
|
parser: this.parser,
|
|
contentType: this.contentType,
|
|
relsType: this.relsType,
|
|
baseNullGetter: this.baseNullGetter.bind(this),
|
|
filePath: this.filePath,
|
|
fileTypeConfig: this.fileTypeConfig,
|
|
fileType: this.fileType,
|
|
linebreaks: this.linebreaks,
|
|
stripInvalidXMLChars: this.stripInvalidXMLChars
|
|
};
|
|
}
|
|
}, {
|
|
key: "render",
|
|
value: function render(to) {
|
|
this.filePath = to;
|
|
var options = this.getOptions();
|
|
options.resolved = this.scopeManager.resolved;
|
|
options.scopeManager = this.scopeManager;
|
|
options.render = _render;
|
|
options.joinUncorrupt = joinUncorrupt;
|
|
var _render2 = _render(options),
|
|
errors = _render2.errors,
|
|
parts = _render2.parts;
|
|
if (errors.length > 0) {
|
|
this.allErrors = errors;
|
|
this.errorChecker(errors);
|
|
return this;
|
|
}
|
|
this.content = postrender(parts, options);
|
|
this.setModules({
|
|
inspect: {
|
|
filePath: this.filePath,
|
|
content: this.content
|
|
}
|
|
});
|
|
return this;
|
|
}
|
|
}]);
|
|
}(); |