Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nunjucks

Package Overview
Dependencies
Maintainers
1
Versions
58
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nunjucks - npm Package Compare versions

Comparing version 0.1.4 to 0.1.5

tests/compiler.js

2

browser/nunjucks-min.js

@@ -1,1 +0,1 @@

(function(){var a={};(function(){function b(a,c,d){var e=Object.create(a.prototype),f=/xyz/.test(function(){xyz})?/\bparent\b/:/.*/;d=d||{};for(var g in d){var h=d[g],i=e[g];typeof i=="function"&&typeof h=="function"&&f.test(h)?e[g]=function(a,b){return function(){var c=this.parent;this.parent=b;var d=a.apply(this,arguments);return this.parent=c,d}}(h,i):e[g]=h}e.typename=c;var j=function(){e.init&&e.init.apply(this,arguments)};return j.prototype=e,j.prototype.constructor=j,j.extend=function(a,c){return typeof a=="object"&&(c=a,a="anonymous"),b(j,a,c)},j}a.object=b(Object,"Object",{})})(),function(){var b=Array.prototype,c=Object.prototype,d=a.lib={};d.isFunction=function(a){return c.toString.call(a)=="[object Function]"},d.isArray=Array.isArray||function(a){return c.toString.call(a)=="[object Array]"},d.isString=function(a){return c.toString.call(a)=="[object String]"},d.isObject=function(a){return a===Object(a)},d.groupBy=function(a,b){var c={},e=d.isFunction(b)?b:function(a){return a[b]};for(var f=0;f<a.length;f++){var g=a[f],h=e(g,f);(c[h]||(c[h]=[])).push(g)}return c},d.toArray=function(a){return Array.prototype.slice.call(a)},d.without=function(a){var b=[];if(!a)return b;var c=-1,e=a.length,f=d.toArray(arguments).slice(1);while(++c<e)f.indexOf(a[c])===-1&&b.push(a[c]);return b},d.extend=function(a,b){for(var c in b)a[c]=b[c];return a},d.repeat=function(a,b){var c="";for(var d=0;d<b;d++)c+=a;return c},d.map=function(a,c){var d=[];if(a==null)return d;if(b.map&&a.map===b.map)return a.map(c);for(var e=0;e<a.length;e++)d[d.length]=c(a[e],e);return a.length===+a.length&&(d.length=a.length),d}}(),function(){var b=a.lib,c={abs:function(a){return Math.abs(a)},batch:function(a,b,c){var d=[],e=[];for(var f=0;f<a.length;f++)f%b===0&&e.length&&(d.push(e),e=[]),e.push(a[f]);if(e.length){if(c)for(var f=e.length;f<b;f++)e.push(c);d.push(e)}return d},capitalize:function(a){return a=a.toLowerCase(),a[0].toUpperCase()+a.slice(1)},center:function(a,c){c=c||80;if(a.length>=c)return a;var d=c-a.length,e=b.repeat(" ",d/2-d%2),f=b.repeat(" ",d/2);return e+a+f},"default":function(a,b){return a?a:b},escape:function(a){return a.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/'/g,"&#39;").replace(/</g,"&lt;").replace(/>/g,"&gt;")},first:function(a){return a[0]},groupby:function(a,c){return b.groupBy(a,c)},indent:function(a,c,d){c=c||4;var e="",f=a.split("\n"),g=b.repeat(" ",c);for(var h=0;h<f.length;h++)h==0&&!d?e+=f[h]+"\n":e+=g+f[h]+"\n";return e},join:function(a,c,d){return c=c||"",d&&(a=b.map(a,function(a){return a[d]})),a.join(c)},last:function(a){return a[a.length-1]},length:function(a){return a.length},list:function(a){if(b.isString(a))return a.split("");if(b.isObject(a)){var c=[];if(Object.keys)c=Object.keys(a);else for(var d in a)c.push(d);return b.map(c,function(b){return{key:b,value:a[b]}})}throw new Error("list: type not iterable")},lower:function(a){return a.toLowerCase()},random:function(a){var b=Math.floor(Math.random()*a.length);return b==a.length&&b--,a[b]},replace:function(a,b,c,d){var e=a,f=e,g=1;e=e.replace(b,c);while(f!=e){if(g>=d)break;f=e,e=e.replace(b,c),g++}return e},reverse:function(a){var d;return b.isString(a)?d=c.list(a):d=b.map(a,function(a){return a}),d.reverse(),b.isString(a)?d.join(""):d},round:function(a,b,c){b=b||0;var d=Math.pow(10,b),e;return c=="ceil"?e=Math.ceil:c=="floor"?e=Math.floor:e=Math.round,e(a*d)/d},slice:function(a,b,c){var d=Math.floor(a.length/b),e=a.length%b,f=0,g=[];for(var h=0;h<b;h++){var i=f+h*d;h<e&&f++;var j=f+(h+1)*d,k=a.slice(i,j);c&&h>=e&&k.push(c),g.push(k)}return g},sort:function(a,c,d,e){return a=b.map(a,function(a){return a}),a.sort(function(a,f){var g,h;return e?(g=a[e],h=f[e]):(g=a,h=f),!d&&b.isString(g)&&b.isString(h)&&(g=g.toLowerCase(),h=h.toLowerCase()),g<h?c?1:-1:g>h?c?-1:1:0}),a},string:function(a){return a.toString()},title:function(a){return a.toUpperCase()},trim:function(a){return a.replace(/^\s*|\s*$/g,"")},upper:function(a){return a.toUpperCase()},wordcount:function(a){return a.match(/\w+/g).length},"float":function(a,b){return parseFloat(a)||b},"int":function(a,b){return parseInt(a)||b}};c.d=c.default,c.e=c.escape,a.filters=c}(),function(){var b=a.object,c=b.extend({init:function(a){this.variables={},this.parent=a},set:function(a,b){var c=a.split("."),d=this.variables;for(var e=0;e<c.length-1;e++){var f=c[e];d[f]||(d[f]={}),d=d[f]}d[c[c.length-1]]=b},lookup:function(a){var b=this.parent;return this.variables[a]||b&&b.lookup(a)},push:function(){return new c(this)},pop:function(){return this.parent}});a.runtime={Frame:c}}(),function(){var b=a.lib,c=a.object,d=a.lexer,e=a.compiler,f=a.filters,g=a.loaders,h=a.runtime.Frame,i=c.extend({init:function(a,c){a?this.loaders=b.isArray(a)?a:[a]:g.FileSystemLoader?this.loaders=[new g.FileSystemLoader]:this.loaders=[new g.HttpLoader("/views")],c&&d.setTags(c),this.filters=f,this.cache={}},addFilter:function(a,b){this.filters[a]=b},getFilter:function(a){return this.filters[a]},getTemplate:function(a,b){var c=null,d=this.cache[a],e;if(!d||!d.isUpToDate()){for(var f=0;f<this.loaders.length;f++)if(c=this.loaders[f].getSource(a))break;if(!c)throw new Error("template not found: "+a);this.cache[a]=new k(c.src,this,c.path,c.upToDate,b)}return this.cache[a]},registerPrecompiled:function(a){for(var b in a)this.cache[b]=new k({type:"code",obj:a[b]},this,b,function(){return!0},!0)},express:function(a){var c=this;a.render=function(d,e,f){var g={};b.isFunction(e)&&(f=e,e={}),g=b.extend(g,a.locals),e._locals&&(g=b.extend(g,e._locals)),g=b.extend(g,e);var h=c.render(d,e);f(null,h)}},render:function(a,b){return this.getTemplate(a).render(b)}}),j=c.extend({init:function(a,b){this.ctx=a,this.blocks={};for(var c in b)this.addBlock(c,b[c])},lookup:function(a){return this.ctx[a]},setVariable:function(a,b){this.ctx[a]=b},getVariables:function(){return this.ctx},addBlock:function(a,b){this.blocks[a]=this.blocks[a]||[],this.blocks[a].push(b)},getBlock:function(a){if(!this.blocks[a])throw new Error('unknown block "'+a+'"');return this.blocks[a][0]},getSuper:function(a,b,c){var d=(this.blocks[b]||[]).indexOf(c),e=this.blocks[b][d+1],f=this;return function(){if(d==-1||!e)throw new Error('no super block available for "'+b+'"');return e(a,f)}}}),k=c.extend({init:function(a,c,d,e,f){this.env=c||new i;if(b.isObject(a))switch(a.type){case"code":this.tmplProps=a.obj;break;case"string":this.tmplStr=a.obj}else{if(!b.isString(a))throw new Error("src must be a string or an object describing the source");this.tmplStr=a}this.path=d,this.upToDate=e||function(){return!1},f?this._compile():this.compiled=!1},render:function(a,b){this.compiled||this._compile();var c=new j(a||{},this.blocks);return this.rootRenderFunc(this.env,c,b||new h)},isUpToDate:function(){return this.upToDate()},_compile:function(){var a;if(this.tmplProps)a=this.tmplProps;else{var b=new Function(e.compile(this.tmplStr,this.env));a=b()}this.blocks=this._getBlocks(a),this.rootRenderFunc=a.root,this.compiled=!0},_getBlocks:function(a){var b={};for(var c in a)c.slice(0,2)=="b_"&&(b[c.slice(2)]=a[c]);return b}});a.environment={Environment:i,Template:k}}();var b=a.environment,c=a.compiler,d=a.parser,e=a.lexer,f=a.loaders;window.nunjucks={},window.nunjucks.Environment=b.Environment,window.nunjucks.Template=b.Template,f.FileSystemLoader?window.nunjucks.FileSystemLoader=f.FileSystemLoader:window.nunjucks.HttpLoader=f.HttpLoader,window.nunjucks.compiler=c,window.nunjucks.parser=d,window.nunjucks.lexer=e,window.nunjucks.require=function(b){return a[b]}})();
(function(){var a={};(function(){function b(a,c,d){var e=Object.create(a.prototype),f=/xyz/.test(function(){xyz})?/\bparent\b/:/.*/;d=d||{};for(var g in d){var h=d[g],i=e[g];typeof i=="function"&&typeof h=="function"&&f.test(h)?e[g]=function(a,b){return function(){var c=this.parent;this.parent=b;var d=a.apply(this,arguments);return this.parent=c,d}}(h,i):e[g]=h}e.typename=c;var j=function(){e.init&&e.init.apply(this,arguments)};return j.prototype=e,j.prototype.constructor=j,j.extend=function(a,c){return typeof a=="object"&&(c=a,a="anonymous"),b(j,a,c)},j}a.object=b(Object,"Object",{})})(),function(){var b=Array.prototype,c=Object.prototype,d=a.lib={};d.isFunction=function(a){return c.toString.call(a)=="[object Function]"},d.isArray=Array.isArray||function(a){return c.toString.call(a)=="[object Array]"},d.isString=function(a){return c.toString.call(a)=="[object String]"},d.isObject=function(a){return a===Object(a)},d.groupBy=function(a,b){var c={},e=d.isFunction(b)?b:function(a){return a[b]};for(var f=0;f<a.length;f++){var g=a[f],h=e(g,f);(c[h]||(c[h]=[])).push(g)}return c},d.toArray=function(a){return Array.prototype.slice.call(a)},d.without=function(a){var b=[];if(!a)return b;var c=-1,e=a.length,f=d.toArray(arguments).slice(1);while(++c<e)f.indexOf(a[c])===-1&&b.push(a[c]);return b},d.extend=function(a,b){for(var c in b)a[c]=b[c];return a},d.repeat=function(a,b){var c="";for(var d=0;d<b;d++)c+=a;return c},d.each=function(a,c,d){if(a==null)return;if(b.each&&a.each==b.each)a.forEach(c,d);else if(a.length===+a.length)for(var e=0,f=a.length;e<f;e++)c.call(d,a[e],e,a)},d.map=function(a,c){var d=[];if(a==null)return d;if(b.map&&a.map===b.map)return a.map(c);for(var e=0;e<a.length;e++)d[d.length]=c(a[e],e);return a.length===+a.length&&(d.length=a.length),d}}(),function(){var b=a.lib,c={abs:function(a){return Math.abs(a)},batch:function(a,b,c){var d=[],e=[];for(var f=0;f<a.length;f++)f%b===0&&e.length&&(d.push(e),e=[]),e.push(a[f]);if(e.length){if(c)for(var f=e.length;f<b;f++)e.push(c);d.push(e)}return d},capitalize:function(a){return a=a.toLowerCase(),a[0].toUpperCase()+a.slice(1)},center:function(a,c){c=c||80;if(a.length>=c)return a;var d=c-a.length,e=b.repeat(" ",d/2-d%2),f=b.repeat(" ",d/2);return e+a+f},"default":function(a,b){return a?a:b},escape:function(a){return a.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/'/g,"&#39;").replace(/</g,"&lt;").replace(/>/g,"&gt;")},first:function(a){return a[0]},groupby:function(a,c){return b.groupBy(a,c)},indent:function(a,c,d){c=c||4;var e="",f=a.split("\n"),g=b.repeat(" ",c);for(var h=0;h<f.length;h++)h==0&&!d?e+=f[h]+"\n":e+=g+f[h]+"\n";return e},join:function(a,c,d){return c=c||"",d&&(a=b.map(a,function(a){return a[d]})),a.join(c)},last:function(a){return a[a.length-1]},length:function(a){return a.length},list:function(a){if(b.isString(a))return a.split("");if(b.isObject(a)){var c=[];if(Object.keys)c=Object.keys(a);else for(var d in a)c.push(d);return b.map(c,function(b){return{key:b,value:a[b]}})}throw new Error("list: type not iterable")},lower:function(a){return a.toLowerCase()},random:function(a){var b=Math.floor(Math.random()*a.length);return b==a.length&&b--,a[b]},replace:function(a,b,c,d){var e=a,f=e,g=1;e=e.replace(b,c);while(f!=e){if(g>=d)break;f=e,e=e.replace(b,c),g++}return e},reverse:function(a){var d;return b.isString(a)?d=c.list(a):d=b.map(a,function(a){return a}),d.reverse(),b.isString(a)?d.join(""):d},round:function(a,b,c){b=b||0;var d=Math.pow(10,b),e;return c=="ceil"?e=Math.ceil:c=="floor"?e=Math.floor:e=Math.round,e(a*d)/d},slice:function(a,b,c){var d=Math.floor(a.length/b),e=a.length%b,f=0,g=[];for(var h=0;h<b;h++){var i=f+h*d;h<e&&f++;var j=f+(h+1)*d,k=a.slice(i,j);c&&h>=e&&k.push(c),g.push(k)}return g},sort:function(a,c,d,e){return a=b.map(a,function(a){return a}),a.sort(function(a,f){var g,h;return e?(g=a[e],h=f[e]):(g=a,h=f),!d&&b.isString(g)&&b.isString(h)&&(g=g.toLowerCase(),h=h.toLowerCase()),g<h?c?1:-1:g>h?c?-1:1:0}),a},string:function(a){return a.toString()},title:function(a){return a.toUpperCase()},trim:function(a){return a.replace(/^\s*|\s*$/g,"")},upper:function(a){return a.toUpperCase()},wordcount:function(a){return a.match(/\w+/g).length},"float":function(a,b){return parseFloat(a)||b},"int":function(a,b){return parseInt(a)||b}};c.d=c.default,c.e=c.escape,a.filters=c}(),function(){function d(a,b,c){return function(){var d=g(arguments),e,h=f(arguments);if(d>a.length){e=Array.prototype.slice.call(arguments,0,a.length);var i=Array.prototype.slice.call(arguments,e.length,d);for(var j=0;j<i.length;j++)j<b.length&&(h[b[j]]=i[j]);e.push(h)}else if(d<a.length){e=Array.prototype.slice.call(arguments,0,d);for(var j=d;j<a.length;j++){var k=a[j];e.push(h[k]),delete h[k]}e.push(h)}else e=arguments;return c.apply(this,e)}}function e(a){return a.__keywords=!0,a}function f(a){return a.length&&a[a.length-1].__keywords?a[a.length-1]:{}}function g(a){return a.length===0?0:a[a.length-1].__keywords?a.length-1:a.length}var b=a.object,c=b.extend({init:function(a){this.variables={},this.parent=a},set:function(a,b){var c=a.split("."),d=this.variables;for(var e=0;e<c.length-1;e++){var f=c[e];d[f]||(d[f]={}),d=d[f]}d[c[c.length-1]]=b},lookup:function(a){var b=this.parent;return this.variables[a]||b&&b.lookup(a)},push:function(){return new c(this)},pop:function(){return this.parent}});a.runtime={Frame:c,makeMacro:d,makeKeywordArgs:e,numArgs:g}}(),function(){var b=a.lib,c=a.object,d=a.lexer,e=a.compiler,f=a.filters,g=a.loaders,h=a.runtime,i=h.Frame,j=c.extend({init:function(a,c){a?this.loaders=b.isArray(a)?a:[a]:g.FileSystemLoader?this.loaders=[new g.FileSystemLoader]:this.loaders=[new g.HttpLoader("/views")],c&&d.setTags(c),this.filters=f,this.cache={}},addFilter:function(a,b){this.filters[a]=b},getFilter:function(a){return this.filters[a]},getTemplate:function(a,b){var c=null,d=this.cache[a],e;if(!d||!d.isUpToDate()){for(var f=0;f<this.loaders.length;f++)if(c=this.loaders[f].getSource(a))break;if(!c)throw new Error("template not found: "+a);this.cache[a]=new l(c.src,this,c.path,c.upToDate,b)}return this.cache[a]},registerPrecompiled:function(a){for(var b in a)this.cache[b]=new l({type:"code",obj:a[b]},this,b,function(){return!0},!0)},express:function(a){var c=this;a.render=function(d,e,f){var g={};b.isFunction(e)&&(f=e,e={}),g=b.extend(g,a.locals),e._locals&&(g=b.extend(g,e._locals)),g=b.extend(g,e);var h=c.render(d,g);f(null,h)}},render:function(a,b){return this.getTemplate(a).render(b)}}),k=c.extend({init:function(a,b){this.ctx=a,this.blocks={},this.exported=[];for(var c in b)this.addBlock(c,b[c])},lookup:function(a){return this.ctx[a]},setVariable:function(a,b){this.ctx[a]=b},getVariables:function(){return this.ctx},addBlock:function(a,b){this.blocks[a]=this.blocks[a]||[],this.blocks[a].push(b)},getBlock:function(a){if(!this.blocks[a])throw new Error('unknown block "'+a+'"');return this.blocks[a][0]},getSuper:function(a,b,c){var d=(this.blocks[b]||[]).indexOf(c),e=this.blocks[b][d+1],f=this;return function(){if(d==-1||!e)throw new Error('no super block available for "'+b+'"');return e(a,f)}},addExport:function(a){this.exported.push(a)},getExported:function(){var a={};for(var b=0;b<this.exported.length;b++){var c=this.exported[b];a[c]=this.ctx[c]}return a}}),l=c.extend({init:function(a,c,d,e,f){this.env=c||new j;if(b.isObject(a))switch(a.type){case"code":this.tmplProps=a.obj;break;case"string":this.tmplStr=a.obj}else{if(!b.isString(a))throw new Error("src must be a string or an object describing the source");this.tmplStr=a}this.path=d,this.upToDate=e||function(){return!1},f?this._compile():this.compiled=!1},render:function(a,b){this.compiled||this._compile();var c=new k(a||{},this.blocks);return this.rootRenderFunc(this.env,c,b||new i,h)},isUpToDate:function(){return this.upToDate()},getExported:function(){this.compiled||this._compile();var a=new k({},this.blocks);return this.rootRenderFunc(this.env,a,new i,h),a.getExported()},_compile:function(){var a;if(this.tmplProps)a=this.tmplProps;else{var b=new Function(e.compile(this.tmplStr,this.env));a=b()}this.blocks=this._getBlocks(a),this.rootRenderFunc=a.root,this.compiled=!0},_getBlocks:function(a){var b={};for(var c in a)c.slice(0,2)=="b_"&&(b[c.slice(2)]=a[c]);return b}});a.environment={Environment:j,Template:l}}();var b=a.environment,c=a.compiler,d=a.parser,e=a.lexer,f=a.loaders;window.nunjucks={},window.nunjucks.Environment=b.Environment,window.nunjucks.Template=b.Template,f.FileSystemLoader?window.nunjucks.FileSystemLoader=f.FileSystemLoader:window.nunjucks.HttpLoader=f.HttpLoader,window.nunjucks.compiler=c,window.nunjucks.parser=d,window.nunjucks.lexer=e,window.nunjucks.require=function(b){return a[b]}})();

@@ -131,2 +131,17 @@ (function() {

exports.each = function(obj, func, context) {
if(obj == null) {
return;
}
if(ArrayProto.each && obj.each == ArrayProto.each) {
obj.forEach(func, context);
}
else if(obj.length === +obj.length) {
for(var i=0, l=obj.length; i<l; i++) {
func.call(context, obj[i], i, obj);
}
}
};
exports.map = function(obj, func) {

@@ -496,8 +511,77 @@ var results = [];

modules['runtime'] = {
Frame: Frame
function makeMacro(argNames, kwargNames, func) {
return function() {
var argCount = numArgs(arguments);
var args;
var kwargs = getKeywordArgs(arguments);
if(argCount > argNames.length) {
args = Array.prototype.slice.call(arguments, 0, argNames.length);
// Positional arguments that should be passed in as
// keyword arguments (essentially default values)
var vals = Array.prototype.slice.call(arguments, args.length, argCount);
for(var i=0; i<vals.length; i++) {
if(i < kwargNames.length) {
kwargs[kwargNames[i]] = vals[i];
}
}
args.push(kwargs);
}
else if(argCount < argNames.length) {
args = Array.prototype.slice.call(arguments, 0, argCount);
for(var i=argCount; i<argNames.length; i++) {
var arg = argNames[i];
// Keyword arguments that should be passed as
// positional arguments, i.e. the caller explicitly
// used the name of a positional arg
args.push(kwargs[arg]);
delete kwargs[arg];
}
args.push(kwargs);
}
else {
args = arguments;
}
return func.apply(this, args);
};
}
function makeKeywordArgs(obj) {
obj.__keywords = true;
return obj;
}
function getKeywordArgs(args) {
if(args.length && args[args.length - 1].__keywords) {
return args[args.length - 1];
}
return {};
}
function numArgs(args) {
if(args.length === 0) {
return 0;
}
else if(args[args.length - 1].__keywords) {
return args.length - 1;
}
else {
return args.length;
}
}
modules['runtime'] = {
Frame: Frame,
makeMacro: makeMacro,
makeKeywordArgs: makeKeywordArgs,
numArgs: numArgs
};
})();
(function() {
var lib = modules["lib"];

@@ -509,3 +593,4 @@ var Object = modules["object"];

var builtin_loaders = modules["loaders"];
var Frame = modules["runtime"].Frame;
var runtime = modules["runtime"];
var Frame = runtime.Frame;

@@ -599,4 +684,4 @@ var Environment = Object.extend({

var res = env.render(name, ctx);
k(null, res);
var res = env.render(name, context);
k(null, res);
};

@@ -614,2 +699,3 @@ },

this.blocks = {};
this.exported = [];

@@ -628,3 +714,3 @@ for(var name in blocks) {

},
getVariables: function() {

@@ -659,2 +745,15 @@ return this.ctx;

};
},
addExport: function(name) {
this.exported.push(name);
},
getExported: function() {
var exported = {};
for(var i=0; i<this.exported.length; i++) {
var name = this.exported[i];
exported[name] = this.ctx[name];
}
return exported;
}

@@ -700,3 +799,4 @@ });

context,
frame || new Frame());
frame || new Frame(),
runtime);
},

@@ -708,2 +808,16 @@

getExported: function() {
if(!this.compiled) {
this._compile();
}
// Run the rootRenderFunc to populate the context with exported vars
var context = new Context({}, this.blocks);
this.rootRenderFunc(this.env,
context,
new Frame(),
runtime);
return context.getExported();
},
_compile: function() {

@@ -719,3 +833,3 @@ var props;

}
this.blocks = this._getBlocks(props);

@@ -740,4 +854,5 @@ this.rootRenderFunc = props.root;

// var fs = modules["fs"];
// //var src = fs.readFileSync('test.html', 'utf-8');
// var src = "{% for i in [1,2,3] %}{% include 'item.html' %}{% endfor %}";
// var src = fs.readFileSync('test.html', 'utf-8');
// //var src = '{% macro foo(x, y, z=3) %}h{% endmacro %}';
// //var src = '{% macro foo() %}{{ h }}{% endmacro %} {{ foo() }}';

@@ -744,0 +859,0 @@ // var env = new Environment();

{
"name": "nunjucks",
"description": "A jinja inspired templating engine",
"version": "0.1.4",
"version": "0.1.5",
"author": {

@@ -10,3 +10,4 @@ "name": "James Long",

"devDependencies": {
"should": "1.1.0"
"should": "*",
"mocha": "*"
},

@@ -13,0 +14,0 @@ "engines": {

@@ -19,3 +19,3 @@

// A common pattern is to emit binary operators
// A common pattern is to emit binary operators
function binOpEmitter(str) {

@@ -29,2 +29,9 @@ return function(node, frame) {

// Generate an array of strings
function quotedArray(arr) {
return '[' +
lib.map(arr, function(x) { return '"' + x + '"'; }) +
']';
}
var Compiler = Object.extend({

@@ -46,5 +53,11 @@ init: function() {

emitLines: function() {
lib.each(lib.toArray(arguments), function(line) {
this.emitLine(line);
}, this);
},
emitFuncBegin: function(name) {
this.buffer = 'output';
this.emitLine('function ' + name + '(env, context, frame) {');
this.emitLine('function ' + name + '(env, context, frame, runtime) {');
this.emitLine('var ' + this.buffer + ' = "";');

@@ -68,6 +81,5 @@ },

_compileChildren: function(node, frame) {
var _this = this;
node.iterChildren(function(n) {
_this.compile(n, frame);
});
lib.each(node.children, function(n) {
this.compile(n, frame);
}, this);
},

@@ -165,4 +177,4 @@

compilePair: function(node, frame) {
var key = node.getKey();
var val = node.getValue();
var key = node.key;
var val = node.value;

@@ -173,3 +185,3 @@ if(key instanceof nodes.Symbol) {

else if(!(key instanceof nodes.Literal &&
typeof node.value == "string")) {
typeof key.value == "string")) {
throw new Error("Dict keys must be strings or names");

@@ -216,3 +228,3 @@ }

},
compilePos: function(node, frame) {

@@ -225,3 +237,3 @@ this.emit('+');

this.compile(node.expr, frame);
for(var i=0; i<node.ops.length; i++) {

@@ -247,3 +259,3 @@ var n = node.ops[i];

this.emit(')');
this._compileAggregate(node, frame, '(', ')');
this._compileAggregate(node.args, frame, '(', ')');
},

@@ -256,16 +268,36 @@

this.emit('env.getFilter("' + name.value + '")');
this._compileAggregate(node, frame, '(', ')');
this._compileAggregate(node.args, frame, '(', ')');
},
compileKeywordArgs: function(node, frame) {
var names = [];
lib.each(node.children, function(pair) {
names.push(pair.key.value);
});
this.emit('runtime.makeKeywordArgs(');
this.compileDict(node, frame);
this.emit(')');
},
compileSet: function(node, frame) {
var val = this.tmpid();
var id = this.tmpid();
this.emit('var ' + val + ' = ');
this.emit('var ' + id + ' = ');
this._compileExpression(node.value);
this.emitLine(';');
for(var i=0; i<node.targets.length; i++) {
var t = node.targets[i];
this.emitLine('context.setVariable("' + t.value + '", ' +
val + ');');
var name = node.targets[i].value;
frame.set(name, id);
this.emitLine('frame.set("' + name + '", ' + id + ');');
this.emitLine('if(!frame.parent) {');
this.emitLine('context.setVariable("' + name + '", ' + id + ');');
if(name.charAt(0) != '_') {
this.emitLine('context.addExport("' + name + '");');
}
this.emitLine('}');
}

@@ -293,3 +325,3 @@ },

this.emitLine('frame = frame.push()');
this.emitLine('frame = frame.push();');

@@ -345,5 +377,157 @@ this.emit('var ' + arr + ' = ');

_emitMacroBegin: function(node, frame) {
var args = [];
var kwargs = null;
var funcId = 'macro_' + this.tmpid();
// Type check the definition of the args
lib.each(node.args.children, function(arg, i) {
if(i === node.args.children.length - 1 &&
arg instanceof nodes.Dict) {
kwargs = arg;
}
else {
this.assertType(arg, nodes.Symbol);
args.push(arg);
}
}, this);
var realNames = lib.map(args, function(n) { return 'l_' + n.value; });
realNames.push('kwargs');
// Quoted argument names
var argNames = lib.map(args, function(n) { return '"' + n.value + '"'; });
var kwargNames = lib.map((kwargs && kwargs.children) || [],
function(n) { return '"' + n.key.value + '"'; });
// We pass a function to makeMacro which destructures the
// arguments so support setting positional args with keywords
// args and passing keyword args as positional args
// (essentially default values). See runtime.js.
this.emitLines(
'var ' + funcId + ' = runtime.makeMacro(',
'[' + argNames.join(', ') + '], ',
'[' + kwargNames.join(', ') + '], ',
'function (' + realNames.join(', ') + ') {',
'frame = frame.push();',
'kwargs = kwargs || {};'
);
// Expose the arguments to the template. Don't need to use
// random names because the function
// will create a new run-time scope for us
lib.each(args, function(arg) {
this.emitLine('frame.set("' + arg.value + '", ' +
'l_' + arg.value + ');');
frame.set(arg.value, 'l_' + arg.value);
}, this);
// Expose the keyword arguments
if(kwargs) {
lib.each(kwargs.children, function(pair) {
var name = pair.key.value;
this.emit('frame.set("' + name + '", ' +
'kwargs.hasOwnProperty("' + name + '") ? ' +
'kwargs["' + name + '"] : ');
this._compileExpression(pair.value);
this.emitLine(');');
}, this);
}
return funcId;
},
_emitMacroEnd: function() {
this.emitLine('frame = frame.pop();');
this.emitLine('return ' + this.buffer + ';');
this.emitLine('});');
},
compileMacro: function(node, frame) {
frame = frame.push();
var funcId = this._emitMacroBegin(node, frame);
// Start a new output buffer, and set the old one back after
// we're done
var prevBuffer = this.buffer;
this.buffer = 'output';
this.emitLine('var ' + this.buffer + '= "";');
this.compile(node.body, frame);
this._emitMacroEnd();
this.buffer = prevBuffer;
// Expose the macro to the templates
var name = node.name.value;
frame = frame.pop();
frame.set(name, funcId);
if(frame.parent) {
this.emitLine('frame.set("' + name + '", ' + funcId + ');');
}
else {
if(node.name.value.charAt(0) != '_') {
this.emitLine('context.addExport("' + name + '");');
}
this.emitLine('context.setVariable("' + name + '", ' + funcId + ');');
}
},
compileImport: function(node, frame) {
var id = this.tmpid();
var target = node.target.value;
this.emit('var ' + id + ' = env.getTemplate(');
this._compileExpression(node.template, frame);
this.emitLine(').getExported();');
frame.set(target, id);
if(frame.parent) {
this.emitLine('frame.set("' + target + '", ' + id + ');');
}
else {
this.emitLine('context.setVariable("' + target + '", ' + id + ');');
}
},
compileFromImport: function(node, frame) {
this.emit('var imported = env.getTemplate(');
this.compile(node.template, frame);
this.emitLine(').getExported();');
lib.each(node.names.children, function(nameNode) {
var name;
var alias;
var id = this.tmpid();
if(nameNode instanceof nodes.Pair) {
name = nameNode.key.value;
alias = nameNode.value.value;
}
else {
name = nameNode.value;
alias = name;
}
this.emitLine('if(imported.hasOwnProperty("' + name + '")) {');
this.emitLine('var ' + id + ' = imported.' + name + ';');
this.emitLine('} else {');
this.emitLine('throw new Error("cannot import \'' + name + '\'")');
this.emitLine('}');
frame.set(alias, id);
if(frame.parent) {
this.emitLine('frame.set("' + alias + '", ' + id + ');');
}
else {
this.emitLine('context.setVariable("' + alias + '", ' + id + ');');
}
}, this);
},
compileBlock: function(node, frame) {
this.emitLine(this.buffer + ' += context.getBlock("' +
node.name.value + '")(env, context, frame);');
node.name.value + '")(env, context, frame, runtime);');
},

@@ -355,3 +539,3 @@

}
this.emit('var parentTemplate = env.getTemplate(');

@@ -377,3 +561,3 @@ this._compileExpression(node.template, frame);

' += includeTemplate.render(' +
'context.getVariables(), frame);');
'context.getVariables(), frame.push());');
},

@@ -402,3 +586,3 @@

this.emitLine('return ' +
'parentTemplate.rootRenderFunc(env, context, frame);');
'parentTemplate.rootRenderFunc(env, context, frame, runtime);');
}

@@ -413,8 +597,9 @@ this.emitFuncEnd(this.isChild);

this.emitFuncBegin('b_' + name);
this.emitLine('var l_super = context.getSuper(env, ' +
this.emitLine('var l_super = context.getSuper(env, ' +
'"' + name + '", ' +
'b_' + name + ');');
'b_' + name + ', ' +
'runtime);');
var tmpFrame = frame.push();
frame.set('super', 'l_super');
var tmpFrame = new Frame();
tmpFrame.set('super', 'l_super');
this.compile(block.body, tmpFrame);

@@ -451,3 +636,3 @@

// var c = new Compiler();
// var src = "{{ test('hello') }}";
// var src = '{% macro foo(x, y, z=3) %}h{% endmacro %}';

@@ -454,0 +639,0 @@ // var ns = parser.parse(src);

@@ -7,3 +7,4 @@ var lib = require('./lib');

var builtin_loaders = require('./loaders');
var Frame = require('./runtime').Frame;
var runtime = require('./runtime');
var Frame = runtime.Frame;

@@ -98,3 +99,3 @@ var Environment = Object.extend({

var res = env.render(name, context);
k(null, res);
k(null, res);
};

@@ -112,2 +113,3 @@ },

this.blocks = {};
this.exported = [];

@@ -126,3 +128,3 @@ for(var name in blocks) {

},
getVariables: function() {

@@ -157,2 +159,15 @@ return this.ctx;

};
},
addExport: function(name) {
this.exported.push(name);
},
getExported: function() {
var exported = {};
for(var i=0; i<this.exported.length; i++) {
var name = this.exported[i];
exported[name] = this.ctx[name];
}
return exported;
}

@@ -198,3 +213,4 @@ });

context,
frame || new Frame());
frame || new Frame(),
runtime);
},

@@ -206,2 +222,16 @@

getExported: function() {
if(!this.compiled) {
this._compile();
}
// Run the rootRenderFunc to populate the context with exported vars
var context = new Context({}, this.blocks);
this.rootRenderFunc(this.env,
context,
new Frame(),
runtime);
return context.getExported();
},
_compile: function() {

@@ -217,3 +247,3 @@ var props;

}
this.blocks = this._getBlocks(props);

@@ -238,4 +268,5 @@ this.rootRenderFunc = props.root;

// var fs = require('fs');
// //var src = fs.readFileSync('test.html', 'utf-8');
// var src = "{% for i in [1,2,3] %}{% include 'item.html' %}{% endfor %}";
// var src = fs.readFileSync('test.html', 'utf-8');
// //var src = '{% macro foo(x, y, z=3) %}h{% endmacro %}';
// //var src = '{% macro foo() %}{{ h }}{% endmacro %} {{ foo() }}';

@@ -252,2 +283,2 @@ // var env = new Environment();

Template: Template
};
};

@@ -71,3 +71,3 @@

// We've hit a string
return token(TOKEN_STRING, this.parseString(), lineno, colno);
return token(TOKEN_STRING, this.parseString(cur), lineno, colno);
}

