You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Juying/drpy/jinja.min.js

1 line
8.4 KiB

!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).jinja={})}(this,function(n){"use strict";var u=/'(\\.|[^'])*'|"(\\.|[^"'"])*"/g,o=/([$_a-z][$\w]*)|([+-]?\d+(\.\d+)?)/g,c=/^[+-]?\d+(\.\d+)?$/,e=/\[[@#~](,[@#~])*\]|\[\]|\{([@i]:[@#~])(,[@i]:[@#~])*\}|\{\}/g,a=/[$_a-z][$\w]*/gi,p=/i(\.i|\[[@#i]\])*/g,r=/(\.i|\[[@#i]\])/g,l=/(===?|!==?|>=?|<=?|&&|\|\||[+\-\*\/%])/g,h=/(^|[^$\w])(and|or|not|is|isnot)([^$\w]|$)/g,i=/^\s+/,f=/\s+$/,s=/\{\{\{|\{\{|\{%|\{#/,d={"{{{":/^('(\\.|[^'])*'|"(\\.|[^"'"])*"|.)+?\}\}\}/,"{{":/^('(\\.|[^'])*'|"(\\.|[^"'"])*"|.)+?\}\}/,"{%":/^('(\\.|[^'])*'|"(\\.|[^"'"])*"|.)+?%\}/,"{#":/^('(\\.|[^'])*'|"(\\.|[^"'"])*"|.)+?#\}/},g={"{%":"directive","{{":"output","{#":"comment"},v={and:"&&",or:"||",not:"!",is:"==",isnot:"!="},m={true:!0,false:!1,null:null};function y(){this.nest=[],this.compiled=[],this.childBlocks=0,this.parentBlocks=0,this.isSilent=!1}y.prototype.push=function(t){this.isSilent||this.compiled.push(t)},y.prototype.parse=function(t){return this.tokenize(t),this.compiled},y.prototype.tokenize=function(t){var c=0,a=this,p=!1;!function(t,e,i){for(e=new RegExp(e.source,"g"+(e.ignoreCase?"i":"")+(e.multiline?"m":""));n=e.exec(t);){var n=i(n[0],n.index,t);"number"==typeof n&&(e.lastIndex=n)}}(t,s,function(t,e,i){var n=i.slice(e+t.length).match(d[t]),r=(n=n?n[0]:"").replace(u,"@");if(!n||~r.indexOf(t))return e+1;var s,o,r=n.slice(0,0-t.length);if("-"===r.charAt(0)&&(s=!0),"-"===r.slice(-1)&&(o=!0),r=r.replace(/^-|-$/g,"").trim(),a.rawMode&&t+r!=="{%endraw")return e+1;i=i.slice(c,e);c=e+t.length+n.length,p&&(i=j(i)),s&&(i=i.replace(f,"")),o&&(p=!0),"{{{"===t&&(t="{{",r+="|safe"),a.textHandler(i),a.tokenHandler(t,r)});t=t.slice(c);p&&(t=j(t)),this.textHandler(t)},y.prototype.textHandler=function(t){this.push("write("+JSON.stringify(t)+");")},y.prototype.tokenHandler=function(t,e){t=g[t];"directive"===t?this.compileTag(e):"output"===t&&((t=this.extractEnt(e,u,"@")).src=t.src.replace(/\|\|/g,"~").split("|"),t.src=t.src.map(function(t){return t.split("~").join("||")}),1<(e=this.injectEnt(t,"@")).length?(t=e.slice(1).map(this.parseFilter.bind(this)),this.push("filter("+this.parseExpr(e[0])+","+t.join(",")+");")):this.push("filter("+this.parseExpr(e[0])+");"))},y.prototype.compileTag=function(t){var e=t.split(" ")[0],i=x[e];if(!i)throw new Error("Invalid tag: "+t);i.call(this,t.slice(e.length).trim())},y.prototype.parseFilter=function(t){var e=(t=t.trim()).match(/[:(]/),i=e?e.index:-1;if(i<0)return JSON.stringify([t]);e=t.slice(0,i),i=":"===t.charAt(i)?t.slice(i+1):t.slice(i+1,-1),i=this.parseExpr(i,{terms:!0});return"["+JSON.stringify(e)+","+i+"]"},y.prototype.extractEnt=function(t,e,i){var n=[],r="function"==typeof i;return{src:t=t.replace(e,function(t){var e=r?i(t):i;return e?(n.push(t),e):t}),subs:n}},y.prototype.injectEnt=function(t,e){var i=t.src,n=t.subs,t=Array.isArray(i),r=t?i:[i],s=new RegExp("["+e+"]","g"),o=0;return r.forEach(function(t,e){r[e]=t.replace(s,function(){return n[o++]})}),t?r:r[0]},y.prototype.replaceComplex=function(t){t=this.extractEnt(t,/i(\.i|\[[@#i]\])+/g,"v");return t.src=t.src.replace(e,"~"),this.injectEnt(t,"v")},y.prototype.parseExpr=function(e,t){t=t||{};var i=this.extractEnt(e,u,"@");i.src=i.src.replace(h,function(t,e,i,n){return i in v?e+v[i]+n:t});var n=this.extractEnt(i.src,o,function(t){return t in m||c.test(t)?"#":null}),r=this.extractEnt(n.src,a,"i");r.src=r.src.replace(/\s+/g,"");for(var s=r.src;s!==(s=this.replaceComplex(s)););for(;s!==(s=s.replace(/i(\.i|\[[@#i]\])+/,"v")););return s=(s=(s=(s=s.replace(/[iv]\[v?\]/g,"x")).replace(/[@#~v]/g,"i")).replace(l,"%")).replace(/!+[i]/g,"i"),(t.terms?s.split(","):[s]).forEach(function(t){for(;t!==(t=t.replace(/\(i(%i)*\)/g,"i")););if(!t.match(/^i(%i)*/))throw new Error("Invalid expression: "+e+" "+t)}),r.src=r.src.replace(p,this.parseVar.bind(this)),n.src=this.injectEnt(r,"i"),i.src=this.injectEnt(n,"#"),this.injectEnt(i,"@")},y.prototype.parseVar=function(t){var e=Array.proto