docxtemplater
Version:
docx and pptx generator working with templates and data (like Mustache, for Word and Powerpoint documents)
421 lines (375 loc) • 13.8 kB
JavaScript
;
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var _require = require("./utils.js"),
last = _require.last,
first = _require.first;
function XTError(message) {
this.name = "GenericError";
this.message = message;
this.stack = new Error(message).stack;
}
XTError.prototype = Error.prototype;
function XTTemplateError(message) {
this.name = "TemplateError";
this.message = message;
this.stack = new Error(message).stack;
}
XTTemplateError.prototype = new XTError();
function XTRenderingError(message) {
this.name = "RenderingError";
this.message = message;
this.stack = new Error(message).stack;
}
XTRenderingError.prototype = new XTError();
function XTScopeParserError(message) {
this.name = "ScopeParserError";
this.message = message;
this.stack = new Error(message).stack;
}
XTScopeParserError.prototype = new XTError();
function XTInternalError(message) {
this.name = "InternalError";
this.properties = {
explanation: "InternalError"
};
this.message = message;
this.stack = new Error(message).stack;
}
XTInternalError.prototype = new XTError();
function XTAPIVersionError(message) {
this.name = "APIVersionError";
this.properties = {
explanation: "APIVersionError"
};
this.message = message;
this.stack = new Error(message).stack;
}
XTAPIVersionError.prototype = new XTError();
function throwApiVersionError(msg, properties) {
var err = new XTAPIVersionError(msg);
err.properties = _objectSpread({
id: "api_version_error"
}, properties);
throw err;
}
function throwMultiError(errors) {
var err = new XTTemplateError("Multi error");
err.properties = {
errors: errors,
id: "multi_error",
explanation: "The template has multiple errors"
};
throw err;
}
function getUnopenedTagException(options) {
var err = new XTTemplateError("Unopened tag");
err.properties = {
xtag: last(options.xtag.split(" ")),
id: "unopened_tag",
context: options.xtag,
offset: options.offset,
lIndex: options.lIndex,
explanation: "The tag beginning with \"".concat(options.xtag.substr(0, 10), "\" is unopened")
};
return err;
}
function getDuplicateOpenTagException(options) {
var err = new XTTemplateError("Duplicate open tag, expected one open tag");
err.properties = {
xtag: first(options.xtag.split(" ")),
id: "duplicate_open_tag",
context: options.xtag,
offset: options.offset,
lIndex: options.lIndex,
explanation: "The tag beginning with \"".concat(options.xtag.substr(0, 10), "\" has duplicate open tags")
};
return err;
}
function getDuplicateCloseTagException(options) {
var err = new XTTemplateError("Duplicate close tag, expected one close tag");
err.properties = {
xtag: first(options.xtag.split(" ")),
id: "duplicate_close_tag",
context: options.xtag,
offset: options.offset,
lIndex: options.lIndex,
explanation: "The tag ending with \"".concat(options.xtag.substr(0, 10), "\" has duplicate close tags")
};
return err;
}
function getUnclosedTagException(options) {
var err = new XTTemplateError("Unclosed tag");
err.properties = {
xtag: first(options.xtag.split(" ")).substr(1),
id: "unclosed_tag",
context: options.xtag,
offset: options.offset,
lIndex: options.lIndex,
explanation: "The tag beginning with \"".concat(options.xtag.substr(0, 10), "\" is unclosed")
};
return err;
}
function throwXmlTagNotFound(options) {
var err = new XTTemplateError("No tag \"".concat(options.element, "\" was found at the ").concat(options.position));
var part = options.parsed[options.index];
err.properties = {
id: "no_xml_tag_found_at_".concat(options.position),
explanation: "No tag \"".concat(options.element, "\" was found at the ").concat(options.position),
offset: part.offset,
part: part,
parsed: options.parsed,
index: options.index,
element: options.element
};
throw err;
}
function getCorruptCharactersException(_ref) {
var tag = _ref.tag,
value = _ref.value,
offset = _ref.offset;
var err = new XTRenderingError("There are some XML corrupt characters");
err.properties = {
id: "invalid_xml_characters",
xtag: tag,
value: value,
offset: offset,
explanation: "There are some corrupt characters for the field ${tag}"
};
return err;
}
function throwContentMustBeString(type) {
var err = new XTInternalError("Content must be a string");
err.properties.id = "xmltemplater_content_must_be_string";
err.properties.type = type;
throw err;
}
function throwExpandNotFound(options) {
var _options$part = options.part,
value = _options$part.value,
offset = _options$part.offset,
_options$id = options.id,
id = _options$id === void 0 ? "raw_tag_outerxml_invalid" : _options$id,
_options$message = options.message,
message = _options$message === void 0 ? "Raw tag not in paragraph" : _options$message;
var part = options.part;
var _options$explanation = options.explanation,
explanation = _options$explanation === void 0 ? "The tag \"".concat(value, "\" is not inside a paragraph") : _options$explanation;
if (typeof explanation === "function") {
explanation = explanation(part);
}
var err = new XTTemplateError(message);
err.properties = {
id: id,
explanation: explanation,
rootError: options.rootError,
xtag: value,
offset: offset,
postparsed: options.postparsed,
expandTo: options.expandTo,
index: options.index
};
throw err;
}
function throwRawTagShouldBeOnlyTextInParagraph(options) {
var err = new XTTemplateError("Raw tag should be the only text in paragraph");
var tag = options.part.value;
err.properties = {
id: "raw_xml_tag_should_be_only_text_in_paragraph",
explanation: "The raw tag \"".concat(tag, "\" should be the only text in this paragraph. This means that this tag should not be surrounded by any text or spaces."),
xtag: tag,
offset: options.part.offset,
paragraphParts: options.paragraphParts
};
throw err;
}
function getUnmatchedLoopException(part) {
var location = part.location,
offset = part.offset;
var t = location === "start" ? "unclosed" : "unopened";
var T = location === "start" ? "Unclosed" : "Unopened";
var err = new XTTemplateError("".concat(T, " loop"));
var tag = part.value;
err.properties = {
id: "".concat(t, "_loop"),
explanation: "The loop with tag \"".concat(tag, "\" is ").concat(t),
xtag: tag,
offset: offset
};
return err;
}
function getUnbalancedLoopException(pair, lastPair) {
var err = new XTTemplateError("Unbalanced loop tag");
var lastL = lastPair[0].part.value;
var lastR = lastPair[1].part.value;
var l = pair[0].part.value;
var r = pair[1].part.value;
err.properties = {
id: "unbalanced_loop_tags",
explanation: "Unbalanced loop tags {#".concat(lastL, "}{/").concat(lastR, "}{#").concat(l, "}{/").concat(r, "}"),
offset: [lastPair[0].part.offset, pair[1].part.offset],
lastPair: {
left: lastPair[0].part.value,
right: lastPair[1].part.value
},
pair: {
left: pair[0].part.value,
right: pair[1].part.value
}
};
return err;
}
function getClosingTagNotMatchOpeningTag(_ref2) {
var tags = _ref2.tags;
var err = new XTTemplateError("Closing tag does not match opening tag");
err.properties = {
id: "closing_tag_does_not_match_opening_tag",
explanation: "The tag \"".concat(tags[0].value, "\" is closed by the tag \"").concat(tags[1].value, "\""),
openingtag: first(tags).value,
offset: [first(tags).offset, last(tags).offset],
closingtag: last(tags).value
};
return err;
}
function getScopeCompilationError(_ref3) {
var tag = _ref3.tag,
rootError = _ref3.rootError,
offset = _ref3.offset;
var err = new XTScopeParserError("Scope parser compilation failed");
err.properties = {
id: "scopeparser_compilation_failed",
offset: offset,
tag: tag,
explanation: "The scope parser for the tag \"".concat(tag, "\" failed to compile"),
rootError: rootError
};
return err;
}
function getScopeParserExecutionError(_ref4) {
var tag = _ref4.tag,
scope = _ref4.scope,
error = _ref4.error,
offset = _ref4.offset;
var err = new XTScopeParserError("Scope parser execution failed");
err.properties = {
id: "scopeparser_execution_failed",
explanation: "The scope parser for the tag ".concat(tag, " failed to execute"),
scope: scope,
offset: offset,
tag: tag,
rootError: error
};
return err;
}
function getLoopPositionProducesInvalidXMLError(_ref5) {
var tag = _ref5.tag,
offset = _ref5.offset;
var err = new XTTemplateError("The position of the loop tags \"".concat(tag, "\" would produce invalid XML"));
err.properties = {
tag: tag,
id: "loop_position_invalid",
explanation: "The tags \"".concat(tag, "\" are misplaced in the document, for example one of them is in a table and the other one outside the table"),
offset: offset
};
return err;
}
function throwUnimplementedTagType(part, index) {
var errorMsg = "Unimplemented tag type \"".concat(part.type, "\"");
if (part.module) {
errorMsg += " \"".concat(part.module, "\"");
}
var err = new XTTemplateError(errorMsg);
err.properties = {
part: part,
index: index,
id: "unimplemented_tag_type"
};
throw err;
}
function throwMalformedXml(part) {
var err = new XTInternalError("Malformed xml");
err.properties = {
part: part,
id: "malformed_xml"
};
throw err;
}
function throwLocationInvalid(part) {
throw new XTInternalError("Location should be one of \"start\" or \"end\" (given : ".concat(part.location, ")"));
}
function throwResolveBeforeCompile() {
var err = new XTInternalError("You must run `.compile()` before running `.resolveData()`");
err.properties = {
id: "resolve_before_compile"
};
throw err;
}
function throwRenderInvalidTemplate() {
var err = new XTInternalError("You should not call .render on a document that had compilation errors");
err.properties = {
id: "render_on_invalid_template"
};
throw err;
}
function throwFileTypeNotIdentified() {
var err = new XTInternalError("The filetype for this file could not be identified, is this file corrupted ?");
err.properties = {
id: "filetype_not_identified"
};
throw err;
}
function throwXmlInvalid(content, offset) {
var err = new XTTemplateError("An XML file has invalid xml");
err.properties = {
id: "file_has_invalid_xml",
content: content,
offset: offset,
explanation: "The docx contains invalid XML, it is most likely corrupt"
};
throw err;
}
function throwFileTypeNotHandled(fileType) {
var err = new XTInternalError("The filetype \"".concat(fileType, "\" is not handled by docxtemplater"));
err.properties = {
id: "filetype_not_handled",
explanation: "The file you are trying to generate is of type \"".concat(fileType, "\", but only docx and pptx formats are handled"),
fileType: fileType
};
throw err;
}
module.exports = {
XTError: XTError,
XTTemplateError: XTTemplateError,
XTInternalError: XTInternalError,
XTScopeParserError: XTScopeParserError,
XTAPIVersionError: XTAPIVersionError,
// Remove this alias in v4
RenderingError: XTRenderingError,
XTRenderingError: XTRenderingError,
getClosingTagNotMatchOpeningTag: getClosingTagNotMatchOpeningTag,
getLoopPositionProducesInvalidXMLError: getLoopPositionProducesInvalidXMLError,
getScopeCompilationError: getScopeCompilationError,
getScopeParserExecutionError: getScopeParserExecutionError,
getUnclosedTagException: getUnclosedTagException,
getUnopenedTagException: getUnopenedTagException,
getUnmatchedLoopException: getUnmatchedLoopException,
getDuplicateCloseTagException: getDuplicateCloseTagException,
getDuplicateOpenTagException: getDuplicateOpenTagException,
getCorruptCharactersException: getCorruptCharactersException,
getUnbalancedLoopException: getUnbalancedLoopException,
throwApiVersionError: throwApiVersionError,
throwContentMustBeString: throwContentMustBeString,
throwFileTypeNotHandled: throwFileTypeNotHandled,
throwFileTypeNotIdentified: throwFileTypeNotIdentified,
throwLocationInvalid: throwLocationInvalid,
throwMalformedXml: throwMalformedXml,
throwMultiError: throwMultiError,
throwExpandNotFound: throwExpandNotFound,
throwRawTagShouldBeOnlyTextInParagraph: throwRawTagShouldBeOnlyTextInParagraph,
throwUnimplementedTagType: throwUnimplementedTagType,
throwXmlTagNotFound: throwXmlTagNotFound,
throwXmlInvalid: throwXmlInvalid,
throwResolveBeforeCompile: throwResolveBeforeCompile,
throwRenderInvalidTemplate: throwRenderInvalidTemplate
};