@@ -150,4 +150,4 @@ else if((tok = this._extract(whitespaceChars))) {

// tags (don't use the full delimChars for optimization)
var beginChars = (BLOCK_START[0] +
VARIABLE_START[0] +
var beginChars = (BLOCK_START[0] +
VARIABLE_START[0] +
COMMENT_START[0] +

@@ -223,10 +223,10 @@ COMMENT_END[0]);

Tokenizer.prototype.parseString = function() {
Tokenizer.prototype.parseString = function(delimiter) {
this.forward();
var lineno = this.lineno;
var colno = this.colno;
var str = "";
while(this.current() != "\"" && this.current() != "'") {
while(this.current() != delimiter) {
var cur = this.current();

@@ -233,0 +233,0 @@

@@ -69,2 +69,17 @@ var ArrayProto = Array.prototype;

exports.each = function(obj, func, context) {
if(obj == null) {
return;
}
if(ArrayProto.each && obj.each == ArrayProto.each) {
obj.forEach(func, context);
}
else if(obj.length === +obj.length) {
for(var i=0, l=obj.length; i<l; i++) {
func.call(context, obj[i], i, obj);
}
}
};
exports.map = function(obj, func) {

@@ -71,0 +86,0 @@ var results = [];

@@ -6,13 +6,19 @@

// TODO: Don't use a children array, but rather a single node "body"
var Node = Object.extend("Node", {
init: function(lineno, colno, children) {
if(children && !lib.isArray(children)) {
throw new Error("Node.init: third argument must be " +
"an array (the children)");
}
this.children = children || [];
init: function(lineno, colno) {
var args = lib.toArray(arguments).slice(2);
this.lineno = lineno;
this.colno = colno;
lib.each(this.fields, function(field, i) {
var val = args[i];
// Fields should never be undefined, but null. It makes
// testing easier to normalize values.
if(val === undefined) {
val = null;
}
this[field] = val;
}, this);
},

@@ -23,43 +29,31 @@

// TODO: clean this up
for(var k in this) {
var obj = this[k];
function check(obj) {
if(obj instanceof type) {
res.push(obj);
}
if(lib.isArray(obj)) {
for(var i=0; i<obj.length; i++) {
var n = obj[i];
if(n instanceof type) {
res.push(n);
}
if(n instanceof Node) {
res = res.concat(n.findAll(type));
}
};
}
else if(obj instanceof Node) {
if(obj instanceof Node) {
res = res.concat(obj.findAll(type));
}
}
}
if(this instanceof NodeList) {
lib.each(this.children, function(node) {
check(node);
}, this);
}
else {
lib.each(this.fields, function(field) {
var obj = this[field];
check(obj);
}, this);
}
return res;
},
addChild: function(node) {
this.children.push(node);
},
getChild: function(i) {
return this.children[i];
},
numChildren: function() {
return this.children.length;
},
iterChildren: function(func) {
for(var i=0; i<this.children.length; i++) {
func(this.children[i]);
}
iterFields: function(func) {
lib.each(this.fields, function(field) {
func(this[field], field);
}, this);
}

@@ -69,104 +63,50 @@ });

// Abstract nodes
var Expr = Node.extend("Expr");
var Value = Expr.extend("Value", {
init: function (lineno, colno, value) {
this.value = value;
this.parent(lineno, colno);
}
});
var Value = Node.extend("Value", { fields: ['value'] });
// Concrete nodes
var Root = Node.extend("Root");
var NodeList = Node.extend("NodeList");
var Literal = Value.extend("Literal");
var Symbol = Value.extend("Symbol");
var NodeList = Node.extend("NodeList", {
fields: ['children'],
var Group = Expr.extend("Group");
var Array = Expr.extend("Array");
var Pair = Node.extend("Pair", {
init: function(lineno, colno, key, val) {
this.parent(lineno, colno, [key, val]);
init: function(lineno, colno, nodes) {
this.parent(lineno, colno, nodes || []);
},
getKey: function() {
return this.children[0];
},
getValue: function() {
return this.children[1];
addChild: function(node) {
this.children.push(node);
}
});
var Dict = Expr.extend("Dict");
var LookupVal = Expr.extend("LookupVal", {
init: function(lineno, colno, target, val) {
this.target = target;
this.val = val;
this.parent(lineno, colno);
}
});
var If = Node.extend("If", {
init: function(lineno, colno, cond, body, else_) {
this.cond = cond;
this.body = body;
this.else_ = else_;
this.parent(lineno, colno);
}
});
var Root = NodeList.extend("Root");
var Literal = Value.extend("Literal");
var Symbol = Value.extend("Symbol");
var Group = NodeList.extend("Group");
var Array = NodeList.extend("Array");
var Pair = Node.extend("Pair", { fields: ['key', 'value'] });
var Dict = NodeList.extend("Dict");
var LookupVal = Node.extend("LookupVal", { fields: ['target', 'val'] });
var If = Node.extend("If", { fields: ['cond', 'body', 'else_'] });
var For = Node.extend("For", { fields: ['arr', 'name', 'body'] });
var Macro = Node.extend("Macro", { fields: ['name', 'args', 'body'] });
var Import = Node.extend("Import", { fields: ['template', 'target'] });
var FromImport = Node.extend("FromImport", {
fields: ['template', 'names'],
var For = Node.extend("For", {
init: function(lineno, colno, arr, name, body) {
this.arr = arr;
this.name = name;
this.body = body;
this.parent(lineno, colno);
init: function(lineno, colno, template, names) {
this.parent(lineno, colno,
template,
names || new NodeList());
}
});
var FunCall = Node.extend("FunCall", {
init: function(lineno, colno, name, args) {
this.name = name;
this.parent(lineno, colno, args);
}
});
var FunCall = Node.extend("FunCall", { fields: ['name', 'args'] });
var Filter = FunCall.extend("Filter");
var Block = Node.extend("Block", {
init: function(lineno, colno, name, body) {
this.name = name;
this.body = body;
this.parent(lineno, colno);
}
});
var TemplateRef = Node.extend("TemplateRef", {
init: function(lineno, colno, template) {
this.template = template;
this.parent(lineno, colno);
}
});
var KeywordArgs = Dict.extend("KeywordArgs");
var Block = Node.extend("Block", { fields: ['name', 'body'] });
var TemplateRef = Node.extend("TemplateRef", { fields: ['template'] });
var Extends = TemplateRef.extend("Extends");
var Include = TemplateRef.extend("Include");
var Set = Node.extend("Set");
var Output = Node.extend("Output");
var Set = Node.extend("Set", { fields: ['targets', 'value'] });
var Output = NodeList.extend("Output");
var TemplateData = Literal.extend("TemplateData");
var UnaryOp = Expr.extend("UnaryOp", {
init: function(lineno, colno, target) {
this.target = target;
this.parent(lineno, colno);
}
});
var BinOp = Expr.extend("BinOp", {
init: function(lineno, colno, left, right) {
this.left = left;
this.right = right;
this.parent(lineno, colno);
}
});
var UnaryOp = Node.extend("UnaryOp", { fields: ['target'] });
var BinOp = Node.extend("BinOp", { fields: ['left', 'right'] });
var Or = BinOp.extend("Or");

@@ -184,24 +124,15 @@ var And = BinOp.extend("And");

var Pos = UnaryOp.extend("Pos");
var Compare = Expr.extend("Compare", {
init: function(lineno, colno, expr, ops) {
this.expr = expr;
this.ops = ops;
this.parent(lineno, colno);
}
var Compare = Node.extend("Compare", { fields: ['expr', 'ops'] });
var CompareOperand = Node.extend("CompareOperand", {
fields: ['expr', 'type']
});
var CompareOperand = Expr.extend("CompareOperand", {
init: function(lineno, colno, expr, type) {
this.expr = expr;
this.type = type;
this.parent(lineno, colno);
}
});
// Print the AST in a nicely formatted tree format for debuggin
function printNodes(node, indent) {
// TODO: spend more then 30 seconds and clean this up
indent = indent || 0;
function print(str, inline) {
// This is hacky, but this is just a debugging function anyway
function print(str, indent, inline) {
var lines = str.split("\n");
for(var i=0; i<lines.length; i++) {

@@ -225,79 +156,49 @@ if(lines[i]) {

print(node.typename + ": ");
print(node.typename + ": ", indent);
if(node instanceof FunCall) {
print("\n");
printNodes(node.name, indent+2);
node.iterChildren(function(node) {
printNodes(node, indent+2);
if(node instanceof NodeList) {
print('\n');
lib.each(node.children, function(n) {
printNodes(n, indent + 2);
});
}
else if(node instanceof For) {
print("\n");
printNodes(node.name, indent+2);
printNodes(node.arr, indent+2);
printNodes(node.body, indent+2);
}
else if(node instanceof If) {
print("\n");
printNodes(node.cond, indent+2);
printNodes(node.body, indent+2);
else {
var nodes = null;
var props = null;
if(node.else_) {
printNodes(node.else_, indent+2);
node.iterFields(function(val, field) {
if(val instanceof Node) {
nodes = nodes || {};
nodes[field] = val;
}
else {
props = props || {};
props[field] = val;
}
});
if(props) {
print(util.inspect(props, true, null) + '\n', null, true);
}
}
else if(node instanceof Block) {
print("\n");
printNodes(node.name, indent+2);
printNodes(node.body, indent+2);
}
else if(node instanceof Set) {
print("\n");
for(var i=0; i<node.targets.length; i++) {
printNodes(node.targets[i], indent+2);
else {
print('\n');
}
printNodes(node.value, indent+2);
}
else if(node instanceof LookupVal) {
print("\n");
printNodes(node.target, indent+2);
printNodes(node.val, indent+2);
}
else if(node instanceof UnaryOp) {
print("\n");
printNodes(node.target, indent+2);
}
else if(node instanceof BinOp) {
print("\n");
printNodes(node.left, indent+2);
printNodes(node.right, indent+2);
}
else if(node instanceof Compare) {
print("\n");
printNodes(node.expr, indent+2);
for(var i=0; i<node.ops.length; i++) {
printNodes(node.ops[i], indent+2);
if(nodes) {
for(var k in nodes) {
printNodes(nodes[k], indent + 2);
}
}
}
else if(node instanceof CompareOperand) {
console.log("(" + node.type + ")");
printNodes(node.expr, indent+2);
}
else {
var children = node.children;
delete node.children;
print(util.inspect(node, true, null) + "\n", true);
node.children = children;
node.iterChildren(function(node) {
printNodes(node, indent+2);
});
}
}
// var t = new NodeList(0, 0,
// [new Value(0, 0, 3),
// new Value(0, 0, 10),
// new Pair(0, 0,
// new Value(0, 0, 'key'),
// new Value(0, 0, 'value'))]);
// printNodes(t);
module.exports = {

@@ -307,3 +208,2 @@ Node: Node,

NodeList: NodeList,
Expr: Expr,
Value: Value,

@@ -320,4 +220,8 @@ Literal: Literal,

For: For,
Macro: Macro,
Import: Import,
FromImport: FromImport,
FunCall: FunCall,
Filter: Filter,
KeywordArgs: KeywordArgs,
Block: Block,

@@ -324,0 +228,0 @@ Extends: Extends,

// Does not support:
//
// Conditional expression: "yes" if True else "no"
var lexer = require('./lexer');

@@ -107,6 +103,13 @@ var nodes = require('./nodes');

if(!name) {
if(this.peekToken().type != lexer.TOKEN_SYMBOL) {
var tok = this.peekToken();
if(!tok) {
this.fail('unexpected end of file');
}
if(tok.type != lexer.TOKEN_SYMBOL) {
this.fail("advanceAfterBlockEnd: expected symbol token or " +
"explicit name to be passed");
}
name = this.nextToken().value;

@@ -136,3 +139,3 @@ }

if(!node.name instanceof nodes.Symbol) {
if(!(node.name instanceof nodes.Symbol)) {
this.fail('variable name expected');

@@ -162,2 +165,100 @@ }

parseMacro: function() {
var macroTok = this.peekToken();
if(!this.skipSymbol('macro')) {
this.fail("expected macro");
}
var name = this.parsePrimary(true);
var args = this.parseSignature();
var node = new nodes.Macro(macroTok.lineno,
macroTok.colno,
name,
args);
this.advanceAfterBlockEnd(macroTok.value);
node.body = this.parseUntilBlocks('endmacro');
this.advanceAfterBlockEnd();
return node;
},
parseImport: function() {
var importTok = this.peekToken();
if(!this.skipSymbol('import')) {
this.fail("expected import");
}
var template = this.parsePrimary();
if(!this.skipSymbol('as')) {
throw new Error('expected "as" keyword');
}
var target = this.parsePrimary();
var node = new nodes.Import(importTok.lineno,
importTok.colno,
template,
target);
this.advanceAfterBlockEnd(importTok.value);
return node;
},
parseFrom: function() {
var fromTok = this.peekToken();
if(!this.skipSymbol('from')) {
this.fail("expected from");
}
var template = this.parsePrimary();
var node = new nodes.FromImport(fromTok.lineno,
fromTok.colno,
template,
new nodes.NodeList());
if(!this.skipSymbol('import')) {
throw new Error("expected import");
}
var names = node.names;
while(1) {
var type = this.peekToken().type;
if(type == lexer.TOKEN_BLOCK_END) {
if(!names.children.length) {
this.fail('Expected at least one import name');
}
this.nextToken();
break;
}
if(names.children.length > 0 && !this.skip(lexer.TOKEN_COMMA)) {
throw new Error('expected comma');
}
var name = this.parsePrimary();
if(name.value.charAt(0) == '_') {
this.fail('names starting with an underscore cannot be ' +
'imported',
name.lineno,
name.colno);
}
if(this.skipSymbol('as')) {
var alias = this.parsePrimary();
names.addChild(new nodes.Pair(name.lineno,
name.colno,
name,
alias));
}
else {
names.addChild(name);
}
}
return node;
},
parseBlock: function() {

@@ -172,3 +273,3 @@ var tag = this.peekToken();

node.name = this.parsePrimary();
if(!node.name instanceof nodes.Symbol) {
if(!(node.name instanceof nodes.Symbol)) {
this.fail('variable name expected');

@@ -200,4 +301,5 @@ }

if(!(node.template instanceof nodes.Literal &&
lib.isString(node.template.value))) {
this.fail('parseExtends: string expected');
lib.isString(node.template.value)) &&
!(node.template instanceof nodes.Symbol)) {
this.fail('parseExtends: string or value expected');
}

@@ -257,4 +359,3 @@

var node = new nodes.Set(tag.lineno, tag.colno);
node.targets = [];
var node = new nodes.Set(tag.lineno, tag.colno, []);

@@ -294,10 +395,13 @@ var target;

switch(tok.value) {
case 'raw': node = this.parseRaw(); break;
case 'if': node = this.parseIf(); break;
case 'for': node = this.parseFor(); break;
case 'block': node = this.parseBlock(); break;
case 'extends': node = this.parseExtends(); break;
case 'include': node = this.parseInclude(); break;
case 'set': node = this.parseSet(); break;
default: this.fail('unknown block tag: ' + tok.value);
case 'raw': node = this.parseRaw(); break;
case 'if': node = this.parseIf(); break;
case 'for': node = this.parseFor(); break;
case 'block': node = this.parseBlock(); break;
case 'extends': node = this.parseExtends(); break;
case 'include': node = this.parseInclude(); break;
case 'set': node = this.parseSet(); break;
case 'macro': node = this.parseMacro(); break;
case 'import': node = this.parseImport(); break;
case 'from': node = this.parseFrom(); break;
default: this.fail('unknown block tag: ' + tok.value);
}

@@ -361,7 +465,6 @@

// Function call
var list = this.parseAggregate();
node = new nodes.FunCall(tok.lineno,
tok.colno,
node,
list.children);
node = new nodes.FunCall(tok.lineno,
tok.colno,
node,
this.parseSignature());
}

@@ -607,3 +710,3 @@ else if(tok.type == lexer.TOKEN_LEFT_BRACKET) {

parsePrimary: function () {
parsePrimary: function (noPostfix) {
var tok = this.nextToken();

@@ -644,3 +747,6 @@ var val = null;

node = new nodes.Symbol(tok.lineno, tok.colno, tok.value);
node = this.parsePostfix(node);
if(!noPostfix) {
node = this.parsePostfix(node);
}
}

@@ -673,8 +779,13 @@ else {

node = new nodes.Filter(tok.lineno,
tok.colno,
new nodes.Symbol(tok.lineno,
tok.colno,
name),
[node]);
node = new nodes.Filter(
tok.lineno,
tok.colno,
new nodes.Symbol(tok.lineno,
tok.colno,
name),
new nodes.NodeList(
tok.lineno,
tok.colno,
[node])
);

@@ -685,3 +796,3 @@ if(this.peekToken().type == lexer.TOKEN_LEFT_PAREN) {

var call = this.parsePostfix(node);
node.children = node.children.concat(call.children);
node.args.children = node.args.children.concat(call.args.children);
}

@@ -717,3 +828,3 @@ }

if(node.numChildren() > 0) {
if(node.children.length > 0) {
if(!this.skip(lexer.TOKEN_COMMA)) {

@@ -751,2 +862,45 @@ throw new Error("parseAggregate: expected comma after expression");

parseSignature: function() {
var tok = this.nextToken();
var args = new nodes.NodeList(tok.lineno, tok.colno);
var kwargs = new nodes.KeywordArgs(tok.lineno, tok.colno);
var kwnames = [];
var checkComma = false;
while(1) {
var type = this.peekToken().type;
if(type == lexer.TOKEN_RIGHT_PAREN) {
this.nextToken();
break;
}
if(checkComma && !this.skip(lexer.TOKEN_COMMA)) {
throw new Error("parseSignature: expected comma after expression");
}
else {
var arg = this.parsePrimary();
if(this.skipValue(lexer.TOKEN_OPERATOR, '=')) {
kwargs.addChild(
new nodes.Pair(arg.lineno,
arg.colno,
arg,
this.parseExpression())
);
}
else {
args.addChild(arg);
}
}
checkComma = true;
}
if(kwargs.children.length) {
args.addChild(kwargs);
}
return args;
},
parseUntilBlocks: function(/* blockNames */) {

@@ -813,3 +967,3 @@ var prev = this.breakOnBlocks;

// var p = new Parser(lexer.lex('{% set x, y = 3 %}'));
// var p = new Parser(lexer.lex('{{ foo(1, 2, 3, foo=3) }}'));
// var n = p.parse();

@@ -816,0 +970,0 @@ // nodes.printNodes(n);

@@ -45,4 +45,74 @@

module.exports = {
Frame: Frame
function makeMacro(argNames, kwargNames, func) {
return function() {
var argCount = numArgs(arguments);
var args;
var kwargs = getKeywordArgs(arguments);
if(argCount > argNames.length) {
args = Array.prototype.slice.call(arguments, 0, argNames.length);
// Positional arguments that should be passed in as
// keyword arguments (essentially default values)
var vals = Array.prototype.slice.call(arguments, args.length, argCount);
for(var i=0; i<vals.length; i++) {
if(i < kwargNames.length) {
kwargs[kwargNames[i]] = vals[i];
}
}
args.push(kwargs);
}
else if(argCount < argNames.length) {
args = Array.prototype.slice.call(arguments, 0, argCount);
for(var i=argCount; i<argNames.length; i++) {
var arg = argNames[i];
// Keyword arguments that should be passed as
// positional arguments, i.e. the caller explicitly
// used the name of a positional arg
args.push(kwargs[arg]);
delete kwargs[arg];
}
args.push(kwargs);
}
else {
args = arguments;
}
return func.apply(this, args);
};
}
function makeKeywordArgs(obj) {
obj.__keywords = true;
return obj;
}
function getKeywordArgs(args) {
if(args.length && args[args.length - 1].__keywords) {
return args[args.length - 1];
}
return {};
}
function numArgs(args) {
if(args.length === 0) {
return 0;
}
else if(args[args.length - 1].__keywords) {
return args.length - 1;
}
else {
return args.length;
}
}
module.exports = {
Frame: Frame,
makeMacro: makeMacro,
makeKeywordArgs: makeKeywordArgs,
numArgs: numArgs
};

@@ -1,1 +0,1 @@

(function(){var a={};(function(){function b(a,c,d){var e=Object.create(a.prototype),f=/xyz/.test(function(){xyz})?/\bparent\b/:/.*/;d=d||{};for(var g in d){var h=d[g],i=e[g];typeof i=="function"&&typeof h=="function"&&f.test(h)?e[g]=function(a,b){return function(){var c=this.parent;this.parent=b;var d=a.apply(this,arguments);return this.parent=c,d}}(h,i):e[g]=h}e.typename=c;var j=function(){e.init&&e.init.apply(this,arguments)};return j.prototype=e,j.prototype.constructor=j,j.extend=function(a,c){return typeof a=="object"&&(c=a,a="anonymous"),b(j,a,c)},j}a.object=b(Object,"Object",{})})(),function(){var b=Array.prototype,c=Object.prototype,d=a.lib={};d.isFunction=function(a){return c.toString.call(a)=="[object Function]"},d.isArray=Array.isArray||function(a){return c.toString.call(a)=="[object Array]"},d.isString=function(a){return c.toString.call(a)=="[object String]"},d.isObject=function(a){return a===Object(a)},d.groupBy=function(a,b){var c={},e=d.isFunction(b)?b:function(a){return a[b]};for(var f=0;f<a.length;f++){var g=a[f],h=e(g,f);(c[h]||(c[h]=[])).push(g)}return c},d.toArray=function(a){return Array.prototype.slice.call(a)},d.without=function(a){var b=[];if(!a)return b;var c=-1,e=a.length,f=d.toArray(arguments).slice(1);while(++c<e)f.indexOf(a[c])===-1&&b.push(a[c]);return b},d.extend=function(a,b){for(var c in b)a[c]=b[c];return a},d.repeat=function(a,b){var c="";for(var d=0;d<b;d++)c+=a;return c},d.map=function(a,c){var d=[];if(a==null)return d;if(b.map&&a.map===b.map)return a.map(c);for(var e=0;e<a.length;e++)d[d.length]=c(a[e],e);return a.length===+a.length&&(d.length=a.length),d}}(),function(){var b=a.lib,c={abs:function(a){return Math.abs(a)},batch:function(a,b,c){var d=[],e=[];for(var f=0;f<a.length;f++)f%b===0&&e.length&&(d.push(e),e=[]),e.push(a[f]);if(e.length){if(c)for(var f=e.length;f<b;f++)e.push(c);d.push(e)}return d},capitalize:function(a){return a=a.toLowerCase(),a[0].toUpperCase()+a.slice(1)},center:function(a,c){c=c||80;if(a.length>=c)return a;var d=c-a.length,e=b.repeat(" ",d/2-d%2),f=b.repeat(" ",d/2);return e+a+f},"default":function(a,b){return a?a:b},escape:function(a){return a.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/'/g,"&#39;").replace(/</g,"&lt;").replace(/>/g,"&gt;")},first:function(a){return a[0]},groupby:function(a,c){return b.groupBy(a,c)},indent:function(a,c,d){c=c||4;var e="",f=a.split("\n"),g=b.repeat(" ",c);for(var h=0;h<f.length;h++)h==0&&!d?e+=f[h]+"\n":e+=g+f[h]+"\n";return e},join:function(a,c,d){return c=c||"",d&&(a=b.map(a,function(a){return a[d]})),a.join(c)},last:function(a){return a[a.length-1]},length:function(a){return a.length},list:function(a){if(b.isString(a))return a.split("");if(b.isObject(a)){var c=[];if(Object.keys)c=Object.keys(a);else for(var d in a)c.push(d);return b.map(c,function(b){return{key:b,value:a[b]}})}throw new Error("list: type not iterable")},lower:function(a){return a.toLowerCase()},random:function(a){var b=Math.floor(Math.random()*a.length);return b==a.length&&b--,a[b]},replace:function(a,b,c,d){var e=a,f=e,g=1;e=e.replace(b,c);while(f!=e){if(g>=d)break;f=e,e=e.replace(b,c),g++}return e},reverse:function(a){var d;return b.isString(a)?d=c.list(a):d=b.map(a,function(a){return a}),d.reverse(),b.isString(a)?d.join(""):d},round:function(a,b,c){b=b||0;var d=Math.pow(10,b),e;return c=="ceil"?e=Math.ceil:c=="floor"?e=Math.floor:e=Math.round,e(a*d)/d},slice:function(a,b,c){var d=Math.floor(a.length/b),e=a.length%b,f=0,g=[];for(var h=0;h<b;h++){var i=f+h*d;h<e&&f++;var j=f+(h+1)*d,k=a.slice(i,j);c&&h>=e&&k.push(c),g.push(k)}return g},sort:function(a,c,d,e){return a=b.map(a,function(a){return a}),a.sort(function(a,f){var g,h;return e?(g=a[e],h=f[e]):(g=a,h=f),!d&&b.isString(g)&&b.isString(h)&&(g=g.toLowerCase(),h=h.toLowerCase()),g<h?c?1:-1:g>h?c?-1:1:0}),a},string:function(a){return a.toString()},title:function(a){return a.toUpperCase()},trim:function(a){return a.replace(/^\s*|\s*$/g,"")},upper:function(a){return a.toUpperCase()},wordcount:function(a){return a.match(/\w+/g).length},"float":function(a,b){return parseFloat(a)||b},"int":function(a,b){return parseInt(a)||b}};c.d=c.default,c.e=c.escape,a.filters=c}(),function(){var b=a.object,c=b.extend({init:function(a){this.variables={},this.parent=a},set:function(a,b){var c=a.split("."),d=this.variables;for(var e=0;e<c.length-1;e++){var f=c[e];d[f]||(d[f]={}),d=d[f]}d[c[c.length-1]]=b},lookup:function(a){var b=this.parent;return this.variables[a]||b&&b.lookup(a)},push:function(){return new c(this)},pop:function(){return this.parent}});a.runtime={Frame:c}}(),function(){var b=a.lib,c=a.object,d=a.lexer,e=a.compiler,f=a.filters,g=a.loaders,h=a.runtime.Frame,i=c.extend({init:function(a,c){a?this.loaders=b.isArray(a)?a:[a]:g.FileSystemLoader?this.loaders=[new g.FileSystemLoader]:this.loaders=[new g.HttpLoader("/views")],c&&d.setTags(c),this.filters=f,this.cache={}},addFilter:function(a,b){this.filters[a]=b},getFilter:function(a){return this.filters[a]},getTemplate:function(a,b){var c=null,d=this.cache[a],e;if(!d||!d.isUpToDate()){for(var f=0;f<this.loaders.length;f++)if(c=this.loaders[f].getSource(a))break;if(!c)throw new Error("template not found: "+a);this.cache[a]=new k(c.src,this,c.path,c.upToDate,b)}return this.cache[a]},registerPrecompiled:function(a){for(var b in a)this.cache[b]=new k({type:"code",obj:a[b]},this,b,function(){return!0},!0)},express:function(a){var c=this;a.render=function(d,e,f){var g={};b.isFunction(e)&&(f=e,e={}),g=b.extend(g,a.locals),e._locals&&(g=b.extend(g,e._locals)),g=b.extend(g,e);var h=c.render(d,e);f(null,h)}},render:function(a,b){return this.getTemplate(a).render(b)}}),j=c.extend({init:function(a,b){this.ctx=a,this.blocks={};for(var c in b)this.addBlock(c,b[c])},lookup:function(a){return this.ctx[a]},setVariable:function(a,b){this.ctx[a]=b},getVariables:function(){return this.ctx},addBlock:function(a,b){this.blocks[a]=this.blocks[a]||[],this.blocks[a].push(b)},getBlock:function(a){if(!this.blocks[a])throw new Error('unknown block "'+a+'"');return this.blocks[a][0]},getSuper:function(a,b,c){var d=(this.blocks[b]||[]).indexOf(c),e=this.blocks[b][d+1],f=this;return function(){if(d==-1||!e)throw new Error('no super block available for "'+b+'"');return e(a,f)}}}),k=c.extend({init:function(a,c,d,e,f){this.env=c||new i;if(b.isObject(a))switch(a.type){case"code":this.tmplProps=a.obj;break;case"string":this.tmplStr=a.obj}else{if(!b.isString(a))throw new Error("src must be a string or an object describing the source");this.tmplStr=a}this.path=d,this.upToDate=e||function(){return!1},f?this._compile():this.compiled=!1},render:function(a,b){this.compiled||this._compile();var c=new j(a||{},this.blocks);return this.rootRenderFunc(this.env,c,b||new h)},isUpToDate:function(){return this.upToDate()},_compile:function(){var a;if(this.tmplProps)a=this.tmplProps;else{var b=new Function(e.compile(this.tmplStr,this.env));a=b()}this.blocks=this._getBlocks(a),this.rootRenderFunc=a.root,this.compiled=!0},_getBlocks:function(a){var b={};for(var c in a)c.slice(0,2)=="b_"&&(b[c.slice(2)]=a[c]);return b}});a.environment={Environment:i,Template:k}}();var b=a.environment,c=a.compiler,d=a.parser,e=a.lexer,f=a.loaders;window.nunjucks={},window.nunjucks.Environment=b.Environment,window.nunjucks.Template=b.Template,f.FileSystemLoader?window.nunjucks.FileSystemLoader=f.FileSystemLoader:window.nunjucks.HttpLoader=f.HttpLoader,window.nunjucks.compiler=c,window.nunjucks.parser=d,window.nunjucks.lexer=e,window.nunjucks.require=function(b){return a[b]}})();
(function(){var a={};(function(){function b(a,c,d){var e=Object.create(a.prototype),f=/xyz/.test(function(){xyz})?/\bparent\b/:/.*/;d=d||{};for(var g in d){var h=d[g],i=e[g];typeof i=="function"&&typeof h=="function"&&f.test(h)?e[g]=function(a,b){return function(){var c=this.parent;this.parent=b;var d=a.apply(this,arguments);return this.parent=c,d}}(h,i):e[g]=h}e.typename=c;var j=function(){e.init&&e.init.apply(this,arguments)};return j.prototype=e,j.prototype.constructor=j,j.extend=function(a,c){return typeof a=="object"&&(c=a,a="anonymous"),b(j,a,c)},j}a.object=b(Object,"Object",{})})(),function(){var b=Array.prototype,c=Object.prototype,d=a.lib={};d.isFunction=function(a){return c.toString.call(a)=="[object Function]"},d.isArray=Array.isArray||function(a){return c.toString.call(a)=="[object Array]"},d.isString=function(a){return c.toString.call(a)=="[object String]"},d.isObject=function(a){return a===Object(a)},d.groupBy=function(a,b){var c={},e=d.isFunction(b)?b:function(a){return a[b]};for(var f=0;f<a.length;f++){var g=a[f],h=e(g,f);(c[h]||(c[h]=[])).push(g)}return c},d.toArray=function(a){return Array.prototype.slice.call(a)},d.without=function(a){var b=[];if(!a)return b;var c=-1,e=a.length,f=d.toArray(arguments).slice(1);while(++c<e)f.indexOf(a[c])===-1&&b.push(a[c]);return b},d.extend=function(a,b){for(var c in b)a[c]=b[c];return a},d.repeat=function(a,b){var c="";for(var d=0;d<b;d++)c+=a;return c},d.each=function(a,c,d){if(a==null)return;if(b.each&&a.each==b.each)a.forEach(c,d);else if(a.length===+a.length)for(var e=0,f=a.length;e<f;e++)c.call(d,a[e],e,a)},d.map=function(a,c){var d=[];if(a==null)return d;if(b.map&&a.map===b.map)return a.map(c);for(var e=0;e<a.length;e++)d[d.length]=c(a[e],e);return a.length===+a.length&&(d.length=a.length),d}}(),function(){var b=a.lib,c={abs:function(a){return Math.abs(a)},batch:function(a,b,c){var d=[],e=[];for(var f=0;f<a.length;f++)f%b===0&&e.length&&(d.push(e),e=[]),e.push(a[f]);if(e.length){if(c)for(var f=e.length;f<b;f++)e.push(c);d.push(e)}return d},capitalize:function(a){return a=a.toLowerCase(),a[0].toUpperCase()+a.slice(1)},center:function(a,c){c=c||80;if(a.length>=c)return a;var d=c-a.length,e=b.repeat(" ",d/2-d%2),f=b.repeat(" ",d/2);return e+a+f},"default":function(a,b){return a?a:b},escape:function(a){return a.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/'/g,"&#39;").replace(/</g,"&lt;").replace(/>/g,"&gt;")},first:function(a){return a[0]},groupby:function(a,c){return b.groupBy(a,c)},indent:function(a,c,d){c=c||4;var e="",f=a.split("\n"),g=b.repeat(" ",c);for(var h=0;h<f.length;h++)h==0&&!d?e+=f[h]+"\n":e+=g+f[h]+"\n";return e},join:function(a,c,d){return c=c||"",d&&(a=b.map(a,function(a){return a[d]})),a.join(c)},last:function(a){return a[a.length-1]},length:function(a){return a.length},list:function(a){if(b.isString(a))return a.split("");if(b.isObject(a)){var c=[];if(Object.keys)c=Object.keys(a);else for(var d in a)c.push(d);return b.map(c,function(b){return{key:b,value:a[b]}})}throw new Error("list: type not iterable")},lower:function(a){return a.toLowerCase()},random:function(a){var b=Math.floor(Math.random()*a.length);return b==a.length&&b--,a[b]},replace:function(a,b,c,d){var e=a,f=e,g=1;e=e.replace(b,c);while(f!=e){if(g>=d)break;f=e,e=e.replace(b,c),g++}return e},reverse:function(a){var d;return b.isString(a)?d=c.list(a):d=b.map(a,function(a){return a}),d.reverse(),b.isString(a)?d.join(""):d},round:function(a,b,c){b=b||0;var d=Math.pow(10,b),e;return c=="ceil"?e=Math.ceil:c=="floor"?e=Math.floor:e=Math.round,e(a*d)/d},slice:function(a,b,c){var d=Math.floor(a.length/b),e=a.length%b,f=0,g=[];for(var h=0;h<b;h++){var i=f+h*d;h<e&&f++;var j=f+(h+1)*d,k=a.slice(i,j);c&&h>=e&&k.push(c),g.push(k)}return g},sort:function(a,c,d,e){return a=b.map(a,function(a){return a}),a.sort(function(a,f){var g,h;return e?(g=a[e],h=f[e]):(g=a,h=f),!d&&b.isString(g)&&b.isString(h)&&(g=g.toLowerCase(),h=h.toLowerCase()),g<h?c?1:-1:g>h?c?-1:1:0}),a},string:function(a){return a.toString()},title:function(a){return a.toUpperCase()},trim:function(a){return a.replace(/^\s*|\s*$/g,"")},upper:function(a){return a.toUpperCase()},wordcount:function(a){return a.match(/\w+/g).length},"float":function(a,b){return parseFloat(a)||b},"int":function(a,b){return parseInt(a)||b}};c.d=c.default,c.e=c.escape,a.filters=c}(),function(){function d(a,b,c){return function(){var d=g(arguments),e,h=f(arguments);if(d>a.length){e=Array.prototype.slice.call(arguments,0,a.length);var i=Array.prototype.slice.call(arguments,e.length,d);for(var j=0;j<i.length;j++)j<b.length&&(h[b[j]]=i[j]);e.push(h)}else if(d<a.length){e=Array.prototype.slice.call(arguments,0,d);for(var j=d;j<a.length;j++){var k=a[j];e.push(h[k]),delete h[k]}e.push(h)}else e=arguments;return c.apply(this,e)}}function e(a){return a.__keywords=!0,a}function f(a){return a.length&&a[a.length-1].__keywords?a[a.length-1]:{}}function g(a){return a.length===0?0:a[a.length-1].__keywords?a.length-1:a.length}var b=a.object,c=b.extend({init:function(a){this.variables={},this.parent=a},set:function(a,b){var c=a.split("."),d=this.variables;for(var e=0;e<c.length-1;e++){var f=c[e];d[f]||(d[f]={}),d=d[f]}d[c[c.length-1]]=b},lookup:function(a){var b=this.parent;return this.variables[a]||b&&b.lookup(a)},push:function(){return new c(this)},pop:function(){return this.parent}});a.runtime={Frame:c,makeMacro:d,makeKeywordArgs:e,numArgs:g}}(),function(){var b=a.lib,c=a.object,d=a.lexer,e=a.compiler,f=a.filters,g=a.loaders,h=a.runtime,i=h.Frame,j=c.extend({init:function(a,c){a?this.loaders=b.isArray(a)?a:[a]:g.FileSystemLoader?this.loaders=[new g.FileSystemLoader]:this.loaders=[new g.HttpLoader("/views")],c&&d.setTags(c),this.filters=f,this.cache={}},addFilter:function(a,b){this.filters[a]=b},getFilter:function(a){return this.filters[a]},getTemplate:function(a,b){var c=null,d=this.cache[a],e;if(!d||!d.isUpToDate()){for(var f=0;f<this.loaders.length;f++)if(c=this.loaders[f].getSource(a))break;if(!c)throw new Error("template not found: "+a);this.cache[a]=new l(c.src,this,c.path,c.upToDate,b)}return this.cache[a]},registerPrecompiled:function(a){for(var b in a)this.cache[b]=new l({type:"code",obj:a[b]},this,b,function(){return!0},!0)},express:function(a){var c=this;a.render=function(d,e,f){var g={};b.isFunction(e)&&(f=e,e={}),g=b.extend(g,a.locals),e._locals&&(g=b.extend(g,e._locals)),g=b.extend(g,e);var h=c.render(d,g);f(null,h)}},render:function(a,b){return this.getTemplate(a).render(b)}}),k=c.extend({init:function(a,b){this.ctx=a,this.blocks={},this.exported=[];for(var c in b)this.addBlock(c,b[c])},lookup:function(a){return this.ctx[a]},setVariable:function(a,b){this.ctx[a]=b},getVariables:function(){return this.ctx},addBlock:function(a,b){this.blocks[a]=this.blocks[a]||[],this.blocks[a].push(b)},getBlock:function(a){if(!this.blocks[a])throw new Error('unknown block "'+a+'"');return this.blocks[a][0]},getSuper:function(a,b,c){var d=(this.blocks[b]||[]).indexOf(c),e=this.blocks[b][d+1],f=this;return function(){if(d==-1||!e)throw new Error('no super block available for "'+b+'"');return e(a,f)}},addExport:function(a){this.exported.push(a)},getExported:function(){var a={};for(var b=0;b<this.exported.length;b++){var c=this.exported[b];a[c]=this.ctx[c]}return a}}),l=c.extend({init:function(a,c,d,e,f){this.env=c||new j;if(b.isObject(a))switch(a.type){case"code":this.tmplProps=a.obj;break;case"string":this.tmplStr=a.obj}else{if(!b.isString(a))throw new Error("src must be a string or an object describing the source");this.tmplStr=a}this.path=d,this.upToDate=e||function(){return!1},f?this._compile():this.compiled=!1},render:function(a,b){this.compiled||this._compile();var c=new k(a||{},this.blocks);return this.rootRenderFunc(this.env,c,b||new i,h)},isUpToDate:function(){return this.upToDate()},getExported:function(){this.compiled||this._compile();var a=new k({},this.blocks);return this.rootRenderFunc(this.env,a,new i,h),a.getExported()},_compile:function(){var a;if(this.tmplProps)a=this.tmplProps;else{var b=new Function(e.compile(this.tmplStr,this.env));a=b()}this.blocks=this._getBlocks(a),this.rootRenderFunc=a.root,this.compiled=!0},_getBlocks:function(a){var b={};for(var c in a)c.slice(0,2)=="b_"&&(b[c.slice(2)]=a[c]);return b}});a.environment={Environment:j,Template:l}}();var b=a.environment,c=a.compiler,d=a.parser,e=a.lexer,f=a.loaders;window.nunjucks={},window.nunjucks.Environment=b.Environment,window.nunjucks.Template=b.Template,f.FileSystemLoader?window.nunjucks.FileSystemLoader=f.FileSystemLoader:window.nunjucks.HttpLoader=f.HttpLoader,window.nunjucks.compiler=c,window.nunjucks.parser=d,window.nunjucks.lexer=e,window.nunjucks.require=function(b){return a[b]}})();

@@ -131,2 +131,17 @@ (function() {

exports.each = function(obj, func, context) {
if(obj == null) {
return;
}
if(ArrayProto.each && obj.each == ArrayProto.each) {
obj.forEach(func, context);
}
else if(obj.length === +obj.length) {
for(var i=0, l=obj.length; i<l; i++) {
func.call(context, obj[i], i, obj);
}
}
};
exports.map = function(obj, func) {

@@ -496,8 +511,77 @@ var results = [];

modules['runtime'] = {
Frame: Frame
function makeMacro(argNames, kwargNames, func) {
return function() {
var argCount = numArgs(arguments);
var args;
var kwargs = getKeywordArgs(arguments);
if(argCount > argNames.length) {
args = Array.prototype.slice.call(arguments, 0, argNames.length);
// Positional arguments that should be passed in as
// keyword arguments (essentially default values)
var vals = Array.prototype.slice.call(arguments, args.length, argCount);
for(var i=0; i<vals.length; i++) {
if(i < kwargNames.length) {
kwargs[kwargNames[i]] = vals[i];
}
}
args.push(kwargs);
}
else if(argCount < argNames.length) {
args = Array.prototype.slice.call(arguments, 0, argCount);
for(var i=argCount; i<argNames.length; i++) {
var arg = argNames[i];
// Keyword arguments that should be passed as
// positional arguments, i.e. the caller explicitly
// used the name of a positional arg
args.push(kwargs[arg]);
delete kwargs[arg];
}
args.push(kwargs);
}
else {
args = arguments;
}
return func.apply(this, args);
};
}
function makeKeywordArgs(obj) {
obj.__keywords = true;
return obj;
}
function getKeywordArgs(args) {
if(args.length && args[args.length - 1].__keywords) {
return args[args.length - 1];
}
return {};
}
function numArgs(args) {
if(args.length === 0) {
return 0;
}
else if(args[args.length - 1].__keywords) {
return args.length - 1;
}
else {
return args.length;
}
}
modules['runtime'] = {
Frame: Frame,
makeMacro: makeMacro,
makeKeywordArgs: makeKeywordArgs,
numArgs: numArgs
};
})();
(function() {
var lib = modules["lib"];

@@ -509,3 +593,4 @@ var Object = modules["object"];

var builtin_loaders = modules["loaders"];
var Frame = modules["runtime"].Frame;
var runtime = modules["runtime"];
var Frame = runtime.Frame;

@@ -599,4 +684,4 @@ var Environment = Object.extend({

var res = env.render(name, ctx);
k(null, res);
var res = env.render(name, context);
k(null, res);
};

@@ -614,2 +699,3 @@ },

this.blocks = {};
this.exported = [];

@@ -628,3 +714,3 @@ for(var name in blocks) {

},
getVariables: function() {

@@ -659,2 +745,15 @@ return this.ctx;

};
},
addExport: function(name) {
this.exported.push(name);
},
getExported: function() {
var exported = {};
for(var i=0; i<this.exported.length; i++) {
var name = this.exported[i];
exported[name] = this.ctx[name];
}
return exported;
}

@@ -700,3 +799,4 @@ });

context,
frame || new Frame());
frame || new Frame(),
runtime);
},

@@ -708,2 +808,16 @@

getExported: function() {
if(!this.compiled) {
this._compile();
}
// Run the rootRenderFunc to populate the context with exported vars
var context = new Context({}, this.blocks);
this.rootRenderFunc(this.env,
context,
new Frame(),
runtime);
return context.getExported();
},
_compile: function() {

@@ -719,3 +833,3 @@ var props;

}
this.blocks = this._getBlocks(props);

@@ -740,4 +854,5 @@ this.rootRenderFunc = props.root;

// var fs = modules["fs"];
// //var src = fs.readFileSync('test.html', 'utf-8');
// var src = "{% for i in [1,2,3] %}{% include 'item.html' %}{% endfor %}";
// var src = fs.readFileSync('test.html', 'utf-8');
// //var src = '{% macro foo(x, y, z=3) %}h{% endmacro %}';
// //var src = '{% macro foo() %}{{ h }}{% endmacro %} {{ foo() }}';

@@ -744,0 +859,0 @@ // var env = new Environment();

@@ -6,3 +6,3 @@

function render(str, ctx) {
var e = new env.Environment(new loaders.FileSystemLoader('tests'));
var e = new env.Environment(new loaders.FileSystemLoader('tests/templates'));
ctx = ctx || {};

@@ -9,0 +9,0 @@ var t = new env.Template(str, e);

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc