|
53 | 53 | MetaScript.compile = function(source) { |
54 | 54 | source = source+""; |
55 | 55 |
|
56 | | - var index = 0, // Current working index |
57 | | - expr = /(\/\/\?|\/\*\?)/g, // Line/block expression |
58 | | - exprLine = /\n|$/g, // Line terminator |
59 | | - exprBlock = /\*\//g, // Block terminator |
60 | | - match, matchEnd, // Matches |
61 | | - s, // Temporary string |
62 | | - indent = '', // Indentation |
63 | | - lastIndent = '', // Last indentation |
64 | | - out = []; // Output stack |
| 56 | + var index = 0, // Current working index |
| 57 | + expr = /(\/\/\?|\/\*\?)(=?)/g, // Line/block expression |
| 58 | + exprLine = /\n|$/g, // Line terminator |
| 59 | + exprBlock = /\*\//g, // Block terminator |
| 60 | + match, matchEnd, // Matches |
| 61 | + s, // Temporary string |
| 62 | + indent = '', // Indentation |
| 63 | + lastIndent = '', // Last indentation |
| 64 | + out = []; // Output stack |
65 | 65 |
|
66 | 66 | // Escapes a string to be used in a JavaScript string enclosed in single quotes |
67 | 67 | function escape(s) { |
|
88 | 88 | match; |
89 | 89 | while (match = expr.exec(source)) { |
90 | 90 | s = source.substring(index, match.index+1); |
91 | | - if (s !== '') out.push(' write(\''+escape(s)+'\');\n'); |
| 91 | + out.push(' write(\''+escape(s)+'\');\n'); |
92 | 92 | index = match.index+1; |
93 | 93 | } |
94 | 94 | s = source.substring(index, source.length); |
|
102 | 102 | s = source.substring(index, match.index); |
103 | 103 |
|
104 | 104 | // Look if it is a line or a block of meta |
105 | | - if (match[1] === '/'+'/?') { // Line |
106 | | - |
107 | | - // Trim whitespaces in front of the line and remember the indentation for include() and such |
108 | | - s = s.replace(/\n([ \t]*)$/, function($0, $1) { indent = $1; return '\n'; }); |
| 105 | + if (match[1].indexOf('*') < 0) { // Line |
109 | 106 |
|
| 107 | + // Trim whitespaces in front of the line and remember the indentation |
| 108 | + if (match[2] !== '=') |
| 109 | + s = s.replace(/(^|\n)([ \t]*)$/, function($0, $1, $2) { indent = $2; return $1; }); |
| 110 | + |
110 | 111 | // Append leading contents |
111 | 112 | append(s); |
112 | 113 |
|
|
119 | 120 | out.push('__=\''+escape(lastIndent = indent)+'\';\n'); |
120 | 121 | } |
121 | 122 | out.push(evaluate(source.substring(match.index+3, matchEnd.index).trim())); |
122 | | - |
| 123 | + if (match[2] === '=') |
| 124 | + out.push('writeln();\n'); |
| 125 | + |
123 | 126 | // Move on |
124 | 127 | index = matchEnd.index+1; |
125 | 128 |
|
126 | 129 | } else { // Block |
127 | 130 |
|
128 | | - // Trim whitespaces in front of the block if it is using a dedicated line and remember the indentation |
129 | | - s = s.replace(/\n([ \t]+)$/, function($0, $1) { indent = $1; return '\n'; }); |
| 131 | + // Trim whitespaces in front of the block and remember the indentation |
| 132 | + if (match[2] !== '=') |
| 133 | + s = s.replace(/(^|\n)([ \t]*)$/, function($0, $1, $2) { indent = $2; return $1; }); |
130 | 134 |
|
131 | 135 | // Append leading contents |
132 | 136 | append(s); |
|
181 | 185 | ///////////////////////////////////////////// Built-in functions /////////////////////////////////////////////// |
182 | 186 |
|
183 | 187 | /** |
184 | | - * Writes some contents to the document. |
| 188 | + * Writes some contents to the document (no indentation). |
185 | 189 | * @param {*} s Contents to write |
186 | 190 | */ |
187 | 191 | function write(s) { |
|
193 | 197 | * @param {*} s Contents to write |
194 | 198 | */ |
195 | 199 | function writeln(s) { |
196 | | - out.push(s+"\n"); |
| 200 | + if (typeof s === 'undefined') s = ''; |
| 201 | + out.push(s + "\n"); |
197 | 202 | } |
198 | 203 |
|
199 | 204 | /** |
|
237 | 242 | while (indent_str.length < indent) indent_str += ' '; |
238 | 243 | indent = indent_str; |
239 | 244 | } |
240 | | - var lines = str.split("\n"); |
| 245 | + var lines = str.split(/\n/); |
241 | 246 | for (var i=0; i<lines.length; i++) { |
242 | | - lines[i] = indent + lines[i]; |
| 247 | + if (lines[i].trim() !== '') |
| 248 | + lines[i] = indent + lines[i]; |
243 | 249 | } |
244 | 250 | return lines.join("\n"); |
245 | 251 | } |
|
0 commit comments