|
20 | 20 | * see: https://github.com/dcodeIO/MetaScript for details |
21 | 21 | */ // |
22 | 22 | (function(global) { |
23 | | - // not strict |
| 23 | + // not strict for global var shenanigans |
24 | 24 |
|
25 | | - // This is a rather small program with lots of comments, so everyone can hack it easily. |
26 | | - |
27 | 25 | /** |
28 | 26 | * Constructs a new MetaScript instance. |
| 27 | + * @exports MetaScript |
29 | 28 | * @param {string=} source Source to compile |
30 | 29 | * @constructor |
31 | 30 | */ |
32 | 31 | var MetaScript = function(source) { |
33 | 32 |
|
34 | 33 | /** |
35 | | - * Meta program. |
| 34 | + * Meta program source. |
36 | 35 | * @type {?string} |
37 | 36 | */ |
38 | 37 | this.program = typeof source !== 'undefined' ? MetaScript.compile(source) : null; |
|
64 | 63 | out = []; // Output stack |
65 | 64 |
|
66 | 65 | // Escapes a string to be used in a JavaScript string enclosed in single quotes |
67 | | - function escape(s) { |
| 66 | + function escapestr(s) { |
68 | 67 | return s.replace(/\\/g, '\\\\').replace(/'/g, '\\\'').replace(/\r/g, '\\r').replace(/\n/g, '\\n'); |
69 | 68 | } |
70 | 69 |
|
|
88 | 87 | match; |
89 | 88 | while (match = expr.exec(source)) { |
90 | 89 | s = source.substring(index, match.index+1); |
91 | | - if (s !== '') out.push(' write(\''+escape(s)+'\');\n'); |
| 90 | + if (s !== '') out.push(' write(\''+escapestr(s)+'\');\n'); |
92 | 91 | index = match.index+1; |
93 | 92 | } |
94 | 93 | s = source.substring(index, source.length); |
95 | | - if (s !== '') out.push(' write(\''+escape(s)+'\');\n'); |
| 94 | + if (s !== '') out.push(' write(\''+escapestr(s)+'\');\n'); |
96 | 95 | } |
97 | 96 |
|
98 | 97 | // Turn the meta inside out: |
|
117 | 116 |
|
118 | 117 | // Expose indentation and evaluate expression |
119 | 118 | if (indent !== lastIndent) { |
120 | | - out.push('__=\''+escape(lastIndent = indent)+'\';\n'); |
| 119 | + out.push('__=\''+escapestr(lastIndent = indent)+'\';\n'); |
121 | 120 | } |
122 | 121 | out.push(evaluate(source.substring(match.index+3, matchEnd.index).trim())); |
123 | 122 | if (match[2] === '=') |
|
141 | 140 |
|
142 | 141 | // Expose indentation and evaluate expression |
143 | 142 | if (indent !== lastIndent) { |
144 | | - out.push('__=\''+escape(lastIndent = indent)+'\';\n'); |
| 143 | + out.push('__=\''+escapestr(lastIndent = indent)+'\';\n'); |
145 | 144 | } |
146 | 145 | out.push(evaluate(source.substring(match.index+3, matchEnd.index).trim())); |
147 | 146 |
|
|
176 | 175 | */ |
177 | 176 | MetaScript.transform = function(source, scope, basedir) { |
178 | 177 | if (MetaScript.IS_NODE) { |
179 | | - var sandbox; |
180 | | - require("vm").runInNewContext('__result = new MetaScript(__source).transform(__scope, __basedir);', sandbox = { |
181 | | - __source: source, |
182 | | - __scope: scope, |
183 | | - __basedir: basedir, |
184 | | - MetaScript: MetaScript |
| 178 | + var vm = require("vm"), |
| 179 | + sandbox; |
| 180 | + vm.runInNewContext('__result = new MetaScript(__source).transform(__scope, __basedir);', sandbox = { |
| 181 | + __source : source, |
| 182 | + __scope : scope, |
| 183 | + __basedir : basedir, |
| 184 | + MetaScript : MetaScript |
185 | 185 | }); |
186 | 186 | return sandbox.__result; |
187 | 187 | } else { |
188 | | - return new MetaScript(source).transform(scope, basedir); |
| 188 | + return new MetaScript(source).transform(scope, basedir); // Will probably pollute the global namespace |
189 | 189 | } |
190 | 190 | }; |
191 | 191 |
|
|
211 | 211 |
|
212 | 212 | /** |
213 | 213 | * Writes some contents to the document (no indentation). |
| 214 | + * @function write |
214 | 215 | * @param {*} s Contents to write |
215 | 216 | */ |
216 | 217 | function write(s) { |
|
219 | 220 |
|
220 | 221 | /** |
221 | 222 | * Writes some contents to the document, followed by a new line. |
| 223 | + * @function writeln |
222 | 224 | * @param {*} s Contents to write |
223 | 225 | */ |
224 | 226 | function writeln(s) { |
|
228 | 230 |
|
229 | 231 | /** |
230 | 232 | * Extracts the directory name from a file name. |
| 233 | + * @function dirname |
231 | 234 | * @param {string} filename File name |
232 | 235 | * @returns {string} Directory name, defaults to `.` |
233 | 236 | */ |
|
238 | 241 | } |
239 | 242 |
|
240 | 243 | /** |
241 | | - * Intents a block of text. |
| 244 | + * Indents a block of text. |
| 245 | + * @function indent |
242 | 246 | * @param {string} str Text to indent |
243 | 247 | * @param {string|number} indent Whitespace text to use for indentation or the number of whitespaces to use |
244 | 248 | * @returns {string} Indented text |
|
259 | 263 |
|
260 | 264 | /** |
261 | 265 | * Includes another source file. |
| 266 | + * @function include |
262 | 267 | * @param {string} __filename File to include |
263 | 268 | * @param {boolean} __absolute Whether the path is absolute, defaults to `false` for a relative path |
264 | 269 | */ |
|
296 | 301 |
|
297 | 302 | /** |
298 | 303 | * Escaoes a string to be used inside of a single or double quote enclosed JavaScript string. |
| 304 | + * @function escapestr |
299 | 305 | * @param {string} s String to escape |
300 | 306 | * @returns {string} Escaped string |
301 | 307 | */ |
|
354 | 360 | if (typeof module != 'undefined' && module["exports"]) { // CommonJS |
355 | 361 | module["exports"] = MetaScript; |
356 | 362 | } else if (typeof define != 'undefined' && define["amd"]) { // AMD |
357 | | - define(function() { return MetaScript; }); |
| 363 | + define([], function() { return MetaScript; }); |
358 | 364 | } else { // Shim |
359 | 365 | if (!global["dcodeIO"]) { |
360 | 366 | global["dcodeIO"] = {}; |
|
0 commit comments