~chipaca/ubuntu-push/gsettings

« back to all changes in this revision

Viewing changes to docs/example-server/node_modules/mocha/node_modules/jade/lib/jade.js

  • Committer: Tarmac
  • Author(s): Roberto Alsina
  • Date: 2014-09-08 12:45:38 UTC
  • mfrom: (326.1.3 re-doc)
  • Revision ID: tarmac-20140908124538-ajswdg1v1j4sw60o
[r=chipaca] Deduplicate text, and bring in the example code into the project.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*!
 
2
 * Jade
 
3
 * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
 
4
 * MIT Licensed
 
5
 */
 
6
 
 
7
/**
 
8
 * Module dependencies.
 
9
 */
 
10
 
 
11
var Parser = require('./parser')
 
12
  , Lexer = require('./lexer')
 
13
  , Compiler = require('./compiler')
 
14
  , runtime = require('./runtime')
 
15
// if node
 
16
  , fs = require('fs');
 
17
// end
 
18
 
 
19
/**
 
20
 * Library version.
 
21
 */
 
22
 
 
23
exports.version = '0.26.3';
 
24
 
 
25
/**
 
26
 * Expose self closing tags.
 
27
 */
 
28
 
 
29
exports.selfClosing = require('./self-closing');
 
30
 
 
31
/**
 
32
 * Default supported doctypes.
 
33
 */
 
34
 
 
35
exports.doctypes = require('./doctypes');
 
36
 
 
37
/**
 
38
 * Text filters.
 
39
 */
 
40
 
 
41
exports.filters = require('./filters');
 
42
 
 
43
/**
 
44
 * Utilities.
 
45
 */
 
46
 
 
47
exports.utils = require('./utils');
 
48
 
 
49
/**
 
50
 * Expose `Compiler`.
 
51
 */
 
52
 
 
53
exports.Compiler = Compiler;
 
54
 
 
55
/**
 
56
 * Expose `Parser`.
 
57
 */
 
58
 
 
59
exports.Parser = Parser;
 
60
 
 
61
/**
 
62
 * Expose `Lexer`.
 
63
 */
 
64
 
 
65
exports.Lexer = Lexer;
 
66
 
 
67
/**
 
68
 * Nodes.
 
69
 */
 
70
 
 
71
exports.nodes = require('./nodes');
 
72
 
 
73
/**
 
74
 * Jade runtime helpers.
 
75
 */
 
76
 
 
77
exports.runtime = runtime;
 
78
 
 
79
/**
 
80
 * Template function cache.
 
81
 */
 
82
 
 
83
exports.cache = {};
 
84
 
 
85
/**
 
86
 * Parse the given `str` of jade and return a function body.
 
87
 *
 
88
 * @param {String} str
 
89
 * @param {Object} options
 
90
 * @return {String}
 
91
 * @api private
 
92
 */
 
93
 
 
94
function parse(str, options){
 
95
  try {
 
96
    // Parse
 
97
    var parser = new Parser(str, options.filename, options);
 
98
 
 
99
    // Compile
 
100
    var compiler = new (options.compiler || Compiler)(parser.parse(), options)
 
101
      , js = compiler.compile();
 
102
 
 
103
    // Debug compiler
 
104
    if (options.debug) {
 
105
      console.error('\nCompiled Function:\n\n\033[90m%s\033[0m', js.replace(/^/gm, '  '));
 
106
    }
 
107
 
 
108
    return ''
 
109
      + 'var buf = [];\n'
 
110
      + (options.self
 
111
        ? 'var self = locals || {};\n' + js
 
112
        : 'with (locals || {}) {\n' + js + '\n}\n')
 
113
      + 'return buf.join("");';
 
114
  } catch (err) {
 
115
    parser = parser.context();
 
116
    runtime.rethrow(err, parser.filename, parser.lexer.lineno);
 
117
  }
 
118
}
 
