/** * ETPL (Enterprise Template) * Copyright 2013 Baidu Inc. All rights reserved. * * @file 模板引擎 * @author errorrik(errorrik@gmail.com) * otakustay(otakustay@gmail.com) */ !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"___"+I++}function i(t,n){var r=t.prototype,i=new Function;i.prototype=n.prototype,t.prototype=new i,t.prototype.constructor=t,e(t.prototype,r)}function o(t){return D[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=S,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 w=y[v],R=w.match(/^\s*([a-z0-9_-]+)(\((.*)\))?\s*$/i);R&&(g.unshift('fs["'+R[1]+'"]('),R[3]&&g.push(",",R[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){var n=t.match(/^\s*([a-z0-9\/\._-]+)\s*(\(\s*master\s*=\s*([a-z0-9\/\._-]+)\s*\))?\s*/i);if(!n)throw new Error("Invalid "+this.type+" syntax: "+t);this.master=n[3],this.name=n[1],f.call(this,t,e),this.blocks={}}function y(t,e){var n=t.match(/^\s*([a-z0-9\/\._-]+)\s*$/i);if(!n)throw new Error("Invalid "+this.type+" syntax: "+t);this.name=n[1],f.call(this,t,e),this.cloneProps=["name"]}function v(t,e){var n=t.match(/^\s*([a-z0-9\/\._-]+)\s*$/i);if(!n)throw new Error("Invalid "+this.type+" syntax: "+t);this.name=n[1],f.call(this,t,e),this.cloneProps=["name","state","blocks","target"],this.blocks={}}function m(t,e){var n=t.match(/^\s*([a-z0-9_]+)\s*=([\s\S]*)$/i);if(!n)throw new Error("Invalid "+this.type+" syntax: "+t);this.name=n[1],this.expr=n[2],f.call(this,t,e),this.cloneProps=["name","expr"]}function w(t,e){var n=t.match(/^\s*([a-z0-9_-]+)\s*(\(([\s\S]*)\))?\s*$/i);if(!n)throw new Error("Invalid "+this.type+" syntax: "+t);this.name=n[1],this.args=n[3],f.call(this,t,e),this.cloneProps=["name","args"]}function R(t,e){var n=t.match(/^\s*([a-z0-9\/\._-]+)\s*(\(([\s\S]*)\))?\s*$/i);if(!n)throw new Error("Invalid "+this.type+" syntax: "+t);this.name=n[1],this.args=n[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"),r=t.match(n);if(!r)throw new Error("Invalid "+this.type+" syntax: "+t);this.list=r[1],this.item=r[2],this.index=r[4],f.call(this,t,e),this.cloneProps=["list","item","index"]}function b(t,e){f.call(this,t,e)}function x(t,e){b.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("[ETPL_TARGET_EXISTS]"+r)}else n.targets[r]=t,e.targets.push(r)}function C(t,e,n){t.commandTypes[e]=n,n.prototype.type=e}function P(t){this.options={commandOpen:"",commandSyntax:/^\s*(\/)?([a-z]*)\s*(?::([\s\S]*))?$/,variableOpen:"${",variableClose:"}",defaultFilter:"html"},this.commandTypes={},C(this,"target",g),C(this,"block",y),C(this,"import",v),C(this,"use",R),C(this,"var",m),C(this,"for",E),C(this,"if",b),C(this,"elif",x),C(this,"else",k),C(this,"filter",w),this.config(t),this.targets={},this.filters=e({},B)}function _(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,deps:{}},l=[];c(t,o,s,0,function(t){var n,p=a.exec(t);if(p&&(n=p[2]||"target")&&(i=e.commandTypes[n.toLowerCase()])&&"function"==typeof i){r();var c=h.current;e.options.strip&&c instanceof u&&(c.value=c.value.replace(/\r?\n[\x20\t]*$/,"\n")),p[1]?c=d(h,i):(c=new i(p[3],e),"function"==typeof c.init&&c.init(h),c.beforeOpen(h),c.open(h)),h.current=c}else/^\s*\/\//.test(t)||l.push(o,t,s);i=null},function(t){l.push(t)}),r(),d(h);var g=[];for(var y in h.deps)g.push(y);return{targets:h.targets,deps:g}}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 I=178245,D={"&":"&","<":"<",">":">",'"':""","'":"'"},B={html:function(t){return t.replace(/[&<>"']/g,o)},url:encodeURIComponent,raw:function(t){return t}},T='var r="";',S="r+=",z=";",O="return r;",$="undefined"!=typeof navigator&&navigator.userAgent.match(/msie\s*([0-9]+)/i);$&&$[1]-0<8&&(T="var r=[],ri=0;",S="r[ri++]=",O='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;nn;n++)e.addChild(this.children[n].clone());for(var n=0,r=this.cloneProps.length;r>n;n++){var i=this.cloneProps[n];e[i]=this[i]}return e}};var j='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;ii;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];if(r){if(r.applyMaster(r.master))return this.children=r.clone().children,e(this),this.state=L.APPLIED,1}else if("error"===this.engine.options.missTarget)throw new Error("[ETPL_MISS_TARGET]"+t+", when extended by "+(this.target?this.target.name:this.name))},g.prototype.isReady=function(){function t(i){for(var o=0,s=i.children.length;s>o;o++){var a=i.children[o];if(a instanceof v){var p=e.targets[a.name];if(!p&&"error"===e.options.missTarget)throw new Error("[ETPL_MISS_TARGET]"+a.name+", when imported by "+n);r=r&&p&&p.isReady(e)}else a instanceof f&&t(a)}}if(this.state>=L.READY)return 1;var e=this.engine,n=this.name,r=1;return this.applyMaster(this.master)?(t(this),r&&(this.state=L.READY),r):void 0},g.prototype.getRenderer=function(){if(this.renderer)return this.renderer;if(this.isReady()){var t=new Function("data","engine",[j,T,this.getRendererBody(),O].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=R.prototype.open=function(t){t.stack.top().addChild(this)},y.prototype.open=function(t){f.prototype.open.call(this,t),t.stack.find(function(t){return t.blocks}).blocks[this.name]=this},x.prototype.open=function(t){var e=new k;e.open(t);var n=d(t,b);n.addChild(this),t.stack.push(this)},k.prototype.open=function(t){var e=d(t,b);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},R.prototype.close=m.prototype.close=function(){},v.prototype.close=function(t){f.prototype.close.call(this,t),this.state=L.READED},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)},f.prototype.beforeOpen=u.prototype.beforeAdd=function(t){if(!t.stack.bottom()){var e=new g(r(),t.engine);e.open(t)}},g.prototype.beforeOpen=function(){},v.prototype.getRendererBody=function(){return this.applyMaster(this.name),f.prototype.getRendererBody.call(this)},R.prototype.getRendererBody=function(){return p("{0}engine.render({2},{{3}}){1}",S,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)):""},b.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))},w.prototype.getRendererBody=function(){var t=this.args;return p("{2}fs[{5}]((function(){{0}{4}{1}})(){6}){3}",T,O,S,z,f.prototype.getRendererBody.call(this),s(this.name),t?","+l(t,this.engine):"")},P.prototype.config=function(t){e(this.options,t)},P.prototype.compile=P.prototype.parse=function(t){var e=new Function('return ""');if(t){var n=_(t,this),r=n.targets;r.length&&(e=this.targets[r[0]].getRenderer())}return e},P.prototype.getRenderer=function(t){var e=this.targets[t];return e?e.getRenderer():void 0},P.prototype.render=function(t,e){var n=this.getRenderer(t);return n?n(e):""},P.prototype.addCommand=function(t,e){var n=e;"function"!=typeof n&&(n=function(t,e){f.call(this,t,e)},n.prototype=e),i(n,f),C(this,t,n)},P.prototype.addFilter=function(t,e){"function"==typeof e&&(this.filters[t]=e)};var F=new P;F.Engine=P,F.version="3.2.0",F.Command=f,F.util={inherits:i,stringFormat:p,stringLiteralize:s,compileVariable:l,parseSource:_},"object"==typeof exports&&"object"==typeof module?exports=module.exports=F:"function"==typeof define&&define.amd?define(F):t.etpl=F}(this);