Broke large embeddeds out into the scoped preface, hopefully will make future language specificity easier.

This commit is contained in:
Elf M. Sternberg 2013-04-17 12:46:37 -07:00
parent 80c3c6f834
commit f8e6eb7003
1 changed files with 63 additions and 44 deletions

View File

@ -1,6 +1,8 @@
{ {
var _ = require('underscore'); var _ = require('underscore'),
var depth = 0; depth = 0,
parts, variable, conditional, descendant, iterative, sections;
var Contexter = function(c) { var Contexter = function(c) {
this.content = c this.content = c
@ -78,16 +80,60 @@
} }
}); });
sections = function(ps, content) {
return _.map(ps, function(p) { return p(content); }).join("");
}
parts = function(ps) {
return function(content) {
var context = new Contexter(content);
return sections(ps, context);
}
};
text = function(ps) {
var t = ps.join("");
return function(content) {
return t;
};
};
variable = function(t) {
return function(content) {
return content.get(t, "");
};
};
// TODO: Yeah, there's a code smell below.
conditional = function(t, ps) {
return function(content) {
return content.if(t, function(c) {
return sections(ps, content);
});
}
};
descendant = function(t, ps) {
return function(content) {
return content.descend(t, function(c) {
return sections(ps, content);
});
}
};
iterative = function(t, ps) {
return function(content) {
return content.many(t, function(c) {
return sections(ps, content);
});
}
}
} }
document document
= ps:document_part* { = ps:document_part* {
return function(content) { return parts(ps);
var context = new Contexter(content);
return _.map(ps, function(p) {
return p(context);
}).join("");
}
} }
@ -96,19 +142,14 @@ document_part
text text
= b:(!tag c:. {return c})+ { = bs:(!tag c:. {return c})+ {
return (function() { return text(bs);
var t = b.join("");
return function(content) {
return t;
}
}());
} }
variable "variable" variable "variable"
= t:tag_start rd { = t:tag_start rd {
return function(content) { return content.get(t, ""); }; return variable(t);
} }
@ -119,15 +160,7 @@ simple_part
conditional conditional
= t:ifblock_tag_start ps:simple_part* n:ifblock_tag_end = t:ifblock_tag_start ps:simple_part* n:ifblock_tag_end
&{ return (t == n) } &{ return (t == n) }
{ { return conditional(t, ps); }
return function(content) {
return content.if(t, function(c) {
return _.map(ps, function(p) {
return p(c);
}).join('');
});
}
}
ifblock_tag_start "tag_start" ifblock_tag_start "tag_start"
@ -141,17 +174,9 @@ ifblock_tag_end
descendant descendant
= n:blockblock_tag_start ps:simple_part* t:blockblock_tag_end = t:blockblock_tag_start ps:simple_part* n:blockblock_tag_end
&{ return (t == n) } &{ return (t == n) }
{ { return descendant(t, ps); }
return function(content) {
return content.descend(t, function(c) {
return _.map(ps, function(p) {
return p(c);
}).join('');
});
}
}
blockblock_tag_start blockblock_tag_start
@ -166,15 +191,9 @@ blockblock_tag_end
iterative iterative
= t:loopblock_tag_start ps:simple_part* n:loopblock_tag_end = t:loopblock_tag_start ps:simple_part* n:loopblock_tag_end
&{ return t == n } &{ return (t == n) }
{ {
return function(content) { return iterative(t, ps);
return content.many(t, function(c) {
return _.map(ps, function(p) {
return p(c);
}).join('');
});
}
} }
loopblock_tag_start "tag_start" loopblock_tag_start "tag_start"