@@ -49,16 +49,19 @@ export class Renderer {
49
49
50
50
options . filename = filename ;
51
51
52
+ if ( ! ( "debug" in options ) ) {
53
+ options . debug = this . #debug;
54
+ }
55
+
52
56
return await this . compileData ( data , options , level ) ;
53
57
} catch ( err ) {
54
58
return null ;
55
59
}
56
60
}
57
61
58
62
async compileData ( data , options = { } , level = 0 ) {
59
- const debug = ( "debug" in options ? options . debug : this . #debug) ;
60
63
const indent = ( n = 1 ) => {
61
- if ( ! debug ) return "" ;
64
+ if ( ! options . debug ) return "" ;
62
65
63
66
return "\t" . repeat ( level + n ) ;
64
67
} ;
@@ -67,7 +70,7 @@ export class Renderer {
67
70
const lines = this . #dataToLines( data ) ;
68
71
let code = "" ;
69
72
70
- if ( debug && options . filename ) {
73
+ if ( options . debug && options . filename ) {
71
74
code += `${ indent ( 0 ) } // ${ options . filename } \n` ;
72
75
}
73
76
@@ -77,7 +80,7 @@ export class Renderer {
77
80
for ( const line of lines ) {
78
81
switch ( line [ 0 ] ) {
79
82
case "text" :
80
- if ( ! debug ) {
83
+ if ( ! options . debug ) {
81
84
line [ 1 ] = line [ 1 ] . toString ( ) . replace ( / \n \s + / mg, "\n" ) ;
82
85
}
83
86
code += `${ indent ( ) } __output += "${ escape ( line [ 1 ] , text_level ) } ";\n` ;
@@ -110,35 +113,14 @@ export class Renderer {
110
113
break ;
111
114
}
112
115
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 ;
138
120
continue ;
139
121
}
140
122
141
- const level_diff = this . #levelChange( line [ 1 ] , debug ) ;
123
+ const level_diff = this . #levelChange( line [ 1 ] , options . debug ) ;
142
124
143
125
if ( level_diff < 0 ) {
144
126
level += level_diff ;
@@ -159,7 +141,7 @@ export class Renderer {
159
141
const funct = new Function ( "__filters" , code ) ;
160
142
161
143
const ret = ( env ) => {
162
- if ( debug ) {
144
+ if ( options . debug ) {
163
145
return funct . call ( env , Filters ) . replace ( / \n \s * \n / g, "\n" ) . trim ( ) ;
164
146
}
165
147
return funct . call ( env , Filters ) . replace ( / \n \s * \n / g, "\n" ) . replace ( / \x3e \n / g, ">" ) . trim ( ) ;
@@ -170,6 +152,39 @@ export class Renderer {
170
152
return ret ;
171
153
}
172
154
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
+
173
188
#dataToLines( data ) {
174
189
const lines = [ ] ;
175
190
let i = 0 ;
0 commit comments