"use strict";
const DocUtils = require("./docUtils");
const ScopeManager = require("./scopeManager");
const xmlMatcher = require("./xmlMatcher");
const Errors = require("./errors");
const Lexer = require("./lexer");
const Parser = require("./parser.js");
const render = require("./render.js");
function getFullText(content, tagsXmlArray) {
const matcher = xmlMatcher(content, tagsXmlArray);
// get only the text
const output = ((() => {
const result = [];
for (let i = 0, match; i < matcher.matches.length; i++) {
match = matcher.matches[i];
result.push(match.array[2]);
}
return result;
})());
return DocUtils.wordToUtf8(DocUtils.convertSpaces(output.join("")));
}
module.exports = class XmlTemplater {
constructor(content, options) {
this.fromJson(options);
this.load(content);
}
load(content) {
this.content = content;
if (typeof this.content !== "string") {
const err = new Errors.XTInternalError("Content must be a string");
err.properties.id = "xmltemplater_content_must_be_string";
throw err;
}
}
fromJson(options) {
this.tags = (options.tags != null) ? options.tags : {};
this.filePath = options.filePath;
this.modules = options.modules;
this.fileTypeConfig = options.fileTypeConfig;
this.scopeManager = ScopeManager.createBaseScopeManager({tags: this.tags, parser: this.parser});
Object.keys(DocUtils.defaults).map(function (key) {
const defaultValue = DocUtils.defaults[key];
this[key] = (options[key] != null) ? options[key] : defaultValue;
}, this);
}
getFullText() {
return getFullText(this.content, this.fileTypeConfig.tagsXmlTextArray);
}
/*
content is the whole content to be tagged
scope is the current scope
returns the new content of the tagged content
*/
render() {
this.xmllexed = Lexer.xmlparse(this.content, {text: this.fileTypeConfig.tagsXmlTextArray, other: this.fileTypeConfig.tagsXmlLexedArray});
this.lexed = Lexer.parse(this.xmllexed, this.delimiters);
this.parsed = Parser.parse(this.lexed, this.modules);
this.moduleparsed = Parser.postmoduleparse(this.parsed, this.modules);
this.content = render({
compiled: this.moduleparsed,
tags: this.tags,
modules: this.modules,
parser: this.parser,
nullGetter: this.nullGetter,
filePath: this.filePath,
});
return this;
}
};
|