all files / es6/ xmlTemplater.js

100% Statements 39/39
100% Branches 6/6
100% Functions 7/7
100% Lines 39/39
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73      57×   57× 57× 57× 668× 668×   57×   57×       299× 299×     299× 299×       299× 299× 299× 299× 299× 299× 1794× 1794×       57×               277× 277× 274× 274× 269×               267×      
"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;
	}
};