all files / es6/ xml-templater.js

100% Statements 45/45
83.33% Branches 5/6
100% Functions 11/11
100% Lines 45/45
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 74 75 76 77 78 79 80 81 82 83 84      63× 63× 604×   63×       164× 164× 164×     164×   163×     138× 138× 138×     164× 164× 164× 164× 492×       63×     837× 4160×       138× 138× 138× 136× 136× 136× 136× 132×               132× 132× 132×               131× 131×      
"use strict";
 
const DocUtils = require("./doc-utils");
const ScopeManager = require("./scope-manager");
const xmlMatcher = require("./xml-matcher");
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);
	const result = matcher.matches.map(function (match) {
		return match.array[2];
	});
	return DocUtils.wordToUtf8(DocUtils.convertSpaces(result.join("")));
}
 
module.exports = class XmlTemplater {
	constructor(content, options) {
		this.fromJson(options);
		this.setModules({inspect: {filePath: this.filePath}});
		this.load(content);
	}
	load(content) {
		if (typeof content !== "string") {
			const err = new Errors.XTInternalError("Content must be a string");
			err.properties.id = "xmltemplater_content_must_be_string";
			throw err;
		}
		this.content = content;
	}
	setTags(tags) {
		this.tags = (tags != null) ? tags : {};
		this.scopeManager = ScopeManager.createBaseScopeManager({tags: this.tags, parser: this.parser});
		return this;
	}
	fromJson(options) {
		this.filePath = options.filePath;
		this.modules = options.modules;
		this.fileTypeConfig = options.fileTypeConfig;
		Object.keys(DocUtils.defaults).map(function (key) {
			this[key] = options[key] != null ? options[key] : DocUtils.defaults[key];
		}, this);
	}
	getFullText() {
		return getFullText(this.content, this.fileTypeConfig.tagsXmlTextArray);
	}
	setModules(obj) {
		this.modules.forEach((module) => {
			module.set(obj);
		});
	}
	parse() {
		this.xmllexed = Lexer.xmlparse(this.content, {text: this.fileTypeConfig.tagsXmlTextArray, other: this.fileTypeConfig.tagsXmlLexedArray});
		this.setModules({inspect: {xmllexed: this.xmllexed}});
		this.lexed = Lexer.parse(this.xmllexed, this.delimiters);
		this.setModules({inspect: {lexed: this.lexed}});
		this.parsed = Parser.parse(this.lexed, this.modules);
		this.setModules({inspect: {parsed: this.parsed}});
		this.postparsed = Parser.postparse(this.parsed, this.modules);
		return this;
	}
	/*
	content is the whole content to be tagged
	scope is the current scope
	returns the new content of the tagged content
	*/
	render(to) {
		this.filePath = to;
		this.setModules({inspect: {postparsed: this.postparsed}});
		this.content = render({
			compiled: this.postparsed,
			tags: this.tags,
			modules: this.modules,
			parser: this.parser,
			nullGetter: this.nullGetter,
			filePath: this.filePath,
		});
		this.setModules({inspect: {content: this.content}});
		return this;
	}
};