119
 
 
120
/**
 
121
 * Compile a `Function` representation of the given jade `str`.
 
122
 *
 
123
 * Options:
 
124
 *
 
125
 *   - `compileDebug` when `false` debugging code is stripped from the compiled template
 
126
 *   - `client` when `true` the helper functions `escape()` etc will reference `jade.escape()`
 
127
 *      for use with the Jade client-side runtime.js
 
128
 *
 
129
 * @param {String} str
 
130
 * @param {Options} options
 
131
 * @return {Function}
 
132
 * @api public
 
133
 */
 
134
 
 
135
exports.compile = function(str, options){
 
136
  var options = options || {}
 
137
    , client = options.client
 
138
    , filename = options.filename
 
139
      ? JSON.stringify(options.filename)
 
140
      : 'undefined'
 
141
    , fn;
 
142
 
 
143
  if (options.compileDebug !== false) {
 
144
    fn = [
 
145
        'var __jade = [{ lineno: 1, filename: ' + filename + ' }];'
 
146
      , 'try {'
 
147
      , parse(String(str), options)
 
148
      , '} catch (err) {'
 
149
      , '  rethrow(err, __jade[0].filename, __jade[0].lineno);'
 
150
      , '}'
 
151
    ].join('\n');
 
152
  } else {
 
153
    fn = parse(String(str), options);
 
154
  }
 
155
 
 
156
  if (client) {
 
157
    fn = 'attrs = attrs || jade.attrs; escape = escape || jade.escape; rethrow = rethrow || jade.rethrow; merge = merge || jade.merge;\n' + fn;
 
158
  }
 
159
 
 
160
  fn = new Function('locals, attrs, escape, rethrow, merge', fn);
 
161
 
 
162
  if (client) return fn;
 
163
 
 
164
  return function(locals){
 
165
    return fn(locals, runtime.attrs, runtime.escape, runtime.rethrow, runtime.merge);
 
166
  };
 
167
};
 
168
 
 
169
/**
 
170
 * Render the given `str` of jade and invoke
 
171
 * the callback `fn(err, str)`.
 
172
 *
 
173
 * Options:
 
174
 *
 
175
 *   - `cache` enable template caching
 
176
 *   - `filename` filename required for `include` / `extends` and caching
 
177
 *
 
178
 * @param {String} str
 
179
 * @param {Object|Function} options or fn
 
180
 * @param {Function} fn
 
181
 * @api public
 
182
 */
 
183
 
 
184
exports.render = function(str, options, fn){
 
185
  // swap args
 
186
  if ('function' == typeof options) {
 
187
    fn = options, options = {};
 
188
  }
 
189
 
 
190
  // cache requires .filename
 
191
  if (options.cache && !options.filename) {
 
192
    return fn(new Error('the "filename" option is required for caching'));
 
193
  }
 
194
 
 
195
  try {
 
196
    var path = options.filename;
 
197
    var tmpl = options.cache
 
198
      ? exports.cache[path] || (exports.cache[path] = exports.compile(str, options))
 
199
      : exports.compile(str, options);
 
200
    fn(null, tmpl(options));
 
201
  } catch (err) {
 
202
    fn(err);
 
203
  }
 
204
};
 
205
 
 
206
/**
 
207
 * Render a Jade file at the given `path` and callback `fn(err, str)`.
 
208
 *
 
209
 * @param {String} path
 
210
 * @param {Object|Function} options or callback
 
211
 * @param {Function} fn
 
212
 * @api public
 
213
 */
 
214
 
 
215
exports.renderFile = function(path, options, fn){
 
216
  var key = path + ':string';
 
217
 
 
218
  if ('function' == typeof options) {
 
219
    fn = options, options = {};
 
220
  }
 
221
 
 
222
  try {
 
223
    options.filename = path;
 
224
    var str = options.cache
 
225
      ? exports.cache[key] || (exports.cache[key] = fs.readFileSync(path, 'utf8'))
 
226
      : fs.readFileSync(path, 'utf8');
 
227
    exports.render(str, options, fn);
 
228
  } catch (err) {
 
229
    fn(err);
 
230
  }
 
231
};
 
232
 
 
233
/**
 
234
 * Express support.
 
235
 */
 
236
 
 
237
exports.__express = exports.renderFile;