puglite 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/error.js ADDED
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = makeError;
4
+ function makeError(code, message, options) {
5
+ var line = options.line;
6
+ var column = options.column;
7
+ var filename = options.filename;
8
+ var src = options.src;
9
+ var fullMessage;
10
+ var location = line + (column ? ':' + column : '');
11
+ if (src && line >= 1 && line <= src.split('\n').length) {
12
+ var lines = src.split('\n');
13
+ var start_1 = Math.max(line - 3, 0);
14
+ var end = Math.min(lines.length, line + 3);
15
+ // Error context
16
+ var context = lines
17
+ .slice(start_1, end)
18
+ .map(function (text, i) {
19
+ var curr = i + start_1 + 1;
20
+ var preamble = (curr == line ? ' > ' : ' ') + curr + '| ';
21
+ var out = preamble + text;
22
+ if (curr === line && column > 0) {
23
+ out += '\n';
24
+ out += Array(preamble.length + column).join('-') + '^';
25
+ }
26
+ return out;
27
+ })
28
+ .join('\n');
29
+ fullMessage =
30
+ (filename || 'Pug') + ':' + location + '\n' + context + '\n\n' + message;
31
+ }
32
+ else {
33
+ fullMessage = (filename || 'Pug') + ':' + location + '\n\n' + message;
34
+ }
35
+ var err = new Error(fullMessage);
36
+ err.code = 'PUG:' + code;
37
+ err.msg = message;
38
+ err.line = line;
39
+ err.column = column;
40
+ err.filename = filename;
41
+ err.src = src;
42
+ err.toJSON = function () {
43
+ return {
44
+ code: this.code,
45
+ msg: this.msg,
46
+ line: this.line,
47
+ column: this.column,
48
+ filename: this.filename,
49
+ };
50
+ };
51
+ return err;
52
+ }
53
+ // Make this easier to use from CommonJS
54
+ module.exports = makeError;
55
+ module.exports.default = makeError;
56
+ //# sourceMappingURL=index.js.map
package/lib/index.js ADDED
@@ -0,0 +1,416 @@
1
+ 'use strict';
2
+
3
+ /*!
4
+ * Pug
5
+ * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
6
+ * MIT Licensed
7
+ */
8
+
9
+ /**
10
+ * Module dependencies.
11
+ */
12
+
13
+ var fs = require('fs');
14
+ var path = require('path');
15
+ var lex = require('./lexer');
16
+ var stripComments = require('./strip-comments');
17
+ var parse = require('./parser');
18
+ var generateCode = require('./code-gen');
19
+ var runtime = require('./runtime');
20
+ var runtimeWrap = require('./runtime-wrap');
21
+
22
+ /**
23
+ * Name for detection
24
+ */
25
+
26
+ exports.name = 'Pug';
27
+
28
+ /**
29
+ * Pug runtime helpers.
30
+ */
31
+
32
+ exports.runtime = runtime;
33
+
34
+ /**
35
+ * Template function cache.
36
+ */
37
+
38
+ exports.cache = {};
39
+
40
+ function applyPlugins(value, options, plugins, name) {
41
+ return plugins.reduce(function(value, plugin) {
42
+ return plugin[name] ? plugin[name](value, options) : value;
43
+ }, value);
44
+ }
45
+
46
+ function findReplacementFunc(plugins, name) {
47
+ var eligiblePlugins = plugins.filter(function(plugin) {
48
+ return plugin[name];
49
+ });
50
+
51
+ if (eligiblePlugins.length > 1) {
52
+ throw new Error('Two or more plugins all implement ' + name + ' method.');
53
+ } else if (eligiblePlugins.length) {
54
+ return eligiblePlugins[0][name].bind(eligiblePlugins[0]);
55
+ }
56
+ return null;
57
+ }
58
+
59
+
60
+ /**
61
+ * Compile the given `str` of pug and return a function body.
62
+ *
63
+ * @param {String} str
64
+ * @param {Object} options
65
+ * @return {Object}
66
+ * @api private
67
+ */
68
+
69
+ function compileBody(str, options) {
70
+ var debug_sources = {};
71
+ debug_sources[options.filename] = str;
72
+ var dependencies = [];
73
+ var plugins = options.plugins || [];
74
+ // Lex
75
+ var lexOptions = {
76
+ filename: options.filename,
77
+ plugins: plugins
78
+ .filter(function(plugin) {
79
+ return !!plugin.lex;
80
+ })
81
+ .map(function(plugin) {
82
+ return plugin.lex;
83
+ })
84
+ };
85
+ var contents = applyPlugins(
86
+ str,
87
+ {filename: options.filename},
88
+ plugins,
89
+ 'preLex'
90
+ );
91
+ var tokens = applyPlugins(
92
+ lex(contents, lexOptions),
93
+ {filename: options.filename},
94
+ plugins,
95
+ 'postLex'
96
+ );
97
+
98
+ // Parse
99
+ tokens = stripComments(tokens, {filename: options.filename});
100
+ tokens = applyPlugins(tokens, {filename: options.filename}, plugins, 'preParse');
101
+ var parseOptions = {
102
+ filename: options.filename,
103
+ src: str,
104
+ plugins: plugins
105
+ .filter(function(plugin) {
106
+ return !!plugin.parse;
107
+ })
108
+ .map(function(plugin) {
109
+ return plugin.parse;
110
+ })
111
+ };
112
+ var ast = applyPlugins(
113
+ applyPlugins(
114
+ parse(tokens, parseOptions),
115
+ {filename: options.filename},
116
+ plugins,
117
+ 'postParse'
118
+ ),
119
+ {filename: options.filename},
120
+ plugins,
121
+ 'preLoad'
122
+ );
123
+ ast = applyPlugins(ast, options, plugins, 'postLoad');
124
+
125
+ // Compile
126
+ ast = applyPlugins(ast, options, plugins, 'preCodeGen');
127
+ var js = (findReplacementFunc(plugins, 'generateCode') || generateCode)(ast, {
128
+ pretty: options.pretty,
129
+ compileDebug: options.compileDebug,
130
+ doctype: options.doctype,
131
+ inlineRuntimeFunctions: options.inlineRuntimeFunctions,
132
+ globals: options.globals,
133
+ self: options.self,
134
+ includeSources: options.includeSources ? debug_sources : false,
135
+ templateName: options.templateName,
136
+ });
137
+ js = applyPlugins(js, options, plugins, 'postCodeGen');
138
+
139
+ // Debug compiler
140
+ if (options.debug) {
141
+ console.error(
142
+ '\nCompiled Function:\n\n\u001b[90m%s\u001b[0m',
143
+ js.replace(/^/gm, ' ')
144
+ );
145
+ }
146
+
147
+ return {body: js, dependencies: dependencies};
148
+ }
149
+
150
+ /**
151
+ * Get the template from a string or a file, either compiled on-the-fly or
152
+ * read from cache (if enabled), and cache the template if needed.
153
+ *
154
+ * If `str` is not set, the file specified in `options.filename` will be read.
155
+ *
156
+ * If `options.cache` is true, this function reads the file from
157
+ * `options.filename` so it must be set prior to calling this function.
158
+ *
159
+ * @param {Object} options
160
+ * @param {String=} str
161
+ * @return {Function}
162
+ * @api private
163
+ */
164
+ function handleTemplateCache(options, str) {
165
+ var key = options.filename;
166
+ if (options.cache && exports.cache[key]) {
167
+ return exports.cache[key];
168
+ } else {
169
+ if (str === undefined) str = fs.readFileSync(options.filename, 'utf8');
170
+ var templ = exports.compile(str, options);
171
+ if (options.cache) exports.cache[key] = templ;
172
+ return templ;
173
+ }
174
+ }
175
+
176
+ /**
177
+ * Compile a `Function` representation of the given pug `str`.
178
+ *
179
+ * Options:
180
+ *
181
+ * - `compileDebug` when `false` debugging code is stripped from the compiled
182
+ template, when it is explicitly `true`, the source code is included in
183
+ the compiled template for better accuracy.
184
+ * - `filename` used to improve errors when `compileDebug` is not `false` and to resolve imports/extends
185
+ *
186
+ * @param {String} str
187
+ * @param {Options} options
188
+ * @return {Function}
189
+ * @api public
190
+ */
191
+
192
+ exports.compile = function(str, options) {
193
+ var options = options || {};
194
+
195
+ str = String(str);
196
+
197
+ var parsed = compileBody(str, {
198
+ compileDebug: options.compileDebug !== false,
199
+ filename: options.filename,
200
+ basedir: options.basedir,
201
+ pretty: options.pretty,
202
+ doctype: options.doctype,
203
+ inlineRuntimeFunctions: options.inlineRuntimeFunctions,
204
+ globals: options.globals,
205
+ self: options.self,
206
+ includeSources: options.compileDebug === true,
207
+ debug: options.debug,
208
+ templateName: 'template',
209
+ plugins: options.plugins,
210
+ });
211
+
212
+ var res = options.inlineRuntimeFunctions
213
+ ? new Function('', parsed.body + ';return template;')()
214
+ : runtimeWrap(parsed.body);
215
+
216
+ res.dependencies = parsed.dependencies;
217
+
218
+ return res;
219
+ };
220
+
221
+ /**
222
+ * Compile a JavaScript source representation of the given pug `str`.
223
+ *
224
+ * Options:
225
+ *
226
+ * - `compileDebug` When it is `true`, the source code is included in
227
+ * the compiled template for better error messages.
228
+ * - `filename` used to improve errors when `compileDebug` is not `true` and to resolve imports/extends
229
+ * - `name` the name of the resulting function (defaults to "template")
230
+ * - `module` when it is explicitly `true`, the source code include export module syntax
231
+ *
232
+ * @param {String} str
233
+ * @param {Options} options
234
+ * @return {Object}
235
+ * @api public
236
+ */
237
+
238
+ exports.compileClientWithDependenciesTracked = function(str, options) {
239
+ var options = options || {};
240
+
241
+ str = String(str);
242
+ var parsed = compileBody(str, {
243
+ compileDebug: options.compileDebug,
244
+ filename: options.filename,
245
+ basedir: options.basedir,
246
+ pretty: options.pretty,
247
+ doctype: options.doctype,
248
+ inlineRuntimeFunctions: options.inlineRuntimeFunctions !== false,
249
+ globals: options.globals,
250
+ self: options.self,
251
+ includeSources: options.compileDebug,
252
+ debug: options.debug,
253
+ templateName: options.name || 'template',
254
+ plugins: options.plugins,
255
+ });
256
+
257
+ var body = parsed.body;
258
+
259
+ if (options.module) {
260
+ if (options.inlineRuntimeFunctions === false) {
261
+ body = 'var pug = require("pug-runtime");' + body;
262
+ }
263
+ body += ' module.exports = ' + (options.name || 'template') + ';';
264
+ }
265
+
266
+ return {body: body, dependencies: parsed.dependencies};
267
+ };
268
+
269
+ /**
270
+ * Compile a JavaScript source representation of the given pug `str`.
271
+ *
272
+ * Options:
273
+ *
274
+ * - `compileDebug` When it is `true`, the source code is included in
275
+ * the compiled template for better error messages.
276
+ * - `filename` used to improve errors when `compileDebug` is not `true` and to resolve imports/extends
277
+ * - `name` the name of the resulting function (defaults to "template")
278
+ *
279
+ * @param {String} str
280
+ * @param {Options} options
281
+ * @return {String}
282
+ * @api public
283
+ */
284
+ exports.compileClient = function(str, options) {
285
+ return exports.compileClientWithDependenciesTracked(str, options).body;
286
+ };
287
+
288
+ /**
289
+ * Compile a `Function` representation of the given pug file.
290
+ *
291
+ * Options:
292
+ *
293
+ * - `compileDebug` when `false` debugging code is stripped from the compiled
294
+ template, when it is explicitly `true`, the source code is included in
295
+ the compiled template for better accuracy.
296
+ *
297
+ * @param {String} path
298
+ * @param {Options} options
299
+ * @return {Function}
300
+ * @api public
301
+ */
302
+ exports.compileFile = function(path, options) {
303
+ options = options || {};
304
+ options.filename = path;
305
+ return handleTemplateCache(options);
306
+ };
307
+
308
+ /**
309
+ * Render the given `str` of pug.
310
+ *
311
+ * Options:
312
+ *
313
+ * - `cache` enable template caching
314
+ * - `filename` filename required for `include` / `extends` and caching
315
+ *
316
+ * @param {String} str
317
+ * @param {Object|Function} options or fn
318
+ * @param {Function|undefined} fn
319
+ * @returns {String}
320
+ * @api public
321
+ */
322
+
323
+ exports.render = function(str, options, fn) {
324
+ // support callback API
325
+ if ('function' == typeof options) {
326
+ (fn = options), (options = undefined);
327
+ }
328
+ if (typeof fn === 'function') {
329
+ var res;
330
+ try {
331
+ res = exports.render(str, options);
332
+ } catch (ex) {
333
+ return fn(ex);
334
+ }
335
+ return fn(null, res);
336
+ }
337
+
338
+ options = options || {};
339
+
340
+ // cache requires .filename
341
+ if (options.cache && !options.filename) {
342
+ throw new Error('the "filename" option is required for caching');
343
+ }
344
+
345
+ return handleTemplateCache(options, str)(options);
346
+ };
347
+
348
+ /**
349
+ * Render a Pug file at the given `path`.
350
+ *
351
+ * @param {String} path
352
+ * @param {Object|Function} options or callback
353
+ * @param {Function|undefined} fn
354
+ * @returns {String}
355
+ * @api public
356
+ */
357
+
358
+ exports.renderFile = function(path, options, fn) {
359
+ // support callback API
360
+ if ('function' == typeof options) {
361
+ (fn = options), (options = undefined);
362
+ }
363
+ if (typeof fn === 'function') {
364
+ var res;
365
+ try {
366
+ res = exports.renderFile(path, options);
367
+ } catch (ex) {
368
+ return fn(ex);
369
+ }
370
+ return fn(null, res);
371
+ }
372
+
373
+ options = options || {};
374
+
375
+ options.filename = path;
376
+ return handleTemplateCache(options)(options);
377
+ };
378
+
379
+ /**
380
+ * Compile a Pug file at the given `path` for use on the client.
381
+ *
382
+ * @param {String} path
383
+ * @param {Object} options
384
+ * @returns {String}
385
+ * @api public
386
+ */
387
+
388
+ exports.compileFileClient = function(path, options) {
389
+ var key = path + ':client';
390
+ options = options || {};
391
+
392
+ options.filename = path;
393
+
394
+ if (options.cache && exports.cache[key]) {
395
+ return exports.cache[key];
396
+ }
397
+
398
+ var str = fs.readFileSync(options.filename, 'utf8');
399
+ var out = exports.compileClient(str, options);
400
+ if (options.cache) exports.cache[key] = out;
401
+ return out;
402
+ };
403
+
404
+ /**
405
+ * Express support.
406
+ */
407
+
408
+ exports.__express = function(path, options, fn) {
409
+ if (
410
+ options.compileDebug == undefined &&
411
+ process.env.NODE_ENV === 'production'
412
+ ) {
413
+ options.compileDebug = false;
414
+ }
415
+ exports.renderFile(path, options, fn);
416
+ };