Skip to content

Commit

Permalink
experimental pragma support
Browse files Browse the repository at this point in the history
{%esc:j}{foo}{/esc}
  • Loading branch information
akdubya committed Jan 26, 2011
1 parent 17bc895 commit 98ce166
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 28 deletions.
57 changes: 48 additions & 9 deletions lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dust.optimizers = {
"<": visit,
"+": visit,
"@": visit,
"%": visit,
partial: visit,
context: visit,
params: visit,
Expand All @@ -38,6 +39,17 @@ dust.optimizers = {
comment: nullify
}

dust.pragmas = {
esc: function(compiler, context, bodies, params) {
var old = compiler.auto;
if (!context) context = 'h';
compiler.auto = (context === 's') ? '' : context;
var out = compileParts(compiler, bodies.block);
compiler.auto = old;
return out;
}
}

function visit(context, node) {
var out = [node[0]];
for (var i=1, len=node.length; i<len; i++) {
Expand Down Expand Up @@ -126,20 +138,22 @@ function compileBodies(context) {
return out.join('');
}

function compileParts(context, body) {
var parts = '';
for (var i=1, len=body.length; i<len; i++) {
parts += dust.compileNode(context, body[i]);
}
return parts;
}

dust.compileNode = function(context, node) {
return dust.nodes[node[0]](context, node);
}

dust.nodes = {
body: function(context, node) {
var id = context.index++,
name = "body_" + id,
parts = '';

for (var i=1, len=node.length; i<len; i++) {
parts += dust.compileNode(context, node[i]);
}
context.bodies[id] = parts;
var id = context.index++, name = "body_" + id;
context.bodies[id] = compileParts(context, node);
return name;
},

Expand Down Expand Up @@ -199,6 +213,30 @@ dust.nodes = {
+ ")";
},

"%": function(context, node) {
// TODO: Move these hacks into pragma precompiler
var name = node[1][1];
if (!dust.pragmas[name]) return '';

var rawBodies = node[4];
var bodies = {};
for (var i=1, len=rawBodies.length; i<len; i++) {
var b = rawBodies[i];
bodies[b[1][1]] = b[2];
}

var rawParams = node[3];
var params = {};
for (var i=1, len=rawParams.length; i<len; i++) {
var p = rawParams[i];
params[p[1][1]] = p[2][1];
}

var ctx = node[2][1] ? node[2][1].text : null;

return dust.pragmas[name](context, ctx, bodies, params);
},

partial: function(context, node) {
return ".partial("
+ dust.compileNode(context, node[1])
Expand Down Expand Up @@ -241,7 +279,8 @@ dust.nodes = {
var filter = node[i];
list.push("\"" + filter + "\"");
}
return "\"" + context.auto + "\",[" + list.join(',') + "]";
return "\"" + context.auto + "\""
+ (list.length ? ",[" + list.join(',') + "]" : '');
},

key: function(context, node) {
Expand Down
27 changes: 10 additions & 17 deletions lib/dust.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ dust.isEmpty = function(value) {
};

dust.filter = function(string, auto, filters) {
var len = filters.length;

for (var i=0; i<len; i++) {
var name = filters[i];
if (name === "s") {
auto = null;
} else {
string = dust.filters[name](string);
if (filters) {
for (var i=0, len=filters.length; i<len; i++) {
var name = filters[i];
if (name === "s") {
auto = null;
} else {
string = dust.filters[name](string);
}
}
}
if (auto) {
Expand All @@ -95,16 +95,9 @@ dust.filter = function(string, auto, filters) {
};

dust.filters = {
h: function(value) {
return dust.escapeHtml(value);
},

j: function(value) {
return dust.escapeJs(value);
},

h: function(value) { return dust.escapeHtml(value); },
j: function(value) { return dust.escapeJs(value); },
u: encodeURI,

uc: encodeURIComponent
}

Expand Down
4 changes: 2 additions & 2 deletions src/dust.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ section "section"
{ t.push(["bodies"]); return t }

sec_tag_start
= ld t:[#?^<+@] n:identifier c:context p:params
= ld t:[#?^<+@%] n:identifier c:context p:params
{ return [t, n, c, p] }

end_tag "end tag"
Expand Down Expand Up @@ -98,7 +98,7 @@ comment "comment"
{ return ["comment", c.join('')] }

tag
= ld [#?^><+%:@/~] (!rd !eol .)+ rd
= ld [#?^><+%:@/~%] (!rd !eol .)+ rd
/ reference

ld
Expand Down

0 comments on commit 98ce166

Please sign in to comment.