Skip to content

Commit

Permalink
Experimental async lambda implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangoacher committed Aug 22, 2012
1 parent e3c9242 commit 599ea69
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions lib/mu/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,26 @@ function _render(tokens, context, partials, stream, callback) {
case 'mustache':
switch (token[1]) {
case 'utag': // Unescaped Tag
stream.emit('data', s(normalize(context, token[2])));
return next();
var v = normalize(context, token[2], undefined, function( v ) {
stream.emit('data', s(v));
next();
});
if( v !== undefined ) {
stream.emit('data', v );
next();
}
return;

case 'etag': // Escaped Tag
stream.emit('data', escape(s(normalize(context, token[2]))));
return next();
var v = normalize(context, token[2], undefined, function( v ) {
stream.emit('data', escape(s(v)));
next();
});
if( v !== undefined ) {
stream.emit('data', escape(v));
next();
}
return;

case 'section':
var res = normalize(context, token[2], token[3]);
Expand Down Expand Up @@ -106,11 +120,15 @@ function escape(string) {
return string.replace(/[&<>"]/g, escapeReplace);
}

function normalize(context, name, body) {
function normalize(context, name, body, next) {
var val = walkToFind(context, name);

if (typeof(val) === 'function') {
val = val.call(smashContext(context), body);
var fn = val;
val = fn.call(smashContext(context), body, next);
if( val === undefined && fn.length < 2 ) { // => val doesn't accept a callback function
val = '';
}
}

return val;
Expand Down

0 comments on commit 599ea69

Please sign in to comment.