Permalink
Browse files

runtime.js make targets

  • Loading branch information...
1 parent e5c4623 commit 74e1344339a96e513400f223ccfe4fa66e86837f @tj tj committed Jul 15, 2011
Showing with 169 additions and 53 deletions.
  1. +12 −0 Makefile
  2. +54 −52 jade.js
  3. +1 −1 jade.min.js
  4. +95 −0 runtime.js
  5. +1 −0 runtime.min.js
  6. +4 −0 support/foot.js
  7. +2 −0 support/head.js
View
@@ -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 \
@@ -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
View
106 jade.js
@@ -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';
}
@@ -758,11 +759,59 @@ 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.
*
* Options:
@@ -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);
}
};
@@ -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;
@@ -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;')
@@ -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){
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -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;
+
+})();
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -0,0 +1,4 @@
+
+ return exports;
+
+})();
View
@@ -0,0 +1,2 @@
+
+var jade = (function(){

0 comments on commit 74e1344

Please sign in to comment.