Skip to content

Commit

Permalink
core: separates command check from the compileData
Browse files Browse the repository at this point in the history
  • Loading branch information
dresende committed Feb 6, 2024
1 parent f21d07f commit 64baa74
Showing 1 changed file with 46 additions and 31 deletions.
77 changes: 46 additions & 31 deletions core/Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,19 @@ export class Renderer {

options.filename = filename;

if (!("debug" in options)) {
options.debug = this.#debug;
}

return await this.compileData(data, options, level);
} catch (err) {
return null;
}
}

async compileData(data, options = {}, level = 0) {
const debug = ("debug" in options ? options.debug : this.#debug);
const indent = (n = 1) => {
if (!debug) return "";
if (!options.debug) return "";

return "\t".repeat(level + n);
};
Expand All @@ -67,7 +70,7 @@ export class Renderer {
const lines = this.#dataToLines(data);
let code = "";

if (debug && options.filename) {
if (options.debug && options.filename) {
code += `${indent(0)}// ${options.filename}\n`;
}

Expand All @@ -77,7 +80,7 @@ export class Renderer {
for (const line of lines) {
switch (line[0]) {
case "text":
if (!debug) {
if (!options.debug) {
line[1] = line[1].toString().replace(/\n\s+/mg, "\n");
}
code += `${indent()}__output += "${escape(line[1], text_level)}";\n`;
Expand Down Expand Up @@ -110,35 +113,14 @@ export class Renderer {
break;
}

const match = line[1].match(/^(?<command>\w+)\s+(?<method>[^\(]+)(\((?<parameters>.*)\))?$/);

if (match) {
const command = match.groups.command;

switch (command) {
case "include": {
const view = await this.compilePath(match.groups.method, options, options.filename ? dirname(options.filename) : null, debug ? text_level + 2 : text_level);

if (view !== null) {
if (debug) {
code += `\n${indent()}// include ${match.groups.method}\n`;
}

code += `${indent()}__output += ((self) => {\n`;
code += view.code;
code += `${indent()}})(${match.groups.parameters?.length ? match.groups.parameters : "{}"});\n\n`;
} else {
code += `${indent()}__output += "${escape(new RenderingError(`Include not found: ${match.groups.method}`))}";\n`;
}
break;
}
default:
code += `${indent()}__output += "${escape(new RenderingError(`Unknown command: ${command}`))}";\n`;
}
const command_code = await this.#checkCommands(line[1], options, indent, text_level);

if (command_code !== false) {
code += command_code;
continue;
}

const level_diff = this.#levelChange(line[1], debug);
const level_diff = this.#levelChange(line[1], options.debug);

if (level_diff < 0) {
level += level_diff;
Expand All @@ -159,7 +141,7 @@ export class Renderer {
const funct = new Function("__filters", code);

const ret = (env) => {
if (debug) {
if (options.debug) {
return funct.call(env, Filters).replace(/\n\s*\n/g, "\n").trim();
}
return funct.call(env, Filters).replace(/\n\s*\n/g, "\n").replace(/\x3e\n/g, ">").trim();
Expand All @@ -170,6 +152,39 @@ export class Renderer {
return ret;
}

async #checkCommands(line, options, indent, text_level) {
const match = line.match(/^(?<command>\w+)\s+(?<method>[^\(]+)(\((?<parameters>.*)\))?$/);

if (!match) return false;

const command = match.groups.command;

let code = "";

switch (command) {
case "include": {
const view = await this.compilePath(match.groups.method, options, options.filename ? dirname(options.filename) : null, options.debug ? text_level + 2 : text_level);

if (view !== null) {
if (options.debug) {
code += `\n${indent()}// include ${match.groups.method}\n`;
}

code += `${indent()}__output += ((self) => {\n`;
code += view.code;
code += `${indent()}})(${match.groups.parameters?.length ? match.groups.parameters : "{}"});\n\n`;
} else {
code += `${indent()}__output += "${escape(new RenderingError(`Include not found: ${match.groups.method}`))}";\n`;
}
break;
}
default:
code += `${indent()}__output += "${escape(new RenderingError(`Unknown command: ${command}`))}";\n`;
}

return code;
}

#dataToLines(data) {
const lines = [];
let i = 0;
Expand Down

0 comments on commit 64baa74

Please sign in to comment.