Permalink
Browse files

refactor, rename variables for consistency

  • Loading branch information...
1 parent 31a00f9 commit 37f969ce3a102a179073f406544f6dd174d7adb2 @chjj committed Mar 7, 2012
Showing with 38 additions and 40 deletions.
  1. +15 −15 lib/lexer.js
  2. +5 −7 lib/liquor.js
  3. +17 −17 lib/liquor_minimal.js
  4. +1 −1 lib/parser.js
View
30 lib/lexer.js
@@ -1,11 +1,11 @@
/**
* Liquor - Lexer
- * Copyright (c) 2011, Christopher Jeffrey. (MIT Licensed)
+ * Copyright (c) 2011-2012, Christopher Jeffrey. (MIT Licensed)
*/
-var lexer = function(str, options) {
+var lexer = function(src, options) {
var i = 0
- , l = str.length
+ , l = src.length
, ch
, buff = ''
, key
@@ -19,8 +19,8 @@ var lexer = function(str, options) {
, indentSize;
if (options.pretty && options.indent) {
- str = applyIndent(str, options.indent);
- l = str.length;
+ src = applyIndent(src, options.indent);
+ l = src.length;
}
var state = function() {
@@ -35,7 +35,7 @@ var lexer = function(str, options) {
};
for (; i < l; i++) {
- ch = str[i];
+ ch = src[i];
offset++;
if (ch > ' ' && newline) {
@@ -57,7 +57,7 @@ var lexer = function(str, options) {
switch(ch) {
case '\r':
- if (str[i+1] === '\n') break;
+ if (src[i+1] === '\n') break;
;
case '\n':
line++;
@@ -90,7 +90,7 @@ var lexer = function(str, options) {
case '@':
case '?':
case '!':
- if (str[i+1] === ':' && !buff.trim()) {
+ if (src[i+1] === ':' && !buff.trim()) {
switch (ch) {
case '@':
stack.push('iterate');
@@ -135,7 +135,7 @@ var lexer = function(str, options) {
}
break;
case '#':
- if (str[i+1] === '{') {
+ if (src[i+1] === '{') {
switch (state()) {
case 'interpolate':
case 'evaluate':
@@ -203,16 +203,16 @@ var lexer = function(str, options) {
return tokens;
};
-var applyOutdent = function(str, n) {
- if (!n) return str;
- return str.replace(new RegExp('^[ \t]{' + n + '}', 'gm'), '');
+var applyOutdent = function(src, n) {
+ if (!n) return src;
+ return src.replace(new RegExp('^[ \t]{' + n + '}', 'gm'), '');
};
-var applyIndent = function(str, n) {
- if (!n) return str;
+var applyIndent = function(src, n) {
+ if (!n) return src;
var s = '';
while (n--) s += ' ';
- return str.replace(/^/gm, s);
+ return src.replace(/^/gm, s);
};
/**
View
12 lib/liquor.js
@@ -1,7 +1,7 @@
/**
* Liquor (https://github.com/chjj/liquor)
* Javascript templates minus the code.
- * Copyright (c) 2011, Christopher Jeffrey. (MIT Licensed)
+ * Copyright (c) 2011-2012, Christopher Jeffrey. (MIT Licensed)
*/
var lexer = require('./lexer')
@@ -11,14 +11,14 @@ var lexer = require('./lexer')
* Compile
*/
-var liquor = function(text, options) {
+var liquor = function(src, options) {
options = options || {};
- text = parser(lexer(text, options), options);
+ src = parser(lexer(src, options), options);
- if (options === 'debug') return text;
+ if (options === 'debug') return src;
- var func = new Function('$, each', text);
+ var func = new Function('$, each', src);
return function(locals) {
return func(locals || {}, each);
};
@@ -65,5 +65,3 @@ if (typeof module !== 'undefined' && module.exports) {
} else {
this.liquor = liquor;
}
-
-module.exports = require('./liquor_minimal');
View
34 lib/liquor_minimal.js
@@ -1,7 +1,7 @@
/**
* Liquor (https://github.com/chjj/liquor)
- * Javascript templates minus the code.
- * Copyright (c) 2011, Christopher Jeffrey. (MIT Licensed)
+ * A minimal version of liquor, suitable for the client-side.
+ * Copyright (c) 2011-2012, Christopher Jeffrey. (MIT Licensed)
*/
;(function() {
@@ -45,19 +45,19 @@ var liquor = (function() {
text: /^[^\0]+?(?= *@:| *\?:| *!:|`|#{|$)/
};
- function lexer(str) {
+ function lexer(src) {
var cap, out = '';
- while (str) {
- if (cap) str = str.substring(cap[0].length);
- if (cap = rules.iterate.exec(str)) {
+ while (src) {
+ if (cap) src = src.substring(cap[0].length);
+ if (cap = rules.iterate.exec(src)) {
out += '"); each('
+ cap[2]
+ ', function(v) { __out.push("'
+ lexer(cap[3])
+ '"); }); __out.push("';
continue;
}
- if (cap = rules.condition.exec(str)) {
+ if (cap = rules.condition.exec(src)) {
out += '"); if ('
+ (cap[2] || '')
+ '(typeof '
@@ -69,47 +69,47 @@ var liquor = (function() {
+ '"); } __out.push("';
continue;
}
- if (cap = rules.evaluate.exec(str)) {
+ if (cap = rules.evaluate.exec(src)) {
out += '"); '
+ cap[1]
+ '; __out.push("';
continue;
}
- if (cap = rules.interpolate.exec(str)) {
+ if (cap = rules.interpolate.exec(src)) {
out += '", ('
+ cap[1]
+ '), "';
continue;
}
- if (cap = rules.text.exec(str)) {
+ if (cap = rules.text.exec(src)) {
out += cap[0]
.replace(/"/g, '\\"')
.replace(/\n/g, '\\n');
continue;
}
- if (str) {
+ if (src) {
throw new
Error('Liquor: Error. Please report this as an issue.');
}
}
return out;
}
- return function(str, opt) {
+ return function(src, opt) {
// normalize whitespace
// escape double quotes
- str = str
+ src = src
.replace(/\r\n|\r/g, '\n')
.replace(/\t/g, ' ');
// wrap
- str = 'with ($) { var __out = []; __out.push("'
- + lexer(str)
+ src = 'with ($) { var __out = []; __out.push("'
+ + lexer(src)
+ '"); return __out.join(""); }';
- if (opt === 'debug') return str;
+ if (opt === 'debug') return src;
- var func = new Function('$, each', str);
+ var func = new Function('$, each', src);
return function(locals) {
return func(locals || {}, each);
};
View
2 lib/parser.js
@@ -1,6 +1,6 @@
/**
* Liquor - Parser
- * Copyright (c) 2011, Christopher Jeffrey. (MIT Licensed)
+ * Copyright (c) 2011-2012, Christopher Jeffrey. (MIT Licensed)
*/
var parser = (function() {

0 comments on commit 37f969c

Please sign in to comment.