ocat-lang
Version:
A programming language for the web design and development
78 lines (77 loc) • 3.28 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.processHTML = void 0;
const fs_1 = require("fs");
const basic_1 = require("./basic");
const path_1 = __importDefault(require("path"));
const validateInputs = (memory, html) => {
if (!memory || typeof memory !== "object") {
throw new Error("Invalid memory object provided.");
}
if (typeof html !== "string") {
throw new Error("Invalid HTML input. Expected a string.");
}
};
const replaceComponentTags = (html, memory) => {
const componentRegex = /<\{\s*(\w+)([^}]*)\s*\}>/g;
return html.replace(componentRegex, (_match, componentName) => {
var _a;
return `<div class="oc-component-${componentName}">${(_a = memory.getComponent(componentName)) !== null && _a !== void 0 ? _a : `<p>Component not found</p>`}</div>`;
});
};
const replaceInnerTags = (html, memory) => {
const innerRegex = /\{\{\s*(\w+)([^}]*)\s*\}\}/g;
return html.replace(innerRegex, (_match, inner) => { var _a, _b; return ((_b = (_a = memory.getVar(inner)) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : "onotdefined").toString(); });
};
const replaceOrderTags = (html, memory) => {
const ordersRegex = /use\((\w+)\)/g;
return html.replace(ordersRegex, (_match, orderName) => {
var _a;
if (!memory.getProperties.includes(orderName)) {
return `This property is not defined for using.`;
}
return (_a = memory.getOrder(orderName)) !== null && _a !== void 0 ? _a : `${orderName} not found`;
});
};
const replaceTemplateTags = (html, memory) => {
const useTemplateRegex = /useTemplate\s*\(\s*([^,]+?)\s*,\s*([^,]+?)\s*\)/g;
const collectionParamRegex = /\{\*\s*(\w+)\s*\*\}/g;
return html.replace(useTemplateRegex, (_match, collectionName, value) => {
var _a;
const template = (0, fs_1.readFileSync)(path_1.default.join("./templates", value), "utf-8");
if (!template) {
return `Template ${value} not found`;
}
const collection = memory.getCollection(collectionName);
return ((_a = collection === null || collection === void 0 ? void 0 : collection.map((item) => {
return template.replace(collectionParamRegex, (_match, param) => {
if (param === "children" || param === "content") {
return item.content;
}
else {
return item.params[param];
}
});
}).join("")) !== null && _a !== void 0 ? _a : "");
});
};
const processHTML = (memory, html) => {
validateInputs(memory, html);
let processedHtml = html;
const proccessers = [
replaceTemplateTags,
replaceComponentTags,
replaceInnerTags,
replaceOrderTags,
];
proccessers.forEach((p) => {
processedHtml = p(processedHtml, memory);
});
processedHtml = (0, basic_1.processBasicWC)(processedHtml);
processedHtml = processedHtml.replace(/undefined/g, "");
return processedHtml;
};
exports.processHTML = processHTML;