blob: 11e992eceb26802aab7ebf1d942010695b89225c [file] [log] [blame]
!function(t){function e(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n]);return t}function n(){this.raw=[],this.length=0}function r(){return"___"+D++}function i(t,e){var n=new Function;n.prototype=e.prototype,t.prototype=new n,t.prototype.constructor=t}function o(t){return B[t]}function s(t){return'"'+t.replace(/\x5C/g,"\\\\").replace(/"/g,'\\"').replace(/\x0A/g,"\\n").replace(/\x09/g,"\\t").replace(/\x0D/g,"\\r")+'"'}function a(t){return t.replace(/[\^\[\]\$\(\)\{\}\?\*\.\+]/g,function(t){return"\\"+t})}function p(t){var e=arguments;return t.replace(/\{([0-9]+)\}/g,function(t,n){return e[n-0+1]})}function h(t){return t=t.replace(/^\s*\*/,""),p('gv({0},["{1}"])',s(t),t.replace(/\[['"]?([^'"]+)['"]?\]/g,function(t,e){return"."+e}).split(".").join('","'))}function c(t,e,n,r,i,o){for(var s=n.length,a=t.split(e),p=0,h=[],c=0,l=a.length;l>c;c++){var u=a[c];if(c){var f=1;for(p++;;){var d=u.indexOf(n);if(0>d){h.push(p>1&&f?e:"",u);break}if(p=r?p-1:0,h.push(p>0&&f?e:"",u.slice(0,d),p>0?n:""),u=u.slice(d+s),f=0,0===p)break}0===p&&(i(h.join("")),o(u),h=[])}else u&&o(u)}p>0&&h.length>0&&(o(e),o(h.join("")))}function l(t,e,n){var r,i=[],o=e.options,a="",p="",u="",f="";return n&&(a="ts(",p=")",u=j,f=z,r=o.defaultFilter),c(t,o.variableOpen,o.variableClose,1,function(t){n&&t.indexOf("|")<0&&r&&(t+="|"+r);var o=t.indexOf("|"),s=(o>0?t.slice(0,o):t).replace(/^\s+/,"").replace(/\s+$/,""),c=o>0?t.slice(o+1):"",d=0===s.indexOf("*"),g=[d?"":a,h(s),d?"":p];if(c){c=l(c,e);for(var y=c.split("|"),v=0,m=y.length;m>v;v++){var R=y[v];/^\s*([a-z0-9_-]+)(\((.*)\))?\s*$/i.test(R)&&(g.unshift('fs["'+RegExp.$1+'"]('),RegExp.$3&&g.push(",",RegExp.$3),g.push(")"))}}i.push(u,g.join(""),f)},function(t){i.push(u,n?s(t):t,f)}),i.join("")}function u(t,e){this.value=t,this.engine=e}function f(t,e){this.value=t,this.engine=e,this.children=[],this.cloneProps=[]}function d(t,e){var n=t.stack,r=e?n.find(function(t){return t instanceof e}):n.bottom();if(r){for(var i;(i=n.top())!==r;){if(!i.autoClose)throw new Error(i.type+" must be closed manually: "+i.value);i.autoClose(t)}r.close(t)}return r}function g(t,e){if(!/^\s*([a-z0-9\/_-]+)\s*(\(\s*master\s*=\s*([a-z0-9\/_-]+)\s*\))?\s*/i.test(t))throw new Error("Invalid "+this.type+" syntax: "+t);this.master=RegExp.$3,this.name=RegExp.$1,f.call(this,t,e),this.blocks={}}function y(t,e){if(!/^\s*([a-z0-9\/_-]+)\s*$/i.test(t))throw new Error("Invalid "+this.type+" syntax: "+t);this.name=RegExp.$1,f.call(this,t,e),this.cloneProps=["name"]}function v(t,e){if(!/^\s*([a-z0-9\/_-]+)\s*$/i.test(t))throw new Error("Invalid "+this.type+" syntax: "+t);this.name=RegExp.$1,f.call(this,t,e),this.cloneProps=["name","state","blocks"],this.blocks={}}function m(t,e){if(!/^\s*([a-z0-9_]+)\s*=([\s\S]*)$/i.test(t))throw new Error("Invalid "+this.type+" syntax: "+t);this.name=RegExp.$1,this.expr=RegExp.$2,f.call(this,t,e),this.cloneProps=["name","expr"]}function R(t,e){if(!/^\s*([a-z0-9_-]+)\s*(\(([\s\S]*)\))?\s*$/i.test(t))throw new Error("Invalid "+this.type+" syntax: "+t);this.name=RegExp.$1,this.args=RegExp.$3,f.call(this,t,e),this.cloneProps=["name","args"]}function x(t,e){if(!/^\s*([a-z0-9\/_-]+)\s*(\(([\s\S]*)\))?\s*$/i.test(t))throw new Error("Invalid "+this.type+" syntax: "+t);this.name=RegExp.$1,this.args=RegExp.$3,f.call(this,t,e),this.cloneProps=["name","args"]}function E(t,e){var n=new RegExp(p("^\\s*({0}[\\s\\S]+{1})\\s+as\\s+{0}([0-9a-z_]+){1}\\s*(,\\s*{0}([0-9a-z_]+){1})?\\s*$",a(e.options.variableOpen),a(e.options.variableClose)),"i");if(!n.test(t))throw new Error("Invalid "+this.type+" syntax: "+t);this.list=RegExp.$1,this.item=RegExp.$2,this.index=RegExp.$4,f.call(this,t,e),this.cloneProps=["list","item","index"]}function w(t,e){f.call(this,t,e)}function b(t,e){w.call(this,t,e)}function k(t,e){f.call(this,t,e)}function A(t,e){e.target=t;var n=e.engine,r=t.name;if(n.targets[r])switch(n.options.namingConflict){case"override":n.targets[r]=t,e.targets.push(r);case"ignore":break;default:throw new Error("Target exists: "+r)}else n.targets[r]=t,e.targets.push(r)}function C(t,e){M[t]=e,e.prototype.type=t}function O(t){this.options={commandOpen:"<!--",commandClose:"-->",commandSyntax:/^\s*(\/)?([a-z]+)\s*(?::([\s\S]*))?$/,variableOpen:"${",variableClose:"}",defaultFilter:"html"},this.config(t),this.targets={},this.filters=e({},I)}function P(t,e){function r(){var t;if(l.length>0&&(t=l.join(""))){var n=new u(t,e);n.beforeAdd(h),p.top().addChild(n),l=[],e.options.strip&&h.current instanceof f&&(n.value=t.replace(/^[\x20\t\r]*\n/,"")),h.current=n}}var i,o=e.options.commandOpen,s=e.options.commandClose,a=e.options.commandSyntax,p=new n,h={engine:e,targets:[],stack:p,target:null},l=[];return c(t,o,s,0,function(t){var n=a.exec(t);if(n&&(i=M[n[2].toLowerCase()])&&"function"==typeof i){r();var p=h.current;e.options.strip&&p instanceof u&&(p.value=p.value.replace(/\r?\n[\x20\t]*$/,"\n")),n[1]?p=d(h,i):(p=new i(n[3],e),"function"==typeof p.beforeOpen&&p.beforeOpen(h),p.open(h)),h.current=p}else/^\s*\/\//.test(t)||l.push(o,t,s);i=null},function(t){l.push(t)}),r(),d(h),h.targets}n.prototype={push:function(t){this.raw[this.length++]=t},pop:function(){if(this.length>0){var t=this.raw[--this.length];return this.raw.length=this.length,t}},top:function(){return this.raw[this.length-1]},bottom:function(){return this.raw[0]},find:function(t){for(var e=this.length;e--;){var n=this.raw[e];if(t(n))return n}}};var D=178245,B={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;"},I={html:function(t){return t.replace(/[&<>"']/g,o)},url:encodeURIComponent,raw:function(t){return t}},_='var r="";',j="r+=",z=";",S="return r;";"undefined"!=typeof navigator&&/msie\s*([0-9]+)/i.test(navigator.userAgent)&&RegExp.$1-0<8&&(_="var r=[],ri=0;",j="r[ri++]=",S='return r.join("");'),u.prototype={getRendererBody:function(){var t=this.value,e=this.engine.options;return!t||e.strip&&/^\s*$/.test(t)?"":l(t,this.engine,1)},clone:function(){return this}},f.prototype={addChild:function(t){this.children.push(t)},open:function(t){var e=t.stack.top();e&&e.addChild(this),t.stack.push(this)},close:function(t){t.stack.top()===this&&t.stack.pop()},getRendererBody:function(){for(var t=[],e=this.children,n=0;n<e.length;n++)t.push(e[n].getRendererBody());return t.join("")},clone:function(){for(var t=new this.constructor(this.value,this.engine),e=0,n=this.children.length;n>e;e++)t.addChild(this.children[e].clone());for(var e=0,n=this.cloneProps.length;n>e;e++){var r=this.cloneProps[e];t[r]=this[r]}return t}};var F='data=data||{};var v={},fs=engine.filters,hg=typeof data.get=="function",gv=function(n,ps){var p=ps[0],d=v[p];if(d==null){if(hg){return data.get(n);}d=data[p];}for(var i=1,l=ps.length;i<l;i++)if(d!=null)d = d[ps[i]];return d;},ts=function(s){if(typeof s==="string"){return s;}if(s==null){s="";}return ""+s;};';i(g,f),i(y,f),i(v,f),i(m,f),i(R,f),i(x,f),i(E,f),i(w,f),i(b,w),i(k,w);var L={READING:1,READED:2,APPLIED:3,READY:4};v.prototype.applyMaster=g.prototype.applyMaster=function(t){function e(t){var r=t.children;if(r instanceof Array)for(var i=0,o=r.length;o>i;i++){var s=r[i];s instanceof y&&n[s.name]&&(s=r[i]=n[s.name]),e(s)}}if(this.state>=L.APPLIED)return 1;var n=this.blocks,r=this.engine.targets[t];return r&&r.applyMaster(r.master)?(this.children=r.clone().children,e(this),this.state=L.APPLIED,1):void 0},g.prototype.isReady=function(){function t(r){for(var i=0,o=r.children.length;o>i;i++){var s=r.children[i];if(s instanceof v){var a=e.targets[s.name];n=n&&a&&a.isReady(e)}else s instanceof f&&t(s)}}if(this.state>=L.READY)return 1;var e=this.engine,n=1;return this.applyMaster(this.master)?(t(this),n&&(this.state=L.READY),n):void 0},g.prototype.getRenderer=function(){if(this.renderer)return this.renderer;if(this.isReady()){var t=new Function("data","engine",[F,_,this.getRendererBody(),S].join("\n")),e=this.engine;return this.renderer=function(n){return t(n,e)},this.renderer}return null},g.prototype.open=function(t){d(t),f.prototype.open.call(this,t),this.state=L.READING,A(this,t)},m.prototype.open=x.prototype.open=function(t){t.stack.top().addChild(this)},y.prototype.open=function(t){f.prototype.open.call(this,t),(t.imp||t.target).blocks[this.name]=this},b.prototype.open=function(t){var e=new k;e.open(t);var n=d(t,w);n.addChild(this),t.stack.push(this)},k.prototype.open=function(t){var e=d(t,w);e.addChild(this),t.stack.push(this)},v.prototype.open=function(t){this.parent=t.stack.top(),this.target=t.target,f.prototype.open.call(this,t),this.state=L.READING,t.imp=this},x.prototype.close=m.prototype.close=function(){},v.prototype.close=function(t){f.prototype.close.call(this,t),this.state=L.READED,t.imp=null},g.prototype.close=function(t){f.prototype.close.call(this,t),this.state=this.master?L.READED:L.APPLIED,t.target=null},v.prototype.autoClose=function(t){var e=this.parent.children;e.push.apply(e,this.children),this.children.length=0;for(var n in this.blocks)this.target.blocks[n]=this.blocks[n];this.blocks={},this.close(t)},x.prototype.beforeOpen=v.prototype.beforeOpen=m.prototype.beforeOpen=E.prototype.beforeOpen=R.prototype.beforeOpen=y.prototype.beforeOpen=w.prototype.beforeOpen=u.prototype.beforeAdd=function(t){if(!t.stack.bottom()){var e=new g(r(),t.engine);e.open(t)}},v.prototype.getRendererBody=function(){return this.applyMaster(this.name),f.prototype.getRendererBody.call(this)},x.prototype.getRendererBody=function(){return p("{0}engine.render({2},{{3}}){1}",j,z,s(this.name),l(this.args,this.engine).replace(/(^|,)\s*([a-z0-9_]+)\s*=/gi,function(t,e,n){return(e||"")+s(n)+":"}))},m.prototype.getRendererBody=function(){return this.expr?p("v[{0}]={1};",s(this.name),l(this.expr,this.engine)):""},w.prototype.getRendererBody=function(){return p("if({0}){{1}}",l(this.value,this.engine),f.prototype.getRendererBody.call(this))},k.prototype.getRendererBody=function(){return p("}else{{0}",f.prototype.getRendererBody.call(this))},E.prototype.getRendererBody=function(){return p('var {0}={1};if({0} instanceof Array)for (var {4}=0,{5}={0}.length;{4}<{5};{4}++){v[{2}]={4};v[{3}]={0}[{4}];{6}}else if(typeof {0}==="object")for(var {4} in {0}){v[{2}]={4};v[{3}]={0}[{4}];{6}}',r(),l(this.list,this.engine),s(this.index||r()),s(this.item),r(),r(),f.prototype.getRendererBody.call(this))},R.prototype.getRendererBody=function(){var t=this.args;return p("{2}fs[{5}]((function(){{0}{4}{1}})(){6}){3}",_,S,j,z,f.prototype.getRendererBody.call(this),s(this.name),t?","+l(t,this.engine):"")};var M={};C("target",g),C("block",y),C("import",v),C("use",x),C("var",m),C("for",E),C("if",w),C("elif",b),C("else",k),C("filter",R),O.prototype.config=function(t){e(this.options,t)},O.prototype.compile=O.prototype.parse=function(t){if(t){var e=P(t,this);if(e.length)return this.targets[e[0]].getRenderer()}return new Function('return ""')},O.prototype.getRenderer=function(t){var e=this.targets[t];return e?e.getRenderer():void 0},O.prototype.render=function(t,e){var n=this.getRenderer(t);return n?n(e):""},O.prototype.addFilter=function(t,e){"function"==typeof e&&(this.filters[t]=e)};var G=new O;G.Engine=O,"object"==typeof exports&&"object"==typeof module?exports=module.exports=G:"function"==typeof define&&define.amd?define(G):t.etpl=G}(this);