Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

improve liquor minimal again

  • Loading branch information...
commit 31a00f9d1beee2b22ed22204c2830ff5a034a021 1 parent 4881d37
@chjj authored
Showing with 69 additions and 57 deletions.
  1. +6 −2 lib/liquor.js
  2. +63 −55 lib/liquor_minimal.js
View
8 lib/liquor.js
@@ -36,8 +36,9 @@ var each = function(obj, func) {
if (typeof l === 'number' && typeof obj !== 'function') {
for (; i < l; i++) {
- if (func.call(obj[i], obj[i], i, obj) === false)
+ if (func.call(obj[i], obj[i], i, obj) === false) {
break;
+ }
}
} else {
var keys = Object.keys(obj)
@@ -46,8 +47,9 @@ var each = function(obj, func) {
for (; i < l; i++) {
key = keys[i];
- if (func.call(obj[key], obj[key], key, obj) === false)
+ if (func.call(obj[key], obj[key], key, obj) === false) {
break;
+ }
}
}
};
@@ -63,3 +65,5 @@ if (typeof module !== 'undefined' && module.exports) {
} else {
this.liquor = liquor;
}
+
+module.exports = require('./liquor_minimal');
View
118 lib/liquor_minimal.js
@@ -19,8 +19,9 @@ var liquor = (function() {
if (typeof l === 'number' && typeof obj !== 'function') {
for (; i < l; i++) {
- if (func.call(obj[i], obj[i], i, obj) === false)
+ if (func.call(obj[i], obj[i], i, obj) === false) {
break;
+ }
}
} else {
var keys = Object.keys(obj)
@@ -29,76 +30,83 @@ var liquor = (function() {
for (; i < l; i++) {
key = keys[i];
- if (func.call(obj[key], obj[key], key, obj) === false)
+ if (func.call(obj[key], obj[key], key, obj) === false) {
break;
+ }
}
}
};
- var iterate = /( *)@:([^\s]+) *([^\n]*(?:\n+\1 {2}[^\n]+)*)/
- , condition = /( *)(?:\?|(!)):([^\s]+) *([^\n]*(?:\n+\1 {2}[^\n]+)*)/;
+ var rules = {
+ iterate: /^( *)@:([^\s]+) *([^\n]*(?:\n+\1 {2}[^\n]+)*)/,
+ condition: /^( *)(?:\?|(!)):([^\s]+) *([^\n]*(?:\n+\1 {2}[^\n]+)*)/,
+ evaluate: /^`([^`]+)`/,
+ interpolate: /^#{([^}]+)}/,
+ text: /^[^\0]+?(?= *@:| *\?:| *!:|`|#{|$)/
+ };
- return function(str, opt) {
- var cap;
+ function lexer(str) {
+ var cap, out = '';
+ while (str) {
+ if (cap) str = str.substring(cap[0].length);
+ if (cap = rules.iterate.exec(str)) {
+ out += '"); each('
+ + cap[2]
+ + ', function(v) { __out.push("'
+ + lexer(cap[3])
+ + '"); }); __out.push("';
+ continue;
+ }
+ if (cap = rules.condition.exec(str)) {
+ out += '"); if ('
+ + (cap[2] || '')
+ + '(typeof '
+ + cap[3]
+ + ' !== "undefined" && '
+ + cap[3]
+ + ')){ __out.push("'
+ + lexer(cap[4])
+ + '"); } __out.push("';
+ continue;
+ }
+ if (cap = rules.evaluate.exec(str)) {
+ out += '"); '
+ + cap[1]
+ + '; __out.push("';
+ continue;
+ }
+ if (cap = rules.interpolate.exec(str)) {
+ out += '", ('
+ + cap[1]
+ + '), "';
+ continue;
+ }
+ if (cap = rules.text.exec(str)) {
+ out += cap[0]
+ .replace(/"/g, '\\"')
+ .replace(/\n/g, '\\n');
+ continue;
+ }
+ if (str) {
+ throw new
+ Error('Liquor: Error. Please report this as an issue.');
+ }
+ }
+ return out;
+ }
+ return function(str, opt) {
// normalize whitespace
// escape double quotes
str = str
.replace(/\r\n|\r/g, '\n')
- .replace(/\t/g, ' ')
- .replace(/"/g, '\\"');
-
- // pre-preprocessing for shorthand
- // notations and sig-whitespace here
- while (cap = iterate.exec(str)) {
- str = str.substring(0, cap.index)
- + '\n'
- + cap[1]
- + '`each('
- + cap[2]
- + ', function(v) {`'
- + cap[3]
- + ' '
- + cap[1]
- + '\n'
- + cap[1]
- + '`})`'
- + str.substring(cap.index + cap[0].length);
- }
-
- while (cap = condition.exec(str)) {
- str = str.substring(0, cap.index)
- + '\n'
- + cap[1]
- + '`if ('
- + (cap[2] || '')
- + '(typeof '
- + cap[3]
- + ' !== "undefined" && '
- + cap[3]
- + ')){`'
- + cap[4]
- + ' '
- + cap[1]
- + '\n'
- + cap[1]
- + '`}`'
- + str.substring(cap.index + cap[0].length);
- }
-
- // evaluate and interpolate
- str = str
- .replace(/`([^`]+)`/g, '"); $1; __out.push("')
- .replace(/#{([^}]+)}/g, '", ($1), "');
+ .replace(/\t/g, ' ');
// wrap
str = 'with ($) { var __out = []; __out.push("'
- + str
+ + lexer(str)
+ '"); return __out.join(""); }';
- // drop the line feeds
- str = str.replace(/\n/g, '\\n');
-
if (opt === 'debug') return str;
var func = new Function('$, each', str);
Please sign in to comment.
Something went wrong with that request. Please try again.