@manuth/woltlab-compiler
Version:
A compiler for generating WoltLab-Package `.tar` Archives and other WoltLab-Package Components
62 lines • 1.78 kB
JavaScript
import { WoltLabXMLCompiler } from "./WoltLabXMLCompiler.js";
/**
* Provides the functionality to compile `.xml`-files which contain `ejs`-strings.
*
* @template T
* The type of the item which can be compiled by this compiler.
*/
export class WoltLabEJSFileCompiler extends WoltLabXMLCompiler {
/**
* Initializes a new instance of the {@link WoltLabEJSFileCompiler `WoltLabEJSFileCompiler<T>`} class.
*
* @param item
* The item to compile.
*/
constructor(item) {
super(item);
}
/**
* Gets the delimiter of the EJS-strings inside the document.
*/
get Delimiter() {
return "%";
}
/**
* Gets the pattern to match against the document.
*/
get Pattern() {
return new RegExp(`<${this.Delimiter}.*?${this.Delimiter}>`, "g");
}
/**
* @inheritdoc
*/
get Document() {
let document = super.Document;
this.FixEJSTags(document);
return document;
}
/**
* Fixes the ejs-tags inside the node.
*
* @param node
* The node to fix.
*/
FixEJSTags(node) {
switch (node.nodeType) {
case node.TEXT_NODE:
if (this.Pattern.test(node.textContent)) {
node.parentNode.replaceChild(node.ownerDocument.createCDATASection(node.textContent), node);
}
break;
default:
if (node.hasChildNodes()) {
for (let i = 0; i < node.childNodes.length; i++) {
let child = node.childNodes.item(i);
this.FixEJSTags(child);
}
}
break;
}
}
}
//# sourceMappingURL=WoltLabEJSFileCompiler.js.map