Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

experimental pragma support

{%esc:j}{foo}{/esc}
  • Loading branch information...
commit 98ce166ae555612942c23c0579b3569fd9d92518 1 parent 17bc895
akdubya authored
Showing with 60 additions and 28 deletions.
  1. +48 −9 lib/compiler.js
  2. +10 −17 lib/dust.js
  3. +2 −2 src/dust.pegjs
View
57 lib/compiler.js
@@ -26,6 +26,7 @@ dust.optimizers = {
"<": visit,
"+": visit,
"@": visit,
+ "%": visit,
partial: visit,
context: visit,
params: visit,
@@ -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++) {
@@ -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;
},
@@ -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])
@@ -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) {
View
27 lib/dust.js
@@ -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) {
@@ -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
}
View
4 src/dust.pegjs
@@ -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"
@@ -98,7 +98,7 @@ comment "comment"
{ return ["comment", c.join('')] }
tag
- = ld [#?^><+%:@/~] (!rd !eol .)+ rd
+ = ld [#?^><+%:@/~%] (!rd !eol .)+ rd
/ reference
ld
Please sign in to comment.
Something went wrong with that request. Please try again.