Permalink
Browse files

Merge branch 'refactor'

  • Loading branch information...
2 parents c7247bc + 79bc2e8 commit 9a3cdf48cc98e37fc96c16405b14f0d822c09a65 @tj tj committed Jul 27, 2010
Showing with 62 additions and 42 deletions.
  1. +62 −42 lib/jade.js
View
@@ -705,6 +705,63 @@ function interpolate(str){
}
/**
+ * Re-throw the given `err` in context to the
+ * `str` of jade, `filename`, and `lineno`.
+ *
+ * @param {Error} err
+ * @param {String} str
+ * @param {String} filename
+ * @param {String} lineno
+ * @api private
+ */
+
+function rethrow(err, str, filename, lineno){
+ var start = lineno - 3 > 0
+ ? lineno - 3
+ : 0;
+ // Error context
+ var context = str.split('\n').slice(start, lineno).map(function(line, i){
+ return ' ' + (i + start + 1) + '. ' + sys.inspect(line);
+ }).join('\n');
+
+ // Alter exception message
+ err.path = filename;
+ err.message = (filename || 'Jade') + ':' + lineno
+ + '\n' + context + '\n\n' + err.message;
+ throw err;
+}
+
+/**
+ * Parse the given `str` of jade and return a `Function`.
+ *
+ * @param {String} str
+ * @param {Object} options
+ * @return {Function}
+ * @api private
+ */
+
+function parse(str, options){
+ var filename = options.filename;
+ try {
+ var parser = new Parser(str, filename);
+ if (options.debug) {
+ parser.debug();
+ parser = new Parser(str, filename);
+ }
+ var js = parser.parse();
+ if (options.debug) sys.puts('\nfunction:', js.replace(/^/gm, ' '));
+ try {
+ return new Function('locals, attrs, escape, _', 'with (locals) {' + js + '}');
+ } catch (err) {
+ process.compile(js, filename || 'Jade');
+ return;
+ }
+ } catch (err) {
+ rethrow(err, str, filename, parser.lineno);
+ }
+}
+
+/**
* Render the given `str` of jade.
*
* Options:
@@ -725,59 +782,22 @@ exports.render = function(str, options){
options = options || {},
filename = options.filename;
- // Attempt to parse
- function parse(){
- try {
- var parser = new Parser(str, filename);
- if (options.debug) {
- parser.debug();
- parser = new Parser(str, filename);
- }
- var js = parser.parse();
- if (options.debug) sys.puts('\nfunction:', js.replace(/^/gm, ' '));
- try {
- return new Function('locals, attrs, escape, _', 'with (locals) {' + js + '}');
- } catch (err) {
- process.compile(js, filename || 'Jade');
- return;
- }
- } catch (err) {
- rethrow(err, parser.lineno);
- }
- }
-
- // Re-throw the given error
- function rethrow(err, lineno){
- var start = lineno - 3 > 0
- ? lineno - 3
- : 0;
- // Error context
- var context = str.split('\n').slice(start, lineno).map(function(line, i){
- return ' ' + (i + start + 1) + '. ' + sys.inspect(line);
- }).join('\n');
-
- // Alter exception message
- err.path = filename;
- err.message = (filename || 'Jade') + ':' + lineno
- + '\n' + context + '\n\n' + err.message;
- throw err;
- }
-
- // Cache templates
+ // Cache support
if (options.cache) {
if (filename) {
if (cache[filename]) {
fn = cache[filename];
} else {
- fn = cache[filename] = parse();
+ fn = cache[filename] = parse(str, options);
}
} else {
throw new Error('filename is required when using the cache option');
}
} else {
- fn = parse();
+ fn = parse(str, options);
}
+ // Render the template
try {
options.locals = options.locals || {};
options.locals.sys = sys;
@@ -788,7 +808,7 @@ exports.render = function(str, options){
escape,
_);
} catch (err) {
- rethrow(err, _.lineno);
+ rethrow(err, str, filename, _.lineno);
}
};

0 comments on commit 9a3cdf4

Please sign in to comment.