-
Notifications
You must be signed in to change notification settings - Fork 0
/
jinja.min.js
2 lines (2 loc) · 10.9 KB
/
jinja.min.js
1
2
!function(){function t(){this.nest=[],this.compiled=[],this.childBlocks=0,this.parentBlocks=0,this.isSilent=!1}function e(t){return t.replace(f,"")}function n(t){return t.replace(m,"")}function i(t,e,n){return new Promise(function(i){function r(){a?n(a[0],a.index,t).then(function(n){"number"==typeof n&&(e.lastIndex=n),a=e.exec(t),r()}):i()}e=new RegExp(e.source,"g"+(e.ignoreCase?"i":"")+(e.multiline?"m":""));var a=e.exec(t);r()})}var r=/'(\\.|[^'])*'|"(\\.|[^"'"])*"/g,a=/([$_a-z][$\w]*)|([+-]?\d+(\.\d+)?)/g,s=/^[+-]?\d+(\.\d+)?$/,o=/\[[@#~](,[@#~])*\]|\[\]|\{([@i]:[@#~])(,[@i]:[@#~])*\}|\{\}/g,c=/[$_a-z][$\w]*/gi,l=/i(\.i|\[[@#i]\])*/g,u=/(\.i|\[[@#i]\])/g,p=/(===?|!==?|>=?|<=?|&&|\|\||[+\-\*\/%])/g,h=/(^|[^$\w])(and|or|not|is|isnot)([^$\w]|$)/g,f=/^\s+/,m=/\s+$/,j=/\{\{\{|\{\{|\{%|\{#/,g={"{{{":/^('(\\.|[^'])*'|"(\\.|[^"'"])*"|.)+?\}\}\}/,"{{":/^('(\\.|[^'])*'|"(\\.|[^"'"])*"|.)+?\}\}/,"{%":/^('(\\.|[^'])*'|"(\\.|[^"'"])*"|.)+?%\}/,"{#":/^('(\\.|[^'])*'|"(\\.|[^"'"])*"|.)+?#\}/},d={"{%":"directive","{{":"output","{#":"comment"},v={and:"&&",or:"||",not:"!",is:"==",isnot:"!="},k={"true":!0,"false":!1,"null":null};t.prototype.push=function(t){this.isSilent||this.compiled.push(t)},t.prototype.parse=function(t){var e=this;return e.tokenize(t).then(function(){return Promise.resolve(e.compiled)})},t.prototype.tokenize=function(t){var a=this;return new Promise(function(s){var o=0,c=a,l=!1;i(t,j,function(t,i,a){var s=a.slice(i+t.length).match(g[t]);s=s?s[0]:"";var u=s.replace(r,"@");if(!s||~u.indexOf(t))return Promise.resolve(i+1);var p=s.slice(0,0-t.length);if("-"==p.charAt(0))var h=!0;if("-"==p.slice(-1))var f=!0;if(p=p.replace(/^-|-$/g,"").trim(),c.rawMode&&t+p!="{%endraw")return Promise.resolve(i+1);c.commentMode;var m=a.slice(o,i);if(o=i+t.length+s.length,l&&(m=e(m)),h&&(m=n(m)),f&&(l=!0),"{{{"==t)t="{{",p+="|safe";else if("{#"==t)return Promise.resolve(o);return c.textHandler(m),c.tokenHandler(t,p)}).then(function(){var n=t.slice(o);l&&(n=e(n)),a.textHandler(n),s()})})},t.prototype.textHandler=function(t){t&&this.push("write("+JSON.stringify(t)+");")},t.prototype.tokenHandler=function(t,e){var n=d[t];if("directive"==n)return this.compileTag(e);if("output"==n){var i=this.extractEnt(e,r,"@");i.src=i.src.replace(/\|\|/g,"~").split("|"),i.src=i.src.map(function(t){return t.split("~").join("||")});var a=this.injectEnt(i,"@");if(a.length>1){var s=a.slice(1).map(this.parseFilter.bind(this));this.push("filter("+this.parseExpr(a[0])+","+s.join(",")+");")}else this.push("filter("+this.parseExpr(a[0])+");");return Promise.resolve()}return Promise.resolve()},t.prototype.compileTag=function(t){var e=t.split(" ")[0],n=jinja.tag_handlers[e];if(!n)throw new Error("Invalid tag: "+t);var i=n.call(this,t.slice(e.length).trim());return void 0==i?Promise.resolve():i},t.prototype.parseFilter=function(t){t=t.trim();var e=t.match(/[:(]/),n=e?e.index:-1;if(0>n)return JSON.stringify([t]);var i=t.slice(0,n),r=":"==t.charAt(n)?t.slice(n+1):t.slice(n+1,-1);return r=this.parseExpr(r,{terms:!0}),"["+JSON.stringify(i)+","+r+"]"},t.prototype.extractEnt=function(t,e,n){var i=[],r="function"==typeof n;return t=t.replace(e,function(t){var e=r?n(t):n;return e?(i.push(t),e):t}),{src:t,subs:i}},t.prototype.injectEnt=function(t,e){var n=t.src,i=t.subs,r=Array.isArray(n),a=r?n:[n],s=new RegExp("["+e+"]","g"),o=0;return a.forEach(function(t,e){a[e]=t.replace(s,function(){return i[o++]})}),r?a:a[0]},t.prototype.replaceComplex=function(t){var e=this.extractEnt(t,/i(\.i|\[[@#i]\])+/g,"v");return e.src=e.src.replace(o,"~"),this.injectEnt(e,"v")},t.prototype.parseExpr=function(t,e){e=e||{};var n=this.extractEnt(t,r,"@");n.src=n.src.replace(h,function(t,e,n,i){return n in v?e+v[n]+i:t});var i=this.extractEnt(n.src,a,function(t){return t in k||s.test(t)?"#":null}),o=this.extractEnt(i.src,c,"i");o.src=o.src.replace(/\s+/g,"");for(var u=o.src;u!=(u=this.replaceComplex(u)););for(;u!=(u=u.replace(/i(\.i|\[[@#i]\])+/,"v")););u=u.replace(/[iv]\[v?\]/g,"x"),u=u.replace(/[@#~v]/g,"i"),u=u.replace(p,"%"),u=u.replace(/!+[i]/g,"i");var f=e.terms?u.split(","):[u];return f.forEach(function(e){for(;e!=(e=e.replace(/\(i(%i)*\)/g,"i")););if(!e.match(/^i(%i)*$/))throw new Error("Invalid expression: "+t)}),o.src=o.src.replace(l,this.parseVar.bind(this)),i.src=this.injectEnt(o,"i"),n.src=this.injectEnt(i,"#"),this.injectEnt(n,"@")},t.prototype.parseVar=function(t){var e=Array.prototype.slice.call(arguments),n=e.pop(),i=e.pop();if("i"==t&&":"==n.charAt(i+1))return'"i"';var r=['"i"'];return t.replace(u,function(t){r.push(".i"==t?'"i"':"[i]"==t?'get("i")':t.slice(1,-1))}),"get("+r.join(",")+")"},t.prototype.escName=function(t){return t.replace(/\W/g,function(t){return"$"+t.charCodeAt(0).toString(16)})},t.prototype.parseQuoted=function(t){"'"==t.charAt(0)&&(t=t.slice(1,-1).replace(/\\.|"/,function(t){return"\\'"==t?"'":"\\"==t.charAt(0)?t:"\\"+t}),t='"'+t+'"');try{return JSON.parse(t)}catch(e){return console.error("could not parse string "+t+"! replacing by empty"),""}},window.jinja={},jinja.tag_handlers={},jinja.filter_handlers={};var y,_=Object.prototype.toString,w=Object.prototype.hasOwnProperty,x=function(t){return null==t?"":"function"==typeof t.toString?t.toString():_.call(t)},E=function(t,e){var n={autoEscape:"html"},i=function(t,e){return Object.keys(e).forEach(function(n){t[n]=e[n]}),t},r=function(){for(var t,e=arguments[0],n=f.length;n--&&(t=f[n][e],"undefined"==typeof t););for(var i=1,r=arguments.length;r>i;i++)null!=t&&(e=arguments[i],t=w.call(t,e)?t[e]:"function"==typeof t._get?t[e]=t._get(e):null);return null==t?null:t},a=function(t,e){f[f.length-1][t]=e},s=function(t){f.push(t||{})},o=function(){f.pop()},c=function(t){m.push(t)},l=function(n){for(var i=1,r=arguments.length;r>i;i++){var a=arguments[i],s=a[0],o=jinja.filter_handlers[s];if(!o)throw new Error("Invalid filter: "+s);a[0]=n,n=o.apply(t,a)}e.autoEscape&&s!=e.autoEscape&&"safe"!=s&&(n=jinja.filter_handlers[e.autoEscape].call(t,n)),m.push(n)},u=function(t,e,n,r){if(null!=t){var a=Array.isArray(t)?t:Object.keys(t),c=a.length,l={loop:{length:c,first:a[0],last:a[c-1]}};s(l);for(var u=0;c>u;u++)i(l.loop,{index:u+1,index0:u}),n(l[e]=a[u]);0==c&&r&&r(),o()}},p=function(t){s(),t(),o()},h=function(){return m.join("")};t=t||{},e=i(n,e||{});var f=[Object.create(t||{})],m=[];return{get:r,set:a,push:s,pop:o,write:c,filter:l,each:u,block:p,render:h}};jinja.make_tag=function(t,e){jinja.tag_handlers[t]="string"==typeof e?jinja.tag_handlers[e]:e},jinja.make_filter=function(t,e){jinja.filter_handlers.name&&console.warn("Filter "+t+" already exists. Overriding."),jinja.filter_handlers[t]=e},jinja.compile=function(e,n){var i=this;return new Promise(function(r){n=n||{};var a=new t;a.read_template_file=i.read_template_file;var s=[];s.push("function render($) {"),s.push("var get = $.get, set = $.set, push = $.push, pop = $.pop, write = $.write, filter = $.filter, each = $.each, block = $.block;"),a.parse(e).then(function(t){if(s.push.apply(s,t),s.push("return $.render();"),s.push("}"),s=s.join("\n"),n.runtime===!1)var e=new Function("data","options","return ("+s+")(runtime(data, options))");else y=y||(y=E.toString()),e=new Function("data","options","return ("+s+")(("+y+")(data, options))");r({render:e})})})},jinja.render=function(t,e,n){return new Promise(function(i){var r={},a=[];for(var s in e)e[s]instanceof Promise&&(r[s]=a.length,a.push(e[s]));Promise.all(a).then(function(a){for(var s in r)e[s]=a[r[s]];jinja.compile(t).then(function(t){i(t.render(e,n))})})["catch"](function(t){console.error(t)})})},jinja.template_files={},jinja.template_url="/templates/",jinja.read_template_file=function(t){if(!jinja.loader)throw new Error("template loader is not set!");return new Promise(function(e,n){var i=jinja.template_files||{},r=i[t];return null==r?jinja.loader(jinja.template_url+t).then(function(t){e(t)})["catch"](function(e){n('Template file "'+t+'" not found: '+e)}):void e(r)})},jinja.loader=function(t){return fetch(t).then(function(t){return 200===t.status||0===t.status?t.text().then(function(t){return console.log(t),jinja.template_files[name]=t,jinja.template_files[name]})["catch"](function(){"use strict";console.log(":(")}):Promise.reject(new Error(t.statusText))})},jinja.make_filter("html",function(t){return x(t).split("&").join("&").split("<").join("<").split(">").join(">").split('"').join(""")}),jinja.make_filter("safe",function(t){return t}),jinja.make_tag("comment",function(){this.commentMode=!0}),jinja.make_tag("endcomment",function(){this.commentMode=!1}),jinja.make_tag("block",function(t){if(this.isParent){++this.parentBlocks;var e="block_"+(this.escName(t)||this.parentBlocks);this.push("block(typeof "+e+' == "function" ? '+e+" : function() {")}else this.hasParent&&(this.isSilent=!1,++this.childBlocks,e="block_"+(this.escName(t)||this.childBlocks),this.push("function "+e+"() {"));this.nest.unshift("block")}),jinja.make_tag("endblock",function(){this.nest.shift(),this.isParent?this.push("});"):this.hasParent&&(this.push("}"),this.isSilent=!0)}),jinja.make_tag("extends",function(t){(t[0].startsWith("'")||t[0].startsWith('"'))&&(t=this.parseQuoted(t));var e=this;return this.read_template_file(t).then(function(t){return e.isParent=!0,e.tokenize(t).then(function(){e.isParent=!1,e.hasParent=!0,e.isSilent=!0})})["catch"](function(t){console.error(t)})}),jinja.make_tag("for",function(t){var e=t.indexOf(" in "),n=t.slice(0,e).trim(),i=t.slice(e+4).trim();this.push("each("+this.parseExpr(i)+","+JSON.stringify(n)+",function() {"),this.nest.unshift("for")}),jinja.make_tag("endfor",function(){this.nest.shift(),this.push("});")}),jinja.make_tag("if",function(t){this.push("if ("+this.parseExpr(t)+") {"),this.nest.unshift("if")}),jinja.make_tag("else",function(){this.push("for"==this.nest[0]?"}, function() {":"} else {")}),jinja.make_tag("elseif",function(t){this.push("} else if ("+this.parseExpr(t)+") {")}),jinja.make_tag("elif","elseif"),jinja.make_tag("endif",function(){this.nest.shift(),this.push("}")}),jinja.make_tag("include",function(t){(t[0].startsWith("'")||t[0].startsWith('"'))&&(t=this.parseQuoted(t));var e=this;return this.read_template_file(t).then(function(t){return e.isInclude=!0,e.tokenize(t).then(function(){e.isInclude=!1})})["catch"](function(t){console.error(t)})}),jinja.make_tag("load",function(){}),jinja.make_tag("raw",function(){this.rawMode=!0}),jinja.make_tag("endraw",function(){this.rawMode=!1}),jinja.make_tag("set",function(t){var e=t.indexOf("="),n=t.slice(0,e).trim(),i=t.slice(e+1).trim();this.push("set("+JSON.stringify(n)+","+this.parseExpr(i)+");")}),jinja.make_tag("assign","set"),jinja.make_tag("static",function(t){t=t.trim(),this.push('write("'+Fat.config.static_url+t.substr(1,t.length-2)+'")')}),jinja.make_tag("static",function(t){t=t.trim();var e=t.split(/\s/);if(e.length<1)return void console.warn("url tag should contain 1 or more parameters. ignoring. ");for(var n=e[0].substr(1,e[0].length-2),i={positional:[],keyword:{}},r=1;r<e.length;r++)if(-1!=e[r].indexOd("=")){var a=e[r].split("=");i.keyword[a[0].trim()]=a[1].trim()}else i.positional.push(e[r].trim());this.push('write("'+Fat.resolve_url(n,i)+'")')})}();
//# sourceMappingURL=jinja.min.js.map