12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- const selfClosingTags = require("./util/selfClosingTags");
-
- module.exports = class Compiler {
- constructor(tree, context) {
- this.tree = tree;
- this.context = context;
- this.pos = 0;
- this.result = "";
- }
-
- compile() {
- this.tree.forEach(node => {
- switch (node.constructor.name) {
- case "Application":
- this.result += this.application(node);
- break;
- case "Number":
- case "String":
- this.result += node.value;
- break;
- }
- });
-
- return this.result;
- }
-
- application(node) {
- let result = `<${node.functionName.name}`;
-
- node.args
- .filter(arg => arg.constructor.name === "Attribute")
- .forEach(arg => {
- result += ` ${arg.name}`;
-
- let compiler = new Compiler([arg.value], this.context);
- let attrValue = compiler.compile();
-
- if (attrValue) {
- result += `="${attrValue}"`;
- }
- });
-
- result += ">";
-
- node.args
- .filter(arg => arg.constructor.name !== "Attribute")
- .forEach(arg => {
- let compiler = new Compiler([arg], this.context);
- result += compiler.compile();
- });
-
- if (!selfClosingTags.includes(node.functionName.name)) {
- result += `</${node.functionName.name}>`;
- }
-
- return result;
- }
- };
|