Skip to content

Commit

Permalink
runtime.js make targets
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Jul 15, 2011
1 parent e5c4623 commit 74e1344
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 53 deletions.
12 changes: 12 additions & 0 deletions Makefile
Expand Up @@ -3,6 +3,8 @@ TESTS = test/*.js
SRC = $(shell find lib -name "*.js" -type f)
UGLIFY_FLAGS = --no-mangle

all: jade.min.js runtime.min.js

test:
@./node_modules/.bin/expresso \
-I node_modules \
Expand All @@ -23,8 +25,18 @@ jade.min.js: jade.js
&& du jade.min.js \
&& du jade.js

runtime.js: lib/runtime.js
@cat support/head.js $< support/foot.js > $@

runtime.min.js: runtime.js
@uglifyjs $(UGLIFY_FLAGS) $< > $@ \
&& du runtime.min.js \
&& du runtime.js

clean:
rm -f jade.js
rm -f jade.min.js
rm -f runtime.js
rm -f runtime.min.js

.PHONY: test benchmark clean
106 changes: 54 additions & 52 deletions jade.js
Expand Up @@ -725,7 +725,8 @@ function parse(str, options){
, inlined = '';

if (inline) {
inlined = attrs.toString() + '\n' + escape.toString() + '\n';
inlined += runtime.attrs.toString() + '\n';
inlined += runtime.escape.toString() + '\n';
} else {
inlined = 'var attrs = jade.attrs, escape = jade.escape;\n';
}
Expand Down Expand Up @@ -758,10 +759,58 @@ function parse(str, options){
return;
}
} catch (err) {
rethrow(err, str, filename, parser.lexer.lineno);
runtime.rethrow(err, str, filename, parser.lexer.lineno);
}
}

/**
* Compile a `Function` representation of the given jade `str`.
*
* Options:
*
* - `compileDebug` when `false` debugging code is stripped from the compiled template
* - `inline` when `false` helpers are not inlined
*
* @param {String} str
* @param {Options} options
* @return {Function}
* @api public
*/

exports.compile = function(str, options){
var options = options || {}
, input = JSON.stringify(str)
, inline = false !== options.inline
, filename = options.filename
? JSON.stringify(options.filename)
: 'undefined'
, inlined = ''
, fn;

if (inline) {
inlined = runtime.rethrow.toString();
} else {
inlined = 'var rethrow = jade.rethrow;';
}

if (options.compileDebug !== false) {
// Reduce closure madness by injecting some locals
fn = [
'var __ = { lineno: 1, input: ' + input + ', filename: ' + filename + ' };'
, inlined
, 'try {'
, parse(String(str), options || {})
, '} catch (err) {'
, ' rethrow(err, __.input, __.filename, __.lineno);'
, '}'
].join('\n');
} else {
fn = parse(String(str), options || {});
}

return new Function('locals', fn);
};

/**
* Render the given `str` of jade.
*
Expand Down Expand Up @@ -810,7 +859,7 @@ exports.render = function(str, options){
locals.__ = meta;
return fn.call(options.scope, locals);
} catch (err) {
rethrow(err, str, filename, meta.lineno);
runtime.rethrow(err, str, filename, meta.lineno);
}
};

Expand Down Expand Up @@ -2405,7 +2454,7 @@ require.register("runtime.js", function(module, exports, require){
* @api private
*/

exports.attrs = function(obj){
exports.attrs = function attrs(obj){
var buf = []
, terse = obj.terse;
delete obj.terse;
Expand Down Expand Up @@ -2440,7 +2489,7 @@ exports.attrs = function(obj){
* @api private
*/

exports.escape = function(html){
exports.escape = function escape(html){
return String(html)
.replace(/&(?!\w+;)/g, '&amp;')
.replace(/</g, '&lt;')
Expand Down Expand Up @@ -2481,53 +2530,6 @@ exports.rethrow = function rethrow(err, str, filename, lineno){
throw err;
};

/**
* Compile a `Function` representation of the given jade `str`.
*
* Options:
*
* - `compileDebug` when `false` debugging code is stripped from the compiled template
* - `inline` when `false` helpers are not inlined
*
* @param {String} str
* @param {Options} options
* @return {Function}
* @api public
*/

exports.compile = function(str, options){
var options = options || {}
, input = JSON.stringify(str)
, inline = false !== options.inline
, filename = options.filename
? JSON.stringify(options.filename)
: 'undefined'
, inlined = ''
, fn;

if (inline) {
inlined = rethrow.toString();
} else {
inlined = 'var rethrow = jade.rethrow;';
}

if (options.compileDebug !== false) {
// Reduce closure madness by injecting some locals
fn = [
'var __ = { lineno: 1, input: ' + input + ', filename: ' + filename + ' };'
, inlined
, 'try {'
, parse(String(str), options || {})
, '} catch (err) {'
, ' rethrow(err, __.input, __.filename, __.lineno);'
, '}'
].join('\n');
} else {
fn = parse(String(str), options || {});
}

return new Function('locals', fn);
};
}); // module: runtime.js

require.register("self-closing.js", function(module, exports, require){
Expand Down
2 changes: 1 addition & 1 deletion jade.min.js

Large diffs are not rendered by default.

95 changes: 95 additions & 0 deletions runtime.js
@@ -0,0 +1,95 @@

var jade = (function(){
/*!
* Jade - runtime
* Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
* MIT Licensed
*/

/**
* Render the given attributes object.
*
* @param {Object} obj
* @return {String}
* @api private
*/

exports.attrs = function attrs(obj){
var buf = []
, terse = obj.terse;
delete obj.terse;
var keys = Object.keys(obj)
, len = keys.length;
if (len) {
buf.push('');
for (var i = 0; i < len; ++i) {
var key = keys[i]
, val = obj[key];
if ('boolean' == typeof val || null == val) {
if (val) {
terse
? buf.push(key)
: buf.push(key + '="' + key + '"');
}
} else if ('class' == key && Array.isArray(val)) {
buf.push(key + '="' + escape(val.join(' ')) + '"');
} else {
buf.push(key + '="' + escape(val) + '"');
}
}
}
return buf.join(' ');
};

/**
* Escape the given string of `html`.
*
* @param {String} html
* @return {String}
* @api private
*/

exports.escape = function escape(html){
return String(html)
.replace(/&(?!\w+;)/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;');
};

/**
* 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
*/

exports.rethrow = function rethrow(err, str, filename, lineno){
var context = 3
, lines = str.split('\n')
, start = Math.max(lineno - context, 0)
, end = Math.min(lines.length, lineno + context);

// Error context
var context = lines.slice(start, end).map(function(line, i){
var curr = i + start + 1;
return (curr == lineno ? ' > ' : ' ')
+ curr
+ '| '
+ line;
}).join('\n');

// Alter exception message
err.path = filename;
err.message = (filename || 'Jade') + ':' + lineno
+ '\n' + context + '\n\n' + err.message;
throw err;
};

return exports;

})();
1 change: 1 addition & 0 deletions runtime.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions support/foot.js
@@ -0,0 +1,4 @@

return exports;

})();
2 changes: 2 additions & 0 deletions support/head.js
@@ -0,0 +1,2 @@

var jade = (function(){

0 comments on commit 74e1344

Please sign in to comment.