UNPKG

siphon-cli

Version:

Simple bundler for web applications. 📦🔧🧡

122 lines (121 loc) • 6.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var utils_1 = require("../../../../utils"); var tab = " "; var Generator = (function () { function Generator() { } Generator.prototype.generate = function (nodes, options, spacers) { var _this = this; if (spacers === void 0) { spacers = ""; } var html = ""; if (nodes) { nodes.forEach(function (node) { var _a, _b, _c, _d, _e; if (node.type) { var attributeList_1 = ""; if (node.type !== "text") { if (options.formatFiles) html += spacers; if (node.attributes) { if (node.attributeList && node.attributeList.length > 50 && options.formatFiles) { attributeList_1 = "\n" + spacers + tab; Object.entries(node.attributes).forEach(function (entry) { if (entry[1] !== true) { attributeList_1 += " ".concat(entry[0], "=\"").concat(entry[1], "\"") + "\n" + spacers + tab; } else attributeList_1 += " ".concat(entry[0]) + "\n" + spacers + tab; }); } else { Object.entries(node.attributes).forEach(function (entry) { if (entry[1] !== true) { attributeList_1 += " ".concat(entry[0], "=\"").concat(entry[1], "\""); } else attributeList_1 += " ".concat(entry[0]); }); } } } switch (true) { case (0, utils_1.isForeignTag)(node.tagName): html += "<".concat(node.tagName).concat(attributeList_1, ">"); if (node.content) { if (options.formatFiles) { html += "\n".concat(node.content); html += spacers; } else html += node.content; } html += "</".concat(node.tagName, ">"); if (options.formatFiles) html += "\n"; break; case node.type === "text": if (options.formatFiles && node.content && node.content.length > 70) { html += "\n" + spacers; var textSlices = []; var i = 0; do { if (node.content[i + 70] === " ") { textSlices.push((_a = node.content) === null || _a === void 0 ? void 0 : _a.slice(i, i + 70)); i += 70; } else { var j = i + 70; while (node.content[j] && node.content[j] !== " ") { j++; } textSlices.push((_b = node.content) === null || _b === void 0 ? void 0 : _b.slice(i, j)); i = j; } } while (node.content[i]); html += textSlices.join("\n" + spacers) + "\n"; } else html += "".concat(node.content); break; case node.type === "definition": html += "<".concat(node.tagName).concat(attributeList_1, ">").concat(options.formatFiles ? "\n" : ""); break; case (0, utils_1.isVoid)(node.tagName): html += "<".concat(node.tagName).concat(attributeList_1).concat(options.formatFiles ? " " : "", "/>").concat(options.formatFiles ? "\n" : ""); break; default: html += "<".concat(node.tagName).concat(attributeList_1, ">"); if (node.children && options.formatFiles) { if (((_c = node.children[0]) === null || _c === void 0 ? void 0 : _c.type) !== "text") { html += "\n"; } } html += _this.generate(node.children, options, spacers + tab); if (options.formatFiles) { if ((node.children && ((_d = node.children[0]) === null || _d === void 0 ? void 0 : _d.type) !== "text") || (node.children && node.children[0].type == "text" && node.children[0].content && ((_e = node.children[0].content) === null || _e === void 0 ? void 0 : _e.length) > 70)) { html += spacers; } } html += "</".concat(node.tagName, ">"); if (options.formatFiles) html += "\n"; break; } } }); } return html; }; return Generator; }()); exports.default = Generator;