Permalink
Browse files

Actually committed filters...

  • Loading branch information...
Anders Hellerup Madsen
Anders Hellerup Madsen committed Dec 12, 2009
1 parent e9c3115 commit 4900633af14ffbf80edd1e8ba03c2a084b2add3b
Showing with 286 additions and 128 deletions.
  1. +87 −82 template_defaults.js
  2. +133 −36 template_system.js
  3. +63 −9 template_system_test.js
  4. +3 −1 templates/template.html
View
@@ -1,13 +1,93 @@
+/*jslint laxbreak: true, eqeqeq: true, undef: true, regexp: false */
+/*global require, process, exports */
+
var sys = require('sys');
var template = require('./template_system');
-exports.callbacks = {
- 'text': function (parser, token) { return TextNode(token.contents); },
+var filters = exports.filters = {
+ add: function (value, arg) { return value + arg; },
+ sub: function (value, arg) { return value - arg; }
+};
+
+
+var nodes = exports.nodes = {
+
+ TextNode: function (text) {
+ return function () { return text; };
+ },
+
+
+ VariableNode: function (filterexpression) {
+ return function (context) {
+ return filterexpression.resolve(context);
+ };
+ },
+
+ ForNode: function (itemname, listname, node_list, isReversed) {
+
+ return function (context) {
+ var forloop = { parentloop: context.get('forloop') },
+ list = context.get(listname),
+ out = '';
+
+ if (! list instanceof Array) { return nodes.TextNode(''); }
+ if (isReversed) { list = list.slice(0).reverse(); }
+
+ context.push();
+ context.set('forloop', forloop);
+
+ list.forEach( function (o, idx, iter) {
+ process.mixin(forloop, {
+ counter: idx + 1,
+ counter0: idx,
+ revcounter: iter.length - idx,
+ revcounter0: iter.length - (idx + 1),
+ first: idx === 0,
+ last: idx === iter.length - 1
+ });
+ context.set(itemname, o);
+
+ out += template.evaluate_node_list( node_list, context );
+ });
+
+ context.pop();
+
+ return out;
+ };
+ },
+
+ IfNode: function (item_names, not_item_names, operator, if_node_list, else_node_list) {
+
+ return function (context) {
+
+ function not(x) { return !x; }
+ function and(p,c) { return p && c; }
+ function or(p,c) { return p || c; }
+
+ var items = item_names.map( context.get, context ).concat(
+ not_item_names.map( context.get, context ).map( not )
+ );
+
+ var isTrue = items.reduce( operator === 'and' ? and : or, true );
+
+ if (isTrue) {
+ return template.evaluate_node_list( if_node_list, context );
+ } else if (else_node_list.length) {
+ return template.evaluate_node_list( else_node_list, context );
+ } else {
+ return '';
+ }
+ };
+ }
+
+};
+
+var callbacks = exports.callbacks = {
+ 'text': function (parser, token) { return nodes.TextNode(token.contents); },
'variable': function (parser, token) {
- // TODO: use split_token here
- return VariableNode(token.contents[0], token.contents.slice(1));
+ return nodes.VariableNode( new template.FilterExpression(token.contents) );
},
'for': function (parser, token) {
@@ -25,7 +105,7 @@ exports.callbacks = {
parser.delete_first_token();
- return ForNode(itemname, listname, node_list, isReversed);
+ return nodes.ForNode(itemname, listname, node_list, isReversed);
},
'if': function (parser, token) {
@@ -57,7 +137,7 @@ exports.callbacks = {
if (p !== 'and' && p !== 'or') { throw 'unexpected syntax in "if" tag. Expected "and" or "or"'; }
if (operator && p !== operator) { throw 'unexpected syntax in "if" tag. Cannot mix "and" and "or"'; }
operator = p;
- expect_item = true;
+ next_should_be_item = true;
}
}
@@ -70,81 +150,6 @@ exports.callbacks = {
parser.delete_first_token();
- return IfNode(item_names, not_item_names, operator, node_list, else_list);
+ return nodes.IfNode(item_names, not_item_names, operator, node_list, else_list);
}
};
-
-function TextNode(text) {
- return function () { return text; }
-}
-exports.TextNode = TextNode;
-
-
-function VariableNode(name, filters) {
-
- // TODO: Filters
- return function (context) { return context.get(name); }
-}
-exports.VariableNode = VariableNode;
-
-
-function ForNode(itemname, listname, node_list, isReversed) {
-
- return function (context) {
- var forloop = { parentloop: context.get('forloop') },
- list = context.get(listname),
- out = '';
-
-
- if (! list instanceof Array) { return TextNode(''); }
- if (isReversed) { list = list.slice(0).reverse(); }
-
- context.push();
- context.set('forloop', forloop);
-
- list.forEach( function (o, idx, iter) {
- process.mixin(forloop, {
- counter: idx + 1,
- counter0: idx,
- revcounter: iter.length - idx,
- revcounter0: iter.length - (idx + 1),
- first: idx === 0,
- last: idx === iter.length - 1,
- });
- context.set(itemname, o);
-
- out += template.evaluate_node_list( node_list, context );
- });
-
- context.pop();
-
- return out;
- };
-}
-exports.ForNode = ForNode;
-
-function IfNode(item_names, not_item_names, operator, if_node_list, else_node_list) {
-
- return function (context) {
-
- function not(x) { return !x; }
- function and(p,c) { return p && c; }
- function or(p,c) { return p || c; }
-
- var items = item_names.map( context.get, context ).concat(
- not_item_names.map( context.get, context ).map( not )
- );
-
- var isTrue = items.reduce( operator === 'and' ? and : or, true );
-
- if (isTrue) {
- return template.evaluate_node_list( if_node_list, context );
- } else if (else_node_list.length) {
- return template.evaluate_node_list( else_node_list, context );
- } else {
- return '';
- }
- };
-}
-exports.IfNode = IfNode;
-
Oops, something went wrong.

0 comments on commit 4900633

Please sign in to comment.