Permalink
Browse files

removed aliases

  • Loading branch information...
chjj committed Jun 15, 2011
1 parent 6ba1ad6 commit d27ddc1cb3b4478d3f82c1ef1a61596f5add6fab
Showing with 15 additions and 44 deletions.
  1. +15 −44 lib/liquor.js
View
@@ -1,55 +1,30 @@
// liquor - javascript templates
// Copyright (c) 2011, Christopher Jeffrey (MIT Licensed)
(function() {
+
var liquor = (function() {
- // for browsers that don't support `Object.keys`
- var fin = Object.keys
- ? function(obj, func, con) {
- var k = Object.keys(obj), i = 0, l = k.length;
- for (; i < l; i++) {
- if (func.call(con || obj[k[i]], obj[k[i]], k[i], obj) === false) break;
- }
- }
- : function(obj, func, con) {
- for (var k in obj) if (liquor.hasOwnProperty.call(obj, k)) {
- if (func.call(con || obj[k], obj[k], k, obj) === false) break;
+ var foreach = function(obj, func, con) {
+ if (!obj) return;
+ if (typeof obj.length === 'number' && typeof obj !== 'function') {
+ for (var i = 0, l = obj.length; i < l; i++) {
+ if (func.call(con || obj[i], obj[i], i, obj) === false) break;
}
- };
- var helpers = {
- each: function(obj, func, con) {
- if (!obj) return;
- if (typeof obj.length === 'number' && typeof obj !== 'function') {
- for (var i = 0, l = obj.length; i < l; i++) {
- if (func.call(con || obj[i], obj[i], i, obj) === false) break;
- }
- } else {
- fin(obj, func, con);
+ } else {
+ for (var k = Object.keys(obj), i = 0, l = k.length; i < l; i++) {
+ if (func.call(con || obj[k[i]], obj[k[i]], k[i], obj) === false) break;
}
}
};
+ var each = /([ \t]*)@:([^\s]+)[ \t]*([^\n]*(?:\n+\1(?:[ ]{2}|\t)[^\n]+)*)/;
+ var cond = /([ \t]*)(?:\?|(!)):([^\s]+)[ \t]*([^\n]*(?:\n+\1(?:[ ]{2}|\t)[^\n]+)*)/;
return function(str, opt) {
- // normalize newlines
- str = str.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
-
- // pre-escape all doublequotes,
- // as we are using them in our compiled code
- str = str.replace(/"/g, '\\"');
-
- // aliases - to be dropped eventually once i decide on a style
- str = str.replace(/&:([^;\n]+);|{{([^}]+)}}|[&@$%]{([^}]+)}/g, '#{$1$2$3}');
- str = str.replace(/([?!@]:[^\s:]+)(?=\s)/g, '$1:');
- str = str.replace(/[#&@](:[^\s:]+:)([ \t]*->)?/g, '@$1');
+ // normalize newlines and escape double quotes
+ str = str.replace(/\r\n/g, '\n').replace(/\r/g, '\n').replace(/"/g, '\\"');
// pre-preprocessing for shorthand notations, sig-whitespace here
- // - when we replace these with js constructs, we need to maintain
- // - the linebreaks and whitespace so the regexes continue to match
- var each = /([ \t]*)@:([^\s:]+):[ \t]*([^\n]*(?:\n+\1(?:[ ]{2}|\t)[^\n]+)*)/;
while (each.test(str)) {
str = str.replace(each, '\n$1`each($2, function(v) {`$3 $1\n$1`})`');
}
-
- // the main point of this is so you dont have to `typeof var !== 'undefined'`
- var cond = /([ \t]*)(?:\?|(!)):([^\s:]+):[ \t]*([^\n]*(?:\n+\1(?:[ ]{2}|\t)[^\n]+)*)/;
while (cond.test(str)) {
str = str.replace(cond,
'\n$1`if ($2(typeof $3 !== "undefined" && $3)) {`$4 $1\n$1`}`'
@@ -58,24 +33,20 @@ var liquor = (function() {
// evaluate
str = str.replace(/`([^`]+)`/g, '"); $1; __out.push("');
-
// interpolate
str = str.replace(/#{([^}]+)}/g, '", ($1), "');
-
// wrap
str = 'with($) { var __out = []; __out.push("'
+ str + '"); return __out.join(""); }';
-
// drop the line feeds
str = str.replace(/\n/g, '\\n');
if (opt === 'debug') return str;
var func = new Function('$', str);
return function(locals) {
- var $ = {}, k;
- for (k in helpers) $[k] = helpers[k];
- for (k in locals) $[k] = locals[k];
+ var $ = locals || {};
+ $.each = $.foreach = foreach;
return func($);
};
};

0 comments on commit d27ddc1

Please sign in to comment.