Permalink
Browse files

Add filters

when using the syntax <%= epression | filter | filter %> filters are
called in this order. a filter function has this prototype
function(string) where is string is the result of the expression. It
should return a string.
one can register a filter by calling blue.registerFilter(name, func)
where name is the name of the filter and func, the function to be called
  • Loading branch information...
1 parent ad794df commit 9222485c8b3cb4d01f20452af16fdf139156d9e1 @Floby committed Oct 22, 2010
Showing with 52 additions and 5 deletions.
  1. +0 −1 TODO
  2. +1 −1 example/main.tpl
  3. +2 −0 lib/blue.js
  4. +47 −1 lib/compile.js
  5. +2 −2 lib/template.js
View
1 TODO
@@ -1,6 +1,5 @@
- Fully implement the Stream interface for the Template class
- configuration of <% %> some people seem to not like it =)
- caching, then smart caching, you know what, let's go to the smart one directly
-- add extendable filters
- add support for functions with callback and streams
- Add tests and benchmarks
View
@@ -3,7 +3,7 @@
<title>Coucou</title>
</head>
<body>
- <%= "oui" %>
+ <%= "OUI" | lowercase %>
<%= data.require('sys') %>
<%@ "paragraphe.tpl" %>
<%@ "div.tpl" %>
View
@@ -1 +1,3 @@
+exports.registerFilter = require('./compile').registerFilter;
exports.Template = require('./template');
+exports.version = '0.1.0';
View
@@ -1,6 +1,39 @@
+var sys = require ('sys')
+
+function substitute_affectation(str, p1, offset, s) {
+ //console.log(sys.inspect(p1))
+ var split = p1.split('|');
+ var str = split.shift();
+ if(split.length == 0) {
+ console.log('no length');
+ return '<%print('+ str +');%>';
+ }
+ var functions = [];
+ var parenthesis = '';
+
+ var filters = compile_template._filters;
+ for (var i = 0; i < split.length; i++) {
+ var name = split[i].trim();
+ if(name == '') continue;
+ if(filters[name]) {
+ functions.push('filters.'+name);
+ parenthesis += ')';
+ }
+ };
+ var result = "<% print("
+ + functions.join('(')
+ + '('
+ + str
+ + parenthesis
+ + ');%>';
+ //sys.debug(result);
+ return result;
+}
+
function compile_template(template) {
// simple substitution from <%= /* */ %> forms to <% echo() %>
- var template = template.replace(/<%=(.*)%>/g, "<% print($1); %>");
+ //template = template.replace(/<%=(.*)%>/g, "<% print($1); %>");
+ template = template.replace(/<%=(.*)%>/g, substitute_affectation);
// substition for includes
template = template.replace(/<%@(.*)%>/g, "<% include($1); %>");
var result = ""; ///< result string
@@ -75,3 +108,16 @@ function compile_template(template) {
//module.exports = process_template;
module.exports = compile_template;
+compile_template._filters = {};
+compile_template.registerFilter = function(name, func) {
+ console.log(typeof(func));
+ if(!/^[a-z0-9A-Z_]+$/.test(name))
+ throw new Error("filter name should be [a-zA-Z0-9_]+");
+ if(typeof(func) !== 'function')
+ throw new TypeError("no function given when registering filter "+name);
+
+ compile_template._filters[name] = func;
+};
+compile_template.registerFilter('lowercase', function(str) {
+ return str.toLowerCase();
+});
View
@@ -111,7 +111,7 @@ Template.prototype.run = function run() {
return false;
}
// to remove
- var tpl = "(function compiled_template(print, include, data) {" + tpl + "})";
+ var tpl = "(function compiled_template(print, include, data, filters) {" + tpl + "})";
try {
var script = new Script(tpl, self.filename);
} catch(e) {
@@ -128,7 +128,7 @@ Template.prototype.run = function run() {
var include = make_include(self); // same, see line 66
try {
// actually run the compiled template
- f(print, include, self._data);
+ f(print, include, self._data, process_template._filters);
} catch(e) {
throw(e);
self.emit('error', e);

0 comments on commit 9222485

Please sign in to comment.