Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update dust.js to 1.0.0(LinkedIn version)

  • Loading branch information...
commit 4e01493c3047ae96fbb748850b7601e882c7bac7 1 parent c12b5e2
@thefron authored
View
2  lib/dust-rails/rails/dust_template.rb
@@ -6,7 +6,7 @@ module Rails
module Source
def self.path
- @path ||= File.expand_path('../../../../vendor/assets/javascripts/dust-full-for-compile.js', __FILE__)
+ @path ||= File.expand_path('../../../../vendor/dustjs/lib/dust.js', __FILE__)
end
def self.contents
View
343 vendor/assets/javascripts/dust-core.js
@@ -1,5 +1,5 @@
//
-// Dust - Asynchronous Templating v0.3.0
+// Dust - Asynchronous Templating v1.0.0
// http://akdubya.github.com/dustjs
//
// Copyright (c) 2010, Aleksander Williams
@@ -8,6 +8,12 @@
var dust = {};
+function getGlobal(){
+ return (function(){
+ return this.dust;
+ }).call(null);
+}
+
(function(dust) {
dust.cache = {};
@@ -75,9 +81,15 @@ if (Array.isArray) {
};
}
-dust.nextTick = function(callback) {
- setTimeout(callback, 0);
-}
+dust.nextTick = (function() {
+ if (typeof process !== "undefined") {
+ return process.nextTick;
+ } else {
+ return function(callback) {
+ setTimeout(callback,0);
+ }
+ }
+} )();
dust.isEmpty = function(value) {
if (dust.isArray(value) && !value.length) return true;
@@ -106,8 +118,10 @@ dust.filters = {
h: function(value) { return dust.escapeHtml(value); },
j: function(value) { return dust.escapeJs(value); },
u: encodeURI,
- uc: encodeURIComponent
-}
+ uc: encodeURIComponent,
+ js: function(value) { if (!JSON) { return value; } return JSON.stringify(value); },
+ jp: function(value) { if (!JSON) { return value; } return JSON.parse(value); }
+};
function Context(stack, global, blocks) {
this.stack = stack;
@@ -117,14 +131,14 @@ function Context(stack, global, blocks) {
dust.makeBase = function(global) {
return new Context(new Stack(), global);
-}
+};
Context.wrap = function(context) {
if (context instanceof Context) {
return context;
}
return new Context(new Stack(context));
-}
+};
Context.prototype.get = function(key) {
var ctx = this.stack, value;
@@ -146,7 +160,6 @@ Context.prototype.getPath = function(cur, down) {
len = down.length;
if (cur && len === 0) return ctx.head;
- if (!ctx.isObject) return undefined;
ctx = ctx.head;
var i = 0;
while(ctx && i < len) {
@@ -168,7 +181,12 @@ Context.prototype.current = function() {
return this.stack.head;
};
-Context.prototype.getBlock = function(key) {
+Context.prototype.getBlock = function(key, chk, ctx) {
+ if (typeof key === "function") {
+ key = key(chk, ctx).data;
+ chk.data = "";
+ }
+
var blocks = this.blocks;
if (!blocks) return;
@@ -177,7 +195,7 @@ Context.prototype.getBlock = function(key) {
fn = blocks[len][key];
if (fn) return fn;
}
-}
+};
Context.prototype.shiftBlocks = function(locals) {
var blocks = this.blocks;
@@ -191,7 +209,7 @@ Context.prototype.shiftBlocks = function(locals) {
return new Context(this.stack, this.global, newBlocks);
}
return this;
-}
+};
function Stack(head, tail, idx, len) {
this.tail = tail;
@@ -224,7 +242,7 @@ Stub.prototype.flush = function() {
this.head = chunk;
}
this.callback(null, this.out);
-}
+};
function Stream() {
this.head = new Chunk(this);
@@ -247,23 +265,46 @@ Stream.prototype.flush = function() {
this.head = chunk;
}
this.emit('end');
-}
+};
Stream.prototype.emit = function(type, data) {
- var events = this.events;
-
- if (events && events[type]) {
- events[type](data);
+ if (!this.events) return false;
+ var handler = this.events[type];
+ if (!handler) return false;
+ if (typeof handler == 'function') {
+ handler(data);
+ } else {
+ var listeners = handler.slice(0);
+ for (var i = 0, l = listeners.length; i < l; i++) {
+ listeners[i](data);
+ }
}
-}
+};
Stream.prototype.on = function(type, callback) {
if (!this.events) {
this.events = {};
}
- this.events[type] = callback;
+ if (!this.events[type]) {
+ this.events[type] = callback;
+ } else if(typeof this.events[type] === 'function') {
+ this.events[type] = [this.events[type], callback];
+ } else {
+ this.events[type].push(callback);
+ }
return this;
-}
+};
+
+Stream.prototype.pipe = function(stream) {
+ this.on("data", function(data) {
+ stream.write(data, "utf8");
+ }).on("end", function() {
+ stream.end();
+ }).on("error", function(err) {
+ stream.error(err);
+ });
+ return this;
+};
function Chunk(root, next, taps) {
this.root = root;
@@ -281,7 +322,7 @@ Chunk.prototype.write = function(data) {
}
this.data += data;
return this;
-}
+};
Chunk.prototype.end = function(data) {
if (data) {
@@ -290,7 +331,7 @@ Chunk.prototype.end = function(data) {
this.flushable = true;
this.root.flush();
return this;
-}
+};
Chunk.prototype.map = function(callback) {
var cursor = new Chunk(this.root, this.next, this.taps),
@@ -300,7 +341,7 @@ Chunk.prototype.map = function(callback) {
this.flushable = true;
callback(branch);
return cursor;
-}
+};
Chunk.prototype.tap = function(tap) {
var taps = this.taps;
@@ -311,20 +352,22 @@ Chunk.prototype.tap = function(tap) {
this.taps = new Tap(tap);
}
return this;
-}
+};
Chunk.prototype.untap = function() {
this.taps = this.taps.tail;
return this;
-}
+};
Chunk.prototype.render = function(body, context) {
return body(this, context);
-}
+};
Chunk.prototype.reference = function(elem, context, auto, filters) {
if (typeof elem === "function") {
- elem = elem(this, context, null, {auto: auto, filters: filters});
+ elem.isReference = true;
+ // Changed the function calling to use apply with the current context to make sure that "this" is wat we expect it to be inside the function
+ elem = elem.apply(context.current(), [this, context, null, {auto: auto, filters: filters}]);
if (elem instanceof Chunk) {
return elem;
}
@@ -338,7 +381,7 @@ Chunk.prototype.reference = function(elem, context, auto, filters) {
Chunk.prototype.section = function(elem, context, bodies, params) {
if (typeof elem === "function") {
- elem = elem(this, context, bodies, params);
+ elem = elem.apply(context.current(), [this, context, bodies, params]);
if (elem instanceof Chunk) {
return elem;
}
@@ -354,15 +397,26 @@ Chunk.prototype.section = function(elem, context, bodies, params) {
if (dust.isArray(elem)) {
if (body) {
var len = elem.length, chunk = this;
+ context.stack.head['$len'] = len;
for (var i=0; i<len; i++) {
+ context.stack.head['$idx'] = i;
chunk = body(chunk, context.push(elem[i], i, len));
}
+ context.stack.head['$idx'] = undefined;
+ context.stack.head['$len'] = undefined;
return chunk;
}
} else if (elem === true) {
if (body) return body(this, context);
} else if (elem || elem === 0) {
- if (body) return body(this, context.push(elem));
+ if (body) {
+ context.stack.head['$idx'] = 0;
+ context.stack.head['$len'] = 1;
+ chunk = body(this, context.push(elem));
+ context.stack.head['$idx'] = undefined;
+ context.stack.head['$len'] = undefined;
+ return chunk;
+ }
} else if (skip) {
return skip(this, context);
}
@@ -379,7 +433,7 @@ Chunk.prototype.exists = function(elem, context, bodies) {
return skip(this, context);
}
return this;
-}
+};
Chunk.prototype.notexists = function(elem, context, bodies) {
var body = bodies.block,
@@ -391,7 +445,7 @@ Chunk.prototype.notexists = function(elem, context, bodies) {
return skip(this, context);
}
return this;
-}
+};
Chunk.prototype.block = function(elem, context, bodies) {
var body = bodies.block;
@@ -406,7 +460,17 @@ Chunk.prototype.block = function(elem, context, bodies) {
return this;
};
-Chunk.prototype.partial = function(elem, context) {
+Chunk.prototype.partial = function(elem, context, params) {
+ var ctx = context.stack, tempHead = ctx.head;
+ if (params){
+ //put the params context second to match what section does. {.} matches the current context without parameters
+ //remove head
+ context = context.rebase(ctx.tail);
+ //put params on
+ context = context.push(params);
+ //reattach the head
+ context = context.push(tempHead);
+ }
if (typeof elem === "function") {
return this.capture(elem, context, function(name, chunk) {
dust.load(name, chunk, context).end();
@@ -438,19 +502,6 @@ Chunk.prototype.setError = function(err) {
return this;
};
-dust.helpers = {
- sep: function(chunk, context, bodies) {
- if (context.stack.index === context.stack.of - 1) {
- return chunk;
- }
- return bodies.block(chunk, context);
- },
-
- idx: function(chunk, context, bodies) {
- return bodies.block(chunk, context.push(context.stack.index));
- }
-}
-
function Tap(head, tail) {
this.head = head;
this.tail = tail;
@@ -470,18 +521,19 @@ Tap.prototype.go = function(value) {
return value;
};
-var HCHARS = new RegExp(/[&<>\"]/),
+var HCHARS = new RegExp(/[&<>\"\']/),
AMP = /&/g,
LT = /</g,
GT = />/g,
- QUOT = /\"/g;
+ QUOT = /\"/g,
+ SQUOT = /\'/g;
dust.escapeHtml = function(s) {
if (typeof s === "string") {
if (!HCHARS.test(s)) {
return s;
}
- return s.replace(AMP,'&amp;').replace(LT,'&lt;').replace(GT,'&gt;').replace(QUOT,'&quot;');
+ return s.replace(AMP,'&amp;').replace(LT,'&lt;').replace(GT,'&gt;').replace(QUOT,'&quot;').replace(SQUOT, '&#39;');
}
return s;
};
@@ -515,8 +567,201 @@ dust.escapeJs = function(s) {
})(dust);
if (typeof exports !== "undefined") {
+ //TODO: Remove the helpers from dust core in the next release.
+ dust.helpers = require("./dust-helpers").helpers;
if (typeof process !== "undefined") {
require('./server')(dust);
}
module.exports = dust;
}
+(function(dust){
+
+/* make a safe version of console if it is not available
+ * currently supporting:
+ * _console.log
+ * */
+var _console = (typeof console !== 'undefined')? console: {
+ log: function(){
+ /* a noop*/
+ }
+};
+
+function isSelect(context) {
+ var value = context.current();
+ return typeof value === "object" && value.isSelect === true;
+}
+
+function filter(chunk, context, bodies, params, filter) {
+ var params = params || {},
+ actual,
+ expected;
+ if (params.key) {
+ actual = helpers.tap(params.key, chunk, context);
+ } else if (isSelect(context)) {
+ actual = context.current().selectKey;
+ if (context.current().isResolved) {
+ filter = function() { return false; };
+ }
+ } else {
+ throw "No key specified for filter and no key found in context from select statement";
+ }
+ expected = helpers.tap(params.value, chunk, context);
+ if (filter(expected, coerce(actual, params.type, context))) {
+ if (isSelect(context)) {
+ context.current().isResolved = true;
+ }
+ return chunk.render(bodies.block, context);
+ } else if (bodies['else']) {
+ return chunk.render(bodies['else'], context);
+ }
+
+ return chunk.write('');
+}
+
+function coerce (value, type, context) {
+ if (value) {
+ switch (type || typeof(value)) {
+ case 'number': return +value;
+ case 'string': return String(value);
+ case 'boolean': return Boolean(value);
+ case 'date': return new Date(value);
+ case 'context': return context.get(value);
+ }
+ }
+
+ return value;
+}
+
+var helpers = {
+
+ sep: function(chunk, context, bodies) {
+ if (context.stack.index === context.stack.of - 1) {
+ return chunk;
+ }
+ return bodies.block(chunk, context);
+ },
+
+ idx: function(chunk, context, bodies) {
+ return bodies.block(chunk, context.push(context.stack.index));
+ },
+
+ contextDump: function(chunk, context, bodies) {
+ _console.log(JSON.stringify(context.stack));
+ return chunk;
+ },
+
+ // Utility helping to resolve dust references in the given chunk
+ tap: function( input, chunk, context ){
+ // return given input if there is no dust reference to resolve
+ var output = input;
+ // dust compiles a string to function, if there are references
+ if( typeof input === "function"){
+ if( ( typeof input.isReference !== "undefined" ) && ( input.isReference === true ) ){ // just a plain function, not a dust `body` function
+ output = input();
+ } else {
+ output = '';
+ chunk.tap(function(data){
+ output += data;
+ return '';
+ }).render(input, context).untap();
+ if( output === '' ){
+ output = false;
+ }
+ }
+ }
+ return output;
+ },
+
+ /**
+ if helper
+ @param cond, either a string literal value or a dust reference
+ a string literal value, is enclosed in double quotes, e.g. cond="2>3"
+ a dust reference is also enclosed in double quotes, e.g. cond="'{val}'' > 3"
+ cond argument should evaluate to a valid javascript expression
+ **/
+
+ "if": function( chunk, context, bodies, params ){
+ if( params && params.cond ){
+ var cond = params.cond;
+ cond = this.tap(cond, chunk, context);
+ // eval expressions with given dust references
+ if( eval( cond ) ){
+ return chunk.render( bodies.block, context );
+ }
+ if( bodies['else'] ){
+ return chunk.render( bodies['else'], context );
+ }
+ }
+ // no condition
+ else {
+ _console.log( "No condition given in the if helper!" );
+ }
+ return chunk;
+ },
+
+ /**
+ select/eq/lt/lte/gt/gte/default helper
+ @param key, either a string literal value or a dust reference
+ a string literal value, is enclosed in double quotes, e.g. key="foo"
+ a dust reference may or may not be enclosed in double quotes, e.g. key="{val}" and key=val are both valid
+ @param type (optiona), supported types are number, boolean, string, date, context, defaults to string
+ **/
+ select: function(chunk, context, bodies, params) {
+ if( params && params.key){
+ // returns given input as output, if the input is not a dust reference, else does a context lookup
+ var key = this.tap(params.key, chunk, context);
+ return chunk.render(bodies.block, context.push({ isSelect: true, isResolved: false, selectKey: key }));
+ }
+ // no key
+ else {
+ _console.log( "No key given in the select helper!" );
+ }
+ return chunk;
+ },
+
+ eq: function(chunk, context, bodies, params) {
+ return filter(chunk, context, bodies, params, function(expected, actual) { return actual === expected; });
+ },
+
+ lt: function(chunk, context, bodies, params) {
+ return filter(chunk, context, bodies, params, function(expected, actual) { return actual < expected; });
+ },
+
+ lte: function(chunk, context, bodies, params) {
+ return filter(chunk, context, bodies, params, function(expected, actual) { return actual <= expected; });
+ },
+
+ gt: function(chunk, context, bodies, params) {
+ return filter(chunk, context, bodies, params, function(expected, actual) { return actual > expected; });
+ },
+
+ gte: function(chunk, context, bodies, params) {
+ return filter(chunk, context, bodies, params, function(expected, actual) { return actual >= expected; });
+ },
+
+ "default": function(chunk, context, bodies, params) {
+ return filter(chunk, context, bodies, params, function(expected, actual) { return true; });
+ },
+ size: function( chunk, context, bodies, params ) {
+ var subject = params.subject;
+ var value = 0;
+ if (!subject) { //undefined, "", 0
+ value = 0;
+ } else if(dust.isArray(subject)) { //array
+ value = subject.length;
+ } else if (!isNaN(subject)) { //numeric values
+ value = subject;
+ } else if (Object(subject) === subject) { //object test
+ var nr = 0;
+ for(var k in subject) if(Object.hasOwnProperty.call(subject,k)) nr++;
+ value = nr;
+ } else {
+ value = (subject + '').length; //any other value (strings etc.)
+ }
+ return chunk.write(value);
+ }
+};
+
+dust.helpers = helpers;
+
+})(typeof exports !== 'undefined' ? exports : getGlobal());
View
21 vendor/assets/javascripts/dust-core.min.js
@@ -1,21 +0,0 @@
-//
-// Dust - Asynchronous Templating v0.3.0
-// http://akdubya.github.com/dustjs
-//
-// Copyright (c) 2010, Aleksander Williams
-// Released under the MIT License.
-//
-
-var dust={};
-(function(d){function h(a,b,c){this.stack=a;this.global=b;this.blocks=c}function k(a,b,c,e){this.tail=b;this.isObject=!d.isArray(a)&&a&&typeof a==="object";this.head=a;this.index=c;this.of=e}function l(a){this.head=new f(this);this.callback=a;this.out=""}function j(){this.head=new f(this)}function f(a,b,c){this.root=a;this.next=b;this.data="";this.flushable=false;this.taps=c}function m(a,b){this.head=a;this.tail=b}d.cache={};d.register=function(a,b){if(a)d.cache[a]=b};d.render=function(a,b,c){c=(new l(c)).head;
-d.load(a,c,h.wrap(b)).end()};d.stream=function(a,b){var c=new j;d.nextTick(function(){d.load(a,c.head,h.wrap(b)).end()});return c};d.renderSource=function(a,b,c){return d.compileFn(a)(b,c)};d.compileFn=function(a,b){var c=d.loadSource(d.compile(a,b));return function(e,g){var i=g?new l(g):new j;d.nextTick(function(){c(i.head,h.wrap(e)).end()});return i}};d.load=function(a,b,c){var e=d.cache[a];if(e)return e(b,c);else{if(d.onLoad)return b.map(function(g){d.onLoad(a,function(i,n){if(i)return g.setError(i);
-d.cache[a]||d.loadSource(d.compile(n,a));d.cache[a](g,c).end()})});return b.setError(Error("Template Not Found: "+a))}};d.loadSource=function(a){return eval(a)};d.isArray=Array.isArray?Array.isArray:function(a){return Object.prototype.toString.call(a)=="[object Array]"};d.nextTick=function(a){setTimeout(a,0)};d.isEmpty=function(a){if(d.isArray(a)&&!a.length)return true;if(a===0)return false;return!a};d.filter=function(a,b,c){if(c)for(var e=0,g=c.length;e<g;e++){var i=c[e];if(i==="s")b=null;else a=
-d.filters[i](a)}if(b)a=d.filters[b](a);return a};d.filters={h:function(a){return d.escapeHtml(a)},j:function(a){return d.escapeJs(a)},u:encodeURI,uc:encodeURIComponent};d.makeBase=function(a){return new h(new k,a)};h.wrap=function(a){if(a instanceof h)return a;return new h(new k(a))};h.prototype.get=function(a){for(var b=this.stack,c;b;){if(b.isObject){c=b.head[a];if(c!==undefined)return c}b=b.tail}return this.global?this.global[a]:undefined};h.prototype.getPath=function(a,b){var c=this.stack,e=b.length;
-if(a&&e===0)return c.head;if(c.isObject){c=c.head;for(var g=0;c&&g<e;){c=c[b[g]];g++}return c}};h.prototype.push=function(a,b,c){return new h(new k(a,this.stack,b,c),this.global,this.blocks)};h.prototype.rebase=function(a){return new h(new k(a),this.global,this.blocks)};h.prototype.current=function(){return this.stack.head};h.prototype.getBlock=function(a){var b=this.blocks;if(b)for(var c=b.length,e;c--;)if(e=b[c][a])return e};h.prototype.shiftBlocks=function(a){var b=this.blocks;if(a){newBlocks=
-b?b.concat([a]):[a];return new h(this.stack,this.global,newBlocks)}return this};l.prototype.flush=function(){for(var a=this.head;a;){if(a.flushable)this.out+=a.data;else{if(a.error){this.callback(a.error);this.flush=function(){}}return}this.head=a=a.next}this.callback(null,this.out)};j.prototype.flush=function(){for(var a=this.head;a;){if(a.flushable)this.emit("data",a.data);else{if(a.error){this.emit("error",a.error);this.flush=function(){}}return}this.head=a=a.next}this.emit("end")};j.prototype.emit=
-function(a,b){var c=this.events;c&&c[a]&&c[a](b)};j.prototype.on=function(a,b){if(!this.events)this.events={};this.events[a]=b;return this};f.prototype.write=function(a){var b=this.taps;if(b)a=b.go(a);this.data+=a;return this};f.prototype.end=function(a){a&&this.write(a);this.flushable=true;this.root.flush();return this};f.prototype.map=function(a){var b=new f(this.root,this.next,this.taps),c=new f(this.root,b,this.taps);this.next=c;this.flushable=true;a(c);return b};f.prototype.tap=function(a){var b=
-this.taps;this.taps=b?b.push(a):new m(a);return this};f.prototype.untap=function(){this.taps=this.taps.tail;return this};f.prototype.render=function(a,b){return a(this,b)};f.prototype.reference=function(a,b,c,e){if(typeof a==="function"){a=a(this,b,null,{auto:c,filters:e});if(a instanceof f)return a}return d.isEmpty(a)?this:this.write(d.filter(a,c,e))};f.prototype.section=function(a,b,c,e){if(typeof a==="function"){a=a(this,b,c,e);if(a instanceof f)return a}var g=c.block;c=c["else"];if(e)b=b.push(e);
-if(d.isArray(a)){if(g){e=a.length;c=this;for(var i=0;i<e;i++)c=g(c,b.push(a[i],i,e));return c}}else if(a===true){if(g)return g(this,b)}else if(a||a===0){if(g)return g(this,b.push(a))}else if(c)return c(this,b);return this};f.prototype.exists=function(a,b,c){var e=c.block;c=c["else"];if(d.isEmpty(a)){if(c)return c(this,b)}else if(e)return e(this,b);return this};f.prototype.notexists=function(a,b,c){var e=c.block;c=c["else"];if(d.isEmpty(a)){if(e)return e(this,b)}else if(c)return c(this,b);return this};
-f.prototype.block=function(a,b,c){c=c.block;if(a)c=a;if(c)return c(this,b);return this};f.prototype.partial=function(a,b){if(typeof a==="function")return this.capture(a,b,function(c,e){d.load(c,e,b).end()});return d.load(a,this,b)};f.prototype.helper=function(a,b,c,e){return d.helpers[a](this,b,c,e)};f.prototype.capture=function(a,b,c){return this.map(function(e){var g=new l(function(i,n){i?e.setError(i):c(n,e)});a(g.head,b).end()})};f.prototype.setError=function(a){this.error=a;this.root.flush();
-return this};d.helpers={sep:function(a,b,c){if(b.stack.index===b.stack.of-1)return a;return c.block(a,b)},idx:function(a,b,c){return c.block(a,b.push(b.stack.index))}};m.prototype.push=function(a){return new m(a,this)};m.prototype.go=function(a){for(var b=this;b;){a=b.head(a);b=b.tail}return a};var o=RegExp(/[&<>\"]/),p=/&/g,q=/</g,r=/>/g,s=/\"/g;d.escapeHtml=function(a){if(typeof a==="string"){if(!o.test(a))return a;return a.replace(p,"&amp;").replace(q,"&lt;").replace(r,"&gt;").replace(s,"&quot;")}return a};
-var t=/\\/g,u=/\r/g,v=/\u2028/g,w=/\u2029/g,x=/\n/g,y=/\f/g,z=/'/g,A=/"/g,B=/\t/g;d.escapeJs=function(a){if(typeof a==="string")return a.replace(t,"\\\\").replace(A,'\\"').replace(z,"\\'").replace(u,"\\r").replace(v,"\\u2028").replace(w,"\\u2029").replace(x,"\\n").replace(y,"\\f").replace(B,"\\t");return a}})(dust);if(typeof exports!=="undefined"){typeof process!=="undefined"&&require("./server")(dust);module.exports=dust};
View
3,209 vendor/assets/javascripts/dust-full-for-compile.js
@@ -1,3209 +0,0 @@
-//
-// Dust - Asynchronous Templating v0.3.0
-// http://akdubya.github.com/dustjs
-//
-// Copyright (c) 2010, Aleksander Williams
-// Released under the MIT License.
-//
-
-var dust = {};
-
-(function(dust) {
-
-dust.cache = {};
-
-dust.register = function(name, tmpl) {
- if (!name) return;
- dust.cache[name] = tmpl;
-};
-
-dust.render = function(name, context, callback) {
- var chunk = new Stub(callback).head;
- dust.load(name, chunk, Context.wrap(context)).end();
-};
-
-dust.stream = function(name, context) {
- var stream = new Stream();
- dust.nextTick(function() {
- dust.load(name, stream.head, Context.wrap(context)).end();
- });
- return stream;
-};
-
-dust.renderSource = function(source, context, callback) {
- return dust.compileFn(source)(context, callback);
-};
-
-dust.compileFn = function(source, name) {
- var tmpl = dust.loadSource(dust.compile(source, name));
- return function(context, callback) {
- var master = callback ? new Stub(callback) : new Stream();
- dust.nextTick(function() {
- tmpl(master.head, Context.wrap(context)).end();
- });
- return master;
- }
-};
-
-dust.load = function(name, chunk, context) {
- var tmpl = dust.cache[name];
- if (tmpl) {
- return tmpl(chunk, context);
- } else {
- if (dust.onLoad) {
- return chunk.map(function(chunk) {
- dust.onLoad(name, function(err, src) {
- if (err) return chunk.setError(err);
- if (!dust.cache[name]) dust.loadSource(dust.compile(src, name));
- dust.cache[name](chunk, context).end();
- });
- });
- }
- return chunk.setError(new Error("Template Not Found: " + name));
- }
-};
-
-dust.loadSource = function(source, path) {
- return eval(source);
-};
-
-if (Array.isArray) {
- dust.isArray = Array.isArray;
-} else {
- dust.isArray = function(arr) {
- return Object.prototype.toString.call(arr) == "[object Array]";
- };
-}
-
-dust.nextTick = function(callback) {
- setTimeout(callback, 0);
-}
-
-dust.isEmpty = function(value) {
- if (dust.isArray(value) && !value.length) return true;
- if (value === 0) return false;
- return (!value);
-};
-
-dust.filter = function(string, auto, filters) {
- 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) {
- string = dust.filters[auto](string);
- }
- return string;
-};
-
-dust.filters = {
- h: function(value) { return dust.escapeHtml(value); },
- j: function(value) { return dust.escapeJs(value); },
- u: encodeURI,
- uc: encodeURIComponent
-}
-
-function Context(stack, global, blocks) {
- this.stack = stack;
- this.global = global;
- this.blocks = blocks;
-}
-
-dust.makeBase = function(global) {
- return new Context(new Stack(), global);
-}
-
-Context.wrap = function(context) {
- if (context instanceof Context) {
- return context;
- }
- return new Context(new Stack(context));
-}
-
-Context.prototype.get = function(key) {
- var ctx = this.stack, value;
-
- while(ctx) {
- if (ctx.isObject) {
- value = ctx.head[key];
- if (!(value === undefined)) {
- return value;
- }
- }
- ctx = ctx.tail;
- }
- return this.global ? this.global[key] : undefined;
-};
-
-Context.prototype.getPath = function(cur, down) {
- var ctx = this.stack,
- len = down.length;
-
- if (cur && len === 0) return ctx.head;
- if (!ctx.isObject) return undefined;
- ctx = ctx.head;
- var i = 0;
- while(ctx && i < len) {
- ctx = ctx[down[i]];
- i++;
- }
- return ctx;
-};
-
-Context.prototype.push = function(head, idx, len) {
- return new Context(new Stack(head, this.stack, idx, len), this.global, this.blocks);
-};
-
-Context.prototype.rebase = function(head) {
- return new Context(new Stack(head), this.global, this.blocks);
-};
-
-Context.prototype.current = function() {
- return this.stack.head;
-};
-
-Context.prototype.getBlock = function(key) {
- var blocks = this.blocks;
-
- if (!blocks) return;
- var len = blocks.length, fn;
- while (len--) {
- fn = blocks[len][key];
- if (fn) return fn;
- }
-}
-
-Context.prototype.shiftBlocks = function(locals) {
- var blocks = this.blocks;
-
- if (locals) {
- if (!blocks) {
- newBlocks = [locals];
- } else {
- newBlocks = blocks.concat([locals]);
- }
- return new Context(this.stack, this.global, newBlocks);
- }
- return this;
-}
-
-function Stack(head, tail, idx, len) {
- this.tail = tail;
- this.isObject = !dust.isArray(head) && head && typeof head === "object";
- this.head = head;
- this.index = idx;
- this.of = len;
-}
-
-function Stub(callback) {
- this.head = new Chunk(this);
- this.callback = callback;
- this.out = '';
-}
-
-Stub.prototype.flush = function() {
- var chunk = this.head;
-
- while (chunk) {
- if (chunk.flushable) {
- this.out += chunk.data;
- } else if (chunk.error) {
- this.callback(chunk.error);
- this.flush = function() {};
- return;
- } else {
- return;
- }
- chunk = chunk.next;
- this.head = chunk;
- }
- this.callback(null, this.out);
-}
-
-function Stream() {
- this.head = new Chunk(this);
-}
-
-Stream.prototype.flush = function() {
- var chunk = this.head;
-
- while(chunk) {
- if (chunk.flushable) {
- this.emit('data', chunk.data);
- } else if (chunk.error) {
- this.emit('error', chunk.error);
- this.flush = function() {};
- return;
- } else {
- return;
- }
- chunk = chunk.next;
- this.head = chunk;
- }
- this.emit('end');
-}
-
-Stream.prototype.emit = function(type, data) {
- var events = this.events;
-
- if (events && events[type]) {
- events[type](data);
- }
-}
-
-Stream.prototype.on = function(type, callback) {
- if (!this.events) {
- this.events = {};
- }
- this.events[type] = callback;
- return this;
-}
-
-function Chunk(root, next, taps) {
- this.root = root;
- this.next = next;
- this.data = '';
- this.flushable = false;
- this.taps = taps;
-}
-
-Chunk.prototype.write = function(data) {
- var taps = this.taps;
-
- if (taps) {
- data = taps.go(data);
- }
- this.data += data;
- return this;
-}
-
-Chunk.prototype.end = function(data) {
- if (data) {
- this.write(data);
- }
- this.flushable = true;
- this.root.flush();
- return this;
-}
-
-Chunk.prototype.map = function(callback) {
- var cursor = new Chunk(this.root, this.next, this.taps),
- branch = new Chunk(this.root, cursor, this.taps);
-
- this.next = branch;
- this.flushable = true;
- callback(branch);
- return cursor;
-}
-
-Chunk.prototype.tap = function(tap) {
- var taps = this.taps;
-
- if (taps) {
- this.taps = taps.push(tap);
- } else {
- this.taps = new Tap(tap);
- }
- return this;
-}
-
-Chunk.prototype.untap = function() {
- this.taps = this.taps.tail;
- return this;
-}
-
-Chunk.prototype.render = function(body, context) {
- return body(this, context);
-}
-
-Chunk.prototype.reference = function(elem, context, auto, filters) {
- if (typeof elem === "function") {
- elem = elem(this, context, null, {auto: auto, filters: filters});
- if (elem instanceof Chunk) {
- return elem;
- }
- }
- if (!dust.isEmpty(elem)) {
- return this.write(dust.filter(elem, auto, filters));
- } else {
- return this;
- }
-};
-
-Chunk.prototype.section = function(elem, context, bodies, params) {
- if (typeof elem === "function") {
- elem = elem(this, context, bodies, params);
- if (elem instanceof Chunk) {
- return elem;
- }
- }
-
- var body = bodies.block,
- skip = bodies['else'];
-
- if (params) {
- context = context.push(params);
- }
-
- if (dust.isArray(elem)) {
- if (body) {
- var len = elem.length, chunk = this;
- for (var i=0; i<len; i++) {
- chunk = body(chunk, context.push(elem[i], i, len));
- }
- return chunk;
- }
- } else if (elem === true) {
- if (body) return body(this, context);
- } else if (elem || elem === 0) {
- if (body) return body(this, context.push(elem));
- } else if (skip) {
- return skip(this, context);
- }
- return this;
-};
-
-Chunk.prototype.exists = function(elem, context, bodies) {
- var body = bodies.block,
- skip = bodies['else'];
-
- if (!dust.isEmpty(elem)) {
- if (body) return body(this, context);
- } else if (skip) {
- return skip(this, context);
- }
- return this;
-}
-
-Chunk.prototype.notexists = function(elem, context, bodies) {
- var body = bodies.block,
- skip = bodies['else'];
-
- if (dust.isEmpty(elem)) {
- if (body) return body(this, context);
- } else if (skip) {
- return skip(this, context);
- }
- return this;
-}
-
-Chunk.prototype.block = function(elem, context, bodies) {
- var body = bodies.block;
-
- if (elem) {
- body = elem;
- }
-
- if (body) {
- return body(this, context);
- }
- return this;
-};
-
-Chunk.prototype.partial = function(elem, context) {
- if (typeof elem === "function") {
- return this.capture(elem, context, function(name, chunk) {
- dust.load(name, chunk, context).end();
- });
- }
- return dust.load(elem, this, context);
-};
-
-Chunk.prototype.helper = function(name, context, bodies, params) {
- return dust.helpers[name](this, context, bodies, params);
-};
-
-Chunk.prototype.capture = function(body, context, callback) {
- return this.map(function(chunk) {
- var stub = new Stub(function(err, out) {
- if (err) {
- chunk.setError(err);
- } else {
- callback(out, chunk);
- }
- });
- body(stub.head, context).end();
- });
-};
-
-Chunk.prototype.setError = function(err) {
- this.error = err;
- this.root.flush();
- return this;
-};
-
-dust.helpers = {
- sep: function(chunk, context, bodies) {
- if (context.stack.index === context.stack.of - 1) {
- return chunk;
- }
- return bodies.block(chunk, context);
- },
-
- idx: function(chunk, context, bodies) {
- return bodies.block(chunk, context.push(context.stack.index));
- }
-}
-
-function Tap(head, tail) {
- this.head = head;
- this.tail = tail;
-}
-
-Tap.prototype.push = function(tap) {
- return new Tap(tap, this);
-};
-
-Tap.prototype.go = function(value) {
- var tap = this;
-
- while(tap) {
- value = tap.head(value);
- tap = tap.tail;
- }
- return value;
-};
-
-var HCHARS = new RegExp(/[&<>\"]/),
- AMP = /&/g,
- LT = /</g,
- GT = />/g,
- QUOT = /\"/g;
-
-dust.escapeHtml = function(s) {
- if (typeof s === "string") {
- if (!HCHARS.test(s)) {
- return s;
- }
- return s.replace(AMP,'&amp;').replace(LT,'&lt;').replace(GT,'&gt;').replace(QUOT,'&quot;');
- }
- return s;
-};
-
-var BS = /\\/g,
- CR = /\r/g,
- LS = /\u2028/g,
- PS = /\u2029/g,
- NL = /\n/g,
- LF = /\f/g,
- SQ = /'/g,
- DQ = /"/g,
- TB = /\t/g;
-
-dust.escapeJs = function(s) {
- if (typeof s === "string") {
- return s
- .replace(BS, '\\\\')
- .replace(DQ, '\\"')
- .replace(SQ, "\\'")
- .replace(CR, '\\r')
- .replace(LS, '\\u2028')
- .replace(PS, '\\u2029')
- .replace(NL, '\\n')
- .replace(LF, '\\f')
- .replace(TB, "\\t");
- }
- return s;
-};
-
-})(dust);
-
-if (typeof exports !== "undefined") {
- if (typeof process !== "undefined") {
- require('./server')(dust);
- }
- module.exports = dust;
-}
-(function(dust) {
-
-dust.compile = function(source, name) {
- var ast = filterAST(dust.parse(source));
- return compile(ast, name);
-};
-
-function filterAST(ast) {
- var context = {};
- return dust.filterNode(context, ast);
-}
-
-dust.filterNode = function(context, node) {
- return dust.optimizers[node[0]](context, node);
-}
-
-dust.optimizers = {
- body: compactBuffers,
- buffer: noop,
- special: convertSpecial,
- format: nullify, // TODO: convert format
- reference: visit,
- "#": visit,
- "?": visit,
- "^": visit,
- "<": visit,
- "+": visit,
- "@": visit,
- "%": visit,
- partial: visit,
- context: visit,
- params: visit,
- bodies: visit,
- param: visit,
- filters: noop,
- key: noop,
- path: noop,
- literal: noop,
- 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++) {
- var res = dust.filterNode(context, node[i]);
- if (res) out.push(res);
- }
- return out;
-}
-
-// Compacts consecutive buffer nodes into a single node
-function compactBuffers(context, node) {
- var out = [node[0]], memo;
- for (var i=1, len=node.length; i<len; i++) {
- var res = dust.filterNode(context, node[i]);
- if (res) {
- if (res[0] === 'buffer') {
- if (memo) {
- memo[1] += res[1];
- } else {
- memo = res;
- out.push(res);
- }
- } else {
- memo = null;
- out.push(res);
- }
- }
- }
- return out;
-}
-
-var specialChars = {
- "s": " ",
- "n": "\n",
- "r": "\r",
- "lb": "{",
- "rb": "}"
-};
-
-function convertSpecial(context, node) { return ['buffer', specialChars[node[1]]] }
-function noop(context, node) { return node }
-function nullify(){}
-
-function compile(ast, name) {
- var context = {
- name: name,
- bodies: [],
- blocks: {},
- index: 0,
- auto: "h"
- }
-
- return "(function(){dust.register("
- + (name ? "\"" + name + "\"" : "null") + ","
- + dust.compileNode(context, ast)
- + ");"
- + compileBlocks(context)
- + compileBodies(context)
- + "return body_0;"
- + "})();";
-}
-
-function compileBlocks(context) {
- var out = [],
- blocks = context.blocks;
-
- for (var name in blocks) {
- out.push(name + ":" + blocks[name]);
- }
- if (out.length) {
- context.blocks = "ctx=ctx.shiftBlocks(blocks);";
- return "var blocks={" + out.join(',') + "};";
- }
- return context.blocks = "";
-}
-
-function compileBodies(context) {
- var out = [],
- bodies = context.bodies,
- blx = context.blocks;
-
- for (var i=0, len=bodies.length; i<len; i++) {
- out[i] = "function body_" + i + "(chk,ctx){"
- + blx + "return chk" + bodies[i] + ";}";
- }
- 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;
- context.bodies[id] = compileParts(context, node);
- return name;
- },
-
- buffer: function(context, node) {
- return ".write(" + escape(node[1]) + ")";
- },
-
- format: function(context, node) {
- return ".write(" + escape(node[1] + node[2]) + ")";
- },
-
- reference: function(context, node) {
- return ".reference(" + dust.compileNode(context, node[1])
- + ",ctx," + dust.compileNode(context, node[2]) + ")";
- },
-
- "#": function(context, node) {
- return compileSection(context, node, "section");
- },
-
- "?": function(context, node) {
- return compileSection(context, node, "exists");
- },
-
- "^": function(context, node) {
- return compileSection(context, node, "notexists");
- },
-
- "<": function(context, node) {
- var bodies = node[4];
- for (var i=1, len=bodies.length; i<len; i++) {
- var param = bodies[i],
- type = param[1][1];
- if (type === "block") {
- context.blocks[node[1].text] = dust.compileNode(context, param[2]);
- return '';
- }
- }
- return '';
- },
-
- "+": function(context, node) {
- return ".block(ctx.getBlock("
- + escape(node[1].text)
- + ")," + dust.compileNode(context, node[2]) + ","
- + dust.compileNode(context, node[4]) + ","
- + dust.compileNode(context, node[3])
- + ")";
- },
-
- "@": function(context, node) {
- return ".helper("
- + escape(node[1].text)
- + "," + dust.compileNode(context, node[2]) + ","
- + dust.compileNode(context, node[4]) + ","
- + dust.compileNode(context, node[3])
- + ")";
- },
-
- "%": 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])
- + "," + dust.compileNode(context, node[2]) + ")";
- },
-
- context: function(context, node) {
- if (node[1]) {
- return "ctx.rebase(" + dust.compileNode(context, node[1]) + ")";
- }
- return "ctx";
- },
-
- params: function(context, node) {
- var out = [];
- for (var i=1, len=node.length; i<len; i++) {
- out.push(dust.compileNode(context, node[i]));
- }
- if (out.length) {
- return "{" + out.join(',') + "}";
- }
- return "null";
- },
-
- bodies: function(context, node) {
- var out = [];
- for (var i=1, len=node.length; i<len; i++) {
- out.push(dust.compileNode(context, node[i]));
- }
- return "{" + out.join(',') + "}";
- },
-
- param: function(context, node) {
- return dust.compileNode(context, node[1]) + ":" + dust.compileNode(context, node[2]);
- },
-
- filters: function(context, node) {
- var list = [];
- for (var i=1, len=node.length; i<len; i++) {
- var filter = node[i];
- list.push("\"" + filter + "\"");
- }
- return "\"" + context.auto + "\""
- + (list.length ? ",[" + list.join(',') + "]" : '');
- },
-
- key: function(context, node) {
- return "ctx.get(\"" + node[1] + "\")";
- },
-
- path: function(context, node) {
- var current = node[1],
- keys = node[2],
- list = [];
-
- for (var i=0,len=keys.length; i<len; i++) {
- list.push("\"" + keys[i] + "\"");
- }
- return "ctx.getPath(" + current + ",[" + list.join(',') + "])";
- },
-
- literal: function(context, node) {
- return escape(node[1]);
- }
-}
-
-function compileSection(context, node, cmd) {
- return "." + cmd + "("
- + dust.compileNode(context, node[1])
- + "," + dust.compileNode(context, node[2]) + ","
- + dust.compileNode(context, node[4]) + ","
- + dust.compileNode(context, node[3])
- + ")";
-}
-
-var escape = (typeof JSON === "undefined")
- ? function(str) { return "\"" + dust.escapeJs(str) + "\"" }
- : JSON.stringify;
-
-})(typeof exports !== 'undefined' ? exports : dust);
-(function(dust){
-
-var parser = (function(){
- /* Generated by PEG.js (http://pegjs.majda.cz/). */
-
- var result = {
- /*
- * Parses the input with a generated parser. If the parsing is successfull,
- * returns a value explicitly or implicitly specified by the grammar from
- * which the parser was generated (see |PEG.buildParser|). If the parsing is
- * unsuccessful, throws |PEG.parser.SyntaxError| describing the error.
- */
- parse: function(input) {
- var pos = 0;
- var reportMatchFailures = true;
- var rightmostMatchFailuresPos = 0;
- var rightmostMatchFailuresExpected = [];
- var cache = {};
-
- function padLeft(input, padding, length) {
- var result = input;
-
- var padLength = length - input.length;
- for (var i = 0; i < padLength; i++) {
- result = padding + result;
- }
-
- return result;
- }
-
- function escape(ch) {
- var charCode = ch.charCodeAt(0);
-
- if (charCode < 0xFF) {
- var escapeChar = 'x';
- var length = 2;
- } else {
- var escapeChar = 'u';
- var length = 4;
- }
-
- return '\\' + escapeChar + padLeft(charCode.toString(16).toUpperCase(), '0', length);
- }
-
- function quote(s) {
- /*
- * ECMA-262, 5th ed., 7.8.4: All characters may appear literally in a
- * string literal except for the closing quote character, backslash,
- * carriage return, line separator, paragraph separator, and line feed.
- * Any character may appear in the form of an escape sequence.
- */
- return '"' + s
- .replace(/\\/g, '\\\\') // backslash
- .replace(/"/g, '\\"') // closing quote character
- .replace(/\r/g, '\\r') // carriage return
- .replace(/\n/g, '\\n') // line feed
- .replace(/[\x80-\uFFFF]/g, escape) // non-ASCII characters
- + '"';
- }
-
- function matchFailed(failure) {
- if (pos < rightmostMatchFailuresPos) {
- return;
- }
-
- if (pos > rightmostMatchFailuresPos) {
- rightmostMatchFailuresPos = pos;
- rightmostMatchFailuresExpected = [];
- }
-
- rightmostMatchFailuresExpected.push(failure);
- }
-
- function parse_body() {
- var cacheKey = "body" + '@' + pos;
- var cachedResult = cache[cacheKey];
- if (cachedResult) {
- pos = cachedResult.nextPos;
- return cachedResult.result;
- }
-
-
- var result1 = [];
- var result2 = parse_part();
- while (result2 !== null) {
- result1.push(result2);
- var result2 = parse_part();
- }
- var result0 = result1 !== null
- ? (function(p) { return ["body"].concat(p) })(result1)
- : null;
-
-
-
- cache[cacheKey] = {
- nextPos: pos,
- result: result0
- };
- return result0;
- }
-
- function parse_part() {
- var cacheKey = "part" + '@' + pos;
- var cachedResult = cache[cacheKey];
- if (cachedResult) {
- pos = cachedResult.nextPos;
- return cachedResult.result;
- }
-
-
- var result6 = parse_comment();
- if (result6 !== null) {
- var result0 = result6;
- } else {
- var result5 = parse_section();
- if (result5 !== null) {
- var result0 = result5;
- } else {
- var result4 = parse_partial();
- if (result4 !== null) {
- var result0 = result4;
- } else {
- var result3 = parse_special();
- if (result3 !== null) {
- var result0 = result3;
- } else {
- var result2 = parse_reference();
- if (result2 !== null) {
- var result0 = result2;
- } else {
- var result1 = parse_buffer();
- if (result1 !== null) {
- var result0 = result1;
- } else {
- var result0 = null;;
- };
- };
- };
- };
- };
- }
-
-
-
- cache[cacheKey] = {
- nextPos: pos,
- result: result0
- };
- return result0;
- }
-
- function parse_section() {
- var cacheKey = "section" + '@' + pos;
- var cachedResult = cache[cacheKey];
- if (cachedResult) {
- pos = cachedResult.nextPos;
- return cachedResult.result;
- }
-
- var savedReportMatchFailures = reportMatchFailures;
- reportMatchFailures = false;
- var savedPos1 = pos;
- var result8 = parse_sec_tag_start();
- if (result8 !== null) {
- var result9 = parse_rd();
- if (result9 !== null) {
- var result10 = parse_body();
- if (result10 !== null) {
- var result11 = parse_bodies();
- if (result11 !== null) {
- var result12 = parse_end_tag();
- if (result12 !== null) {
- var result13 = (function() {return result8[1].text === result12.text})() ? '' : null;
- if (result13 !== null) {
- var result7 = [result8, result9, result10, result11, result12, result13];
- } else {
- var result7 = null;
- pos = savedPos1;
- }
- } else {
- var result7 = null;
- pos = savedPos1;
- }
- } else {
- var result7 = null;
- pos = savedPos1;
- }
- } else {
- var result7 = null;
- pos = savedPos1;
- }
- } else {
- var result7 = null;
- pos = savedPos1;
- }
- } else {
- var result7 = null;
- pos = savedPos1;
- }
- var result6 = result7 !== null
- ? (function(t, b, e, n) { e.push(["param", ["literal", "block"], b]); t.push(e); return t })(result7[0], result7[2], result7[3], result7[4])
- : null;
- if (result6 !== null) {
- var result0 = result6;
- } else {
- var savedPos0 = pos;
- var result3 = parse_sec_tag_start();
- if (result3 !== null) {
- if (input.substr(pos, 1) === "/") {
- var result4 = "/";
- pos += 1;
- } else {
- var result4 = null;
- if (reportMatchFailures) {
- matchFailed("\"/\"");
- }
- }
- if (result4 !== null) {
- var result5 = parse_rd();
- if (result5 !== null) {
- var result2 = [result3, result4, result5];
- } else {
- var result2 = null;
- pos = savedPos0;
- }
- } else {
- var result2 = null;
- pos = savedPos0;
- }
- } else {
- var result2 = null;
- pos = savedPos0;
- }
- var result1 = result2 !== null
- ? (function(t) { t.push(["bodies"]); return t })(result2[0])
- : null;
- if (result1 !== null) {
- var result0 = result1;
- } else {
- var result0 = null;;
- };
- }
- reportMatchFailures = savedReportMatchFailures;
- if (reportMatchFailures && result0 === null) {
- matchFailed("section");
- }
-
- cache[cacheKey] = {
- nextPos: pos,
- result: result0
- };
- return result0;
- }
-
- function parse_sec_tag_start() {
- var cacheKey = "sec_tag_start" + '@' + pos;
- var cachedResult = cache[cacheKey];
- if (cachedResult) {
- pos = cachedResult.nextPos;
- return cachedResult.result;
- }
-
-
- var savedPos0 = pos;
- var result2 = parse_ld();
- if (result2 !== null) {
- if (input.substr(pos).match(/^[#?^<+@%]/) !== null) {
- var result3 = input.charAt(pos);
- pos++;
- } else {
- var result3 = null;
- if (reportMatchFailures) {
- matchFailed("[#?^<+@%]");
- }
- }
- if (result3 !== null) {
- var result4 = parse_identifier();
- if (result4 !== null) {
- var result5 = parse_context();
- if (result5 !== null) {
- var result6 = parse_params();
- if (result6 !== null) {
- var result1 = [result2, result3, result4, result5, result6];
- } else {
- var result1 = null;
- pos = savedPos0;
- }
- } else {
- var result1 = null;
- pos = savedPos0;
- }
- } else {
- var result1 = null;
- pos = savedPos0;
- }
- } else {
- var result1 = null;
- pos = savedPos0;
- }
- } else {
- var result1 = null;
- pos = savedPos0;
- }
- var result0 = result1 !== null
- ? (function(t, n, c, p) { return [t, n, c, p] })(result1[1], result1[2], result1[3], result1[4])
- : null;
-
-
-
- cache[cacheKey] = {
- nextPos: pos,
- result: result0
- };
- return result0;
- }
-
- function parse_end_tag() {
- var cacheKey = "end_tag" + '@' + pos;
- var cachedResult = cache[cacheKey];
- if (cachedResult) {
- pos = cachedResult.nextPos;
- return cachedResult.result;
- }
-
- var savedReportMatchFailures = reportMatchFailures;
- reportMatchFailures = false;
- var savedPos0 = pos;
- var result2 = parse_ld();
- if (result2 !== null) {
- if (input.substr(pos, 1) === "/") {
- var result3 = "/";
- pos += 1;
- } else {
- var result3 = null;
- if (reportMatchFailures) {
- matchFailed("\"/\"");
- }
- }
- if (result3 !== null) {
- var result4 = parse_identifier();
- if (result4 !== null) {
- var result5 = parse_rd();
- if (result5 !== null) {
- var result1 = [result2, result3, result4, result5];
- } else {
- var result1 = null;
- pos = savedPos0;
- }
- } else {
- var result1 = null;
- pos = savedPos0;
- }
- } else {
- var result1 = null;
- pos = savedPos0;
- }
- } else {
- var result1 = null;
- pos = savedPos0;
- }
- var result0 = result1 !== null
- ? (function(n) { return n })(result1[2])
- : null;
- reportMatchFailures = savedReportMatchFailures;
- if (reportMatchFailures && result0 === null) {
- matchFailed("end tag");
- }
-
- cache[cacheKey] = {
- nextPos: pos,
- result: result0
- };
- return result0;
- }
-
- function parse_context() {
- var cacheKey = "context" + '@' + pos;
- var cachedResult = cache[cacheKey];
- if (cachedResult) {
- pos = cachedResult.nextPos;
- return cachedResult.result;
- }
-
-
- var savedPos0 = pos;
- if (input.substr(pos, 1) === ":") {
- var result4 = ":";
- pos += 1;
- } else {
- var result4 = null;
- if (reportMatchFailures) {
- matchFailed("\":\"");
- }
- }
- if (result4 !== null) {
- var result5 = parse_identifier();
- if (result5 !== null) {
- var result3 = [result4, result5];
- } else {
- var result3 = null;
- pos = savedPos0;
- }
- } else {
- var result3 = null;
- pos = savedPos0;
- }
- var result2 = result3 !== null
- ? (function(n) {return n})(result3[1])
- : null;
- var result1 = result2 !== null ? result2 : '';
- var result0 = result1 !== null
- ? (function(n) { return n ? ["context", n] : ["context"] })(result1)
- : null;
-
-
-
- cache[cacheKey] = {
- nextPos: pos,
- result: result0
- };
- return result0;
- }
-
- function parse_params() {
- var cacheKey = "params" + '@' + pos;
- var cachedResult = cache[cacheKey];
- if (cachedResult) {
- pos = cachedResult.nextPos;
- return cachedResult.result;
- }
-
- var savedReportMatchFailures = reportMatchFailures;
- reportMatchFailures = false;
- var result1 = [];
- var savedPos0 = pos;
- var result4 = parse_ws();
- if (result4 !== null) {
- var result5 = parse_key();
- if (result5 !== null) {
- if (input.substr(pos, 1) === "=") {
- var result6 = "=";
- pos += 1;
- } else {
- var result6 = null;
- if (reportMatchFailures) {
- matchFailed("\"=\"");
- }
- }
- if (result6 !== null) {
- var result9 = parse_identifier();
- if (result9 !== null) {
- var result7 = result9;
- } else {
- var result8 = parse_inline();
- if (result8 !== null) {
- var result7 = result8;
- } else {
- var result7 = null;;
- };
- }
- if (result7 !== null) {
- var result3 = [result4, result5, result6, result7];
- } else {
- var result3 = null;
- pos = savedPos0;
- }
- } else {
- var result3 = null;
- pos = savedPos0;
- }
- } else {
- var result3 = null;
- pos = savedPos0;
- }
- } else {
- var result3 = null;
- pos = savedPos0;
- }
- var result2 = result3 !== null
- ? (function(k, v) {return ["param", ["literal", k], v]})(result3[1], result3[3])
- : null;
- while (result2 !== null) {
- result1.push(result2);
- var savedPos0 = pos;
- var result4 = parse_ws();
- if (result4 !== null) {
- var result5 = parse_key();
- if (result5 !== null) {
- if (input.substr(pos, 1) === "=") {
- var result6 = "=";
- pos += 1;
- } else {
- var result6 = null;
- if (reportMatchFailures) {
- matchFailed("\"=\"");
- }
- }
- if (result6 !== null) {
- var result9 = parse_identifier();
- if (result9 !== null) {
- var result7 = result9;
- } else {
- var result8 = parse_inline();
- if (result8 !== null) {
- var result7 = result8;
- } else {
- var result7 = null;;
- };
- }
- if (result7 !== null) {
- var result3 = [result4, result5, result6, result7];
- } else {
- var result3 = null;
- pos = savedPos0;
- }
- } else {
- var result3 = null;
- pos = savedPos0;
- }
- } else {
- var result3 = null;
- pos = savedPos0;
- }
- } else {
- var result3 = null;
- pos = savedPos0;
- }
- var result2 = result3 !== null
- ? (function(k, v) {return ["param", ["literal", k], v]})(result3[1], result3[3])
- : null;
- }
- var result0 = result1 !== null
- ? (function(p) { return ["params"].concat(p) })(result1)
- : null;
- reportMatchFailures = savedReportMatchFailures;
- if (reportMatchFailures && result0 === null) {
- matchFailed("params");
- }
-
- cache[cacheKey] = {
- nextPos: pos,
- result: result0
- };
- return result0;
- }
-
- function parse_bodies() {
- var cacheKey = "bodies" + '@' + pos;
- var cachedResult = cache[cacheKey];
- if (cachedResult) {
- pos = cachedResult.nextPos;
- return cachedResult.result;
- }
-
- var savedReportMatchFailures = reportMatchFailures;
- reportMatchFailures = false;
- var result1 = [];
- var savedPos0 = pos;
- var result4 = parse_ld();
- if (result4 !== null) {
- if (input.substr(pos, 1) === ":") {
- var result5 = ":";
- pos += 1;
- } else {
- var result5 = null;
- if (reportMatchFailures) {
- matchFailed("\":\"");
- }
- }
- if (result5 !== null) {
- var result6 = parse_key();
- if (result6 !== null) {
- var result7 = parse_rd();
- if (result7 !== null) {
- var result8 = parse_body();
- if (result8 !== null) {
- var result3 = [result4, result5, result6, result7, result8];
- } else {
- var result3 = null;
- pos = savedPos0;
- }
- } else {
- var result3 = null;
- pos = savedPos0;
- }
- } else {
- var result3 = null;
- pos = savedPos0;
- }
- } else {
- var result3 = null;
- pos = savedPos0;
- }
- } else {
- var result3 = null;
- pos = savedPos0;
- }
- var result2 = result3 !== null
- ? (function(k, v) {return ["param", ["literal", k], v]})(result3[2], result3[4])
- : null;
- while (result2 !== null) {
- result1.push(result2);
- var savedPos0 = pos;
- var result4 = parse_ld();
- if (result4 !== null) {
- if (input.substr(pos, 1) === ":") {
- var result5 = ":";
- pos += 1;
- } else {
- var result5 = null;
- if (reportMatchFailures) {
- matchFailed("\":\"");
- }
- }
- if (result5 !== null) {
- var result6 = parse_key();
- if (result6 !== null) {
- var result7 = parse_rd();
- if (result7 !== null) {
- var result8 = parse_body();
- if (result8 !== null) {
- var result3 = [result4, result5, result6, result7, result8];
- } else {
- var result3 = null;
- pos = savedPos0;
- }
- } else {
- var result3 = null;
- pos = savedPos0;
- }
- } else {
- var result3 = null;
- pos = savedPos0;
- }
- } else {
- var result3 = null;
- pos = savedPos0;
- }
- } else {
- var result3 = null;
- pos = savedPos0;
- }
- var result2 = result3 !== null
- ? (function(k, v) {return ["param", ["literal", k], v]})(result3[2], result3[4])
- : null;
- }
- var result0 = result1 !== null
- ? (function(p) { return ["bodies"].concat(p) })(result1)
- : null;
- reportMatchFailures = savedReportMatchFailures;
- if (reportMatchFailures && result0 === null) {
- matchFailed("bodies");
- }
-
- cache[cacheKey] = {
- nextPos: pos,
- result: result0
- };
- return result0;
- }
-
- function parse_reference() {
- var cacheKey = "reference" + '@' + pos;
- var cachedResult = cache[cacheKey];
- if (cachedResult) {
- pos = cachedResult.nextPos;
- return cachedResult.result;
- }
-
- var savedReportMatchFailures = reportMatchFailures;
- reportMatchFailures = false;
- var savedPos0 = pos;
- var result2 = parse_ld();
- if (result2 !== null) {
- var result3 = parse_identifier();
- if (result3 !== null) {
- var result4 = parse_filters();
- if (result4 !== null) {
- var result5 = parse_rd();
- if (result5 !== null) {
- var result1 = [result2, result3, result4, result5];
- } else {
- var result1 = null;
- pos = savedPos0;
- }
- } else {
- var result1 = null;
- pos = savedPos0;
- }
- } else {
- var result1 = null;
- pos = savedPos0;
- }
- } else {
- var result1 = null;
- pos = savedPos0;
- }
- var result0 = result1 !== null
- ? (function(n, f) { return ["reference", n, f] })(result1[1], result1[2])
- : null;
- reportMatchFailures = savedReportMatchFailures;
- if (reportMatchFailures && result0 === null) {
- matchFailed("reference");
- }
-
- cache[cacheKey] = {
- nextPos: pos,
- result: result0
- };
- return result0;
- }
-
- function parse_partial() {
- var cacheKey = "partial" + '@' + pos;
- var cachedResult = cache[cacheKey];
- if (cachedResult) {
- pos = cachedResult.nextPos;
- return cachedResult.result;
- }
-
- var savedReportMatchFailures = reportMatchFailures;
- reportMatchFailures = false;
- var savedPos0 = pos;
- var result2 = parse_ld();
- if (result2 !== null) {
- if (input.substr(pos, 1) === ">") {
- var result3 = ">";
- pos += 1;
- } else {
- var result3 = null;
- if (reportMatchFailures) {
- matchFailed("\">\"");
- }
- }
- if (result3 !== null) {
- var result10 = parse_key();
- var result9 = result10 !== null
- ? (function(k) {return ["literal", k]})(result10)
- : null;
- if (result9 !== null) {
- var result4 = result9;
- } else {
- var result8 = parse_inline();
- if (result8 !== null) {
- var result4 = result8;
- } else {
- var result4 = null;;
- };
- }
- if (result4 !== null) {
- var result5 = parse_context();
- if (result5 !== null) {
- if (input.substr(pos, 1) === "/") {
- var result6 = "/";
- pos += 1;
- } else {
- var result6 = null;
- if (reportMatchFailures) {
- matchFailed("\"/\"");
- }
- }
- if (result6 !== null) {
- var result7 = parse_rd();
- if (result7 !== null) {
- var result1 = [result2, result3, result4, result5, result6, result7];
- } else {
- var result1 = null;
- pos = savedPos0;
- }
- } else {
- var result1 = null;
- pos = savedPos0;
- }
- } else {
- var result1 = null;
- pos = savedPos0;
- }
- } else {
- var result1 = null;
- pos = savedPos0;
- }
- } else {
- var result1 = null;
- pos = savedPos0;
- }
- } else {
- var result1 = null;
- pos = savedPos0;
- }
- var result0 = result1 !== null
- ? (function(n, c) { return ["partial", n, c] })(result1[2], result1[3])
- : null;
- reportMatchFailures = savedReportMatchFailures;
- if (reportMatchFailures && result0 === null) {
- matchFailed("partial");
- }
-
- cache[cacheKey] = {
- nextPos: pos,
- result: result0
- };
- return result0;
- }
-
- function parse_filters() {
- var cacheKey = "filters" + '@' + pos;
- var cachedResult = cache[cacheKey];
- if (cachedResult) {
- pos = cachedResult.nextPos;
- return cachedResult.result;
- }
-
- var savedReportMatchFailures = reportMatchFailures;
- reportMatchFailures = false;
- var result1 = [];
- var savedPos0 = pos;
- if (input.substr(pos, 1) === "|") {
- var result4 = "|";
- pos += 1;
- } else {
- var result4 = null;
- if (reportMatchFailures) {
- matchFailed("\"|\"");
- }
- }
- if (result4 !== null) {
- var result5 = parse_key();
- if (result5 !== null) {
- var result3 = [result4, result5];
- } else {
- var result3 = null;
- pos = savedPos0;
- }
- } else {
- var result3 = null;
- pos = savedPos0;
- }
- var result2 = result3 !== null
- ? (function(n) {return n})(result3[1])
- : null;
- while (result2 !== null) {
- result1.push(result2);
- var savedPos0 = pos;
- if (input.substr(pos, 1) === "|") {
- var result4 = "|";
- pos += 1;
- } else {
- var result4 = null;
- if (reportMatchFailures) {
- matchFailed("\"|\"");
- }
- }
- if (result4 !== null) {
- var result5 = parse_key();
- if (result5 !== null) {
- var result3 = [result4, result5];
- } else {
- var result3 = null;
- pos = savedPos0;
- }
- } else {
- var result3 = null;
- pos = savedPos0;
- }
- var result2 = result3 !== null
- ? (function(n) {return n})(result3[1])
- : null;
- }
- var result0 = resu