Skip to content

Commit 64baa74

Browse files
committed
core: separates command check from the compileData
1 parent f21d07f commit 64baa74

File tree

1 file changed

+46
-31
lines changed

1 file changed

+46
-31
lines changed

core/Renderer.js

+46-31
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,19 @@ export class Renderer {
4949

5050
options.filename = filename;
5151

52+
if (!("debug" in options)) {
53+
options.debug = this.#debug;
54+
}
55+
5256
return await this.compileData(data, options, level);
5357
} catch (err) {
5458
return null;
5559
}
5660
}
5761

5862
async compileData(data, options = {}, level = 0) {
59-
const debug = ("debug" in options ? options.debug : this.#debug);
6063
const indent = (n = 1) => {
61-
if (!debug) return "";
64+
if (!options.debug) return "";
6265

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

70-
if (debug && options.filename) {
73+
if (options.debug && options.filename) {
7174
code += `${indent(0)}// ${options.filename}\n`;
7275
}
7376

@@ -77,7 +80,7 @@ export class Renderer {
7780
for (const line of lines) {
7881
switch (line[0]) {
7982
case "text":
80-
if (!debug) {
83+
if (!options.debug) {
8184
line[1] = line[1].toString().replace(/\n\s+/mg, "\n");
8285
}
8386
code += `${indent()}__output += "${escape(line[1], text_level)}";\n`;
@@ -110,35 +113,14 @@ export class Renderer {
110113
break;
111114
}
112115

113-
const match = line[1].match(/^(?<command>\w+)\s+(?<method>[^\(]+)(\((?<parameters>.*)\))?$/);
114-
115-
if (match) {
116-
const command = match.groups.command;
117-
118-
switch (command) {
119-
case "include": {
120-
const view = await this.compilePath(match.groups.method, options, options.filename ? dirname(options.filename) : null, debug ? text_level + 2 : text_level);
121-
122-
if (view !== null) {
123-
if (debug) {
124-
code += `\n${indent()}// include ${match.groups.method}\n`;
125-
}
126-
127-
code += `${indent()}__output += ((self) => {\n`;
128-
code += view.code;
129-
code += `${indent()}})(${match.groups.parameters?.length ? match.groups.parameters : "{}"});\n\n`;
130-
} else {
131-
code += `${indent()}__output += "${escape(new RenderingError(`Include not found: ${match.groups.method}`))}";\n`;
132-
}
133-
break;
134-
}
135-
default:
136-
code += `${indent()}__output += "${escape(new RenderingError(`Unknown command: ${command}`))}";\n`;
137-
}
116+
const command_code = await this.#checkCommands(line[1], options, indent, text_level);
117+
118+
if (command_code !== false) {
119+
code += command_code;
138120
continue;
139121
}
140122

141-
const level_diff = this.#levelChange(line[1], debug);
123+
const level_diff = this.#levelChange(line[1], options.debug);
142124

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

161143
const ret = (env) => {
162-
if (debug) {
144+
if (options.debug) {
163145
return funct.call(env, Filters).replace(/\n\s*\n/g, "\n").trim();
164146
}
165147
return funct.call(env, Filters).replace(/\n\s*\n/g, "\n").replace(/\x3e\n/g, ">").trim();
@@ -170,6 +152,39 @@ export class Renderer {
170152
return ret;
171153
}
172154

155+
async #checkCommands(line, options, indent, text_level) {
156+
const match = line.match(/^(?<command>\w+)\s+(?<method>[^\(]+)(\((?<parameters>.*)\))?$/);
157+
158+
if (!match) return false;
159+
160+
const command = match.groups.command;
161+
162+
let code = "";
163+
164+
switch (command) {
165+
case "include": {
166+
const view = await this.compilePath(match.groups.method, options, options.filename ? dirname(options.filename) : null, options.debug ? text_level + 2 : text_level);
167+
168+
if (view !== null) {
169+
if (options.debug) {
170+
code += `\n${indent()}// include ${match.groups.method}\n`;
171+
}
172+
173+
code += `${indent()}__output += ((self) => {\n`;
174+
code += view.code;
175+
code += `${indent()}})(${match.groups.parameters?.length ? match.groups.parameters : "{}"});\n\n`;
176+
} else {
177+
code += `${indent()}__output += "${escape(new RenderingError(`Include not found: ${match.groups.method}`))}";\n`;
178+
}
179+
break;
180+
}
181+
default:
182+
code += `${indent()}__output += "${escape(new RenderingError(`Unknown command: ${command}`))}";\n`;
183+
}
184+
185+
return code;
186+
}
187+
173188
#dataToLines(data) {
174189
const lines = [];
175190
let i = 0;

0 commit comments

Comments
 (0)