Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export class ArinekHTML {
- #raw_html;
- html;
- #ns;
- keywords = [
- "loop"
- ];
- /**
- * @param {NS} ns
- * @param {string} html_file
- */
- constructor(ns, html_file) {
- this.#raw_html = ns.read(html_file);
- this.#ns = ns;
- }
- /**
- * @param {Collection} data
- */
- parse(data) {
- this.html = this.#raw_html;
- while (this.commands.length !== 0) {
- let commands = this.commands;
- //Validating Commands.
- const invalid_commands = this.validateCommands(commands, this.keywords);
- commands = commands.filter(command => invalid_commands.every(invalid_command => invalid_command.command !== command.command));
- let command_pairs = []
- for (const command of commands) {
- if (command.depth === 0) {
- command_pairs.push(command);
- }
- }
- let new_html = "";
- let prev_end_index = 0;
- for (let index = 0; index < command_pairs.length; index += 2) {
- const open_command = command_pairs[index];
- const close_command = command_pairs[index + 1];
- const open_command_end_index = open_command.index + open_command.command.length + 3;
- const close_command_end_index = close_command.index + close_command.command.length + 3;
- const content = this.html.slice(open_command_end_index, close_command.index);
- //this.#ns.tprint(open_command.command, this.html[open_command.index]);
- let args = open_command.command.split(":")[1].split(" ");
- for (let arg in args) {
- args[arg] = new Arg(open_command, args[arg].split("=")[0], args[arg].split("=")[1]);
- }
- const result = open_command.run(args, content, data, this.#ns);
- new_html += this.html.slice(prev_end_index, open_command.index) + result;
- prev_end_index = close_command_end_index;
- }
- new_html += this.html.slice(prev_end_index);
- this.html = new_html;
- //this.#ns.tprint(this.html);
- }
- for (const item in data.single) {
- this.html = this.html.replaceAll(item, data[item]);
- }
- }
- /**
- * @returns {Command[]}
- */
- get commands() {
- let commands = [];
- const html = this.html; //Shortened name.
- let index = -1; //Index in html file.
- let depth = -1; //Depth of command in command structure.
- //this.#ns.tprint("\n");
- //While there is another command.
- while (html.indexOf("${", index + 1) !== -1) {
- index = html.indexOf("${", index + 1);
- const end_of_command_index = html.indexOf("}", index);
- const command = html.slice(index + 2, end_of_command_index); //Command sliced from html from "${blablabla}" to "blablabla".
- //this.#ns.tprint(index + " ", html[index]);
- //Checking whether it is a command and not a variable.
- if (new RegExp(".*:.*").test(command)) {
- //Checking whether or not it is a closing statement, as such ending in two ":".
- if (new RegExp(".*::").test(command)) {
- const commandObject = new Command(command, depth, index);
- commands.push(commandObject);
- depth -= 1;
- }
- else {
- depth += 1;
- const commandObject = new Command(command, depth, index);
- commands.push(commandObject);
- }
- }
- }
- //this.#ns.tprint(html);
- return commands;
- }
- /**
- * @param {Command[]} commands
- * @param {string[]} keywords
- * @returns {Command[]?}
- */
- validateCommands(commands, keywords) {
- let invalid_commands = [];
- for (const command of commands) {
- if (!keywords.some(keyword => keyword == command.name) && !new RegExp(".*:.*").test(command.command)) {
- invalid_commands.push(command);
- }
- }
- return invalid_commands;
- }
- }
- class Command {
- #command;
- #name;
- #depth;
- #index;
- /**
- * @param {string} command
- * @param {number} depth
- * @param {number} index
- */
- constructor(command, depth, index) {
- this.#command = command;
- this.#name = command.split(":")[0];
- this.#depth = depth;
- this.#index = index;
- }
- get command() {
- return this.#command;
- }
- get name() {
- return this.#name;
- }
- get depth() {
- return this.#depth;
- }
- get index() {
- return this.#index;
- }
- /**
- * @param {Arg[]} args
- * @param {string} content
- * @param {Collection} data
- * @param {NS} ns
- */
- run(args, content, data, ns) {
- switch (this.#name.toUpperCase()) {
- case "LOOP":
- return new LOOP(args, content).run(data, ns);
- break;
- }
- }
- }
- class Arg {
- #command;
- #name;
- #value;
- /**
- * @param {Command} command
- * @param {string} name
- * @param {string} value
- */
- constructor(command, name, value) {
- this.#command = command;
- this.#name = name;
- this.#value = value;
- }
- get command() {
- return this.#command;
- }
- get name() {
- return this.#name;
- }
- get value() {
- return this.#value;
- }
- }
- class LOOP {
- #args;
- #content;
- /**
- *@param {Arg[]} args
- *@param {string} content
- */
- constructor(args, content) {
- this.#args = args;
- this.#content = content;
- }
- get args() {
- return this.#args;
- }
- get content() {
- return this.#content;
- }
- /**
- * @param {Collection} data
- * @param {NS} ns
- */
- run(data, ns) {
- const items = this.#args.find(arg => arg.name.trim() === "items");
- const item = this.#args.find(arg => arg.name.trim() === "item");
- const itemStatus = this.#args.find(arg => arg.name.trim() === "itemStatus");
- const begin = this.#args.find(arg => arg.name.trim() === "begin");
- const end = this.#args.find(arg => arg.name.trim() === "end");
- let new_content = "";
- if (items === undefined) {
- if (begin !== undefined && end !== undefined) {
- if (item === undefined) {
- return this.#content.repeat(Number(end.value) - Number(begin.value) + 1);
- } else {
- for (let i = begin.value; i <= end.value; i++) {
- new_content += this.#content;
- new_content = new_content.replaceAll("${" + item.value + "}", i);
- }
- }
- }
- } else if (item === undefined && itemStatus !== undefined) {
- for (let i = 0; i < data.data[items.value].length; i++) {
- new_content += this.#content;
- new_content = new_content.replaceAll("${" + itemStatus.value + "}", i);
- }
- } else if (item !== undefined && itemStatus === undefined) {
- for (let i = 0; i < data.data[items.value].length; i++) {
- new_content += this.#content;
- new_content = new_content.replaceAll("${" + item.value + "}", data.data[items.value][i]);
- }
- } else {
- for (let i = 0; i < data.data[items.value].length; i++) {
- new_content += this.#content;
- new_content = new_content.replaceAll("${" + item.value + "}", data.data[items.value][i]);
- new_content = new_content.replaceAll("${" + itemStatus.value + "}", i);
- }
- }
- return new_content;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement