-
Notifications
You must be signed in to change notification settings - Fork 0
/
render-min.js
1 lines (1 loc) · 3.56 KB
/
render-min.js
1
XRegExp.install('extensibility');XRegExp.addToken(/([?*+]|{\d+(?:,\d*)?})(\??)/,function(match){return match[1]+(match[2]?'':'?');},{scope:'default',customFlags:'U',trigger:function(){return this.hasFlag('U');}});String.prototype.render=function(){var c,i,N=[{f:_forloop,i:3,r:"{% for (?<c>\\w+) in (?<C>[^\\}]+) %}(?<t>.+){% endfor %}"},{f:_ifcond,i:2,r:"{% if (?<c>[^\\}]+) %}(?<t>[^%]+?)(?:{% else %}(?<T>[^\\{]*))?{% endif %}"},{f:_variable,i:1,r:"{{ ?(?<c>[^ }]+) ?}}"}],t=String(this),O=arguments[1];if(O&&O.templates){var T=O.templates;function _include(t,c,m){if(T[m.C])return T[m.C].render(c,O);}N.unshift({f:_include,i:1,r:"{% include (?<C>[^\\}]+) %}"});}if(typeof arguments[0]==='string')eval('c ='+arguments[0]);else c=arguments[0];function capfirst(s){return upper(s.substr(0,1))+lower(s.substr(1));}function title(s){var i,w=s.split(' ');for(i in w)w[i]=capfirst(w[i]);return w.join(' ');}function lower(s){return s.toLowerCase();}function upper(s){return s.toUpperCase();}function ljust(s,n){return s+_just(s,n);}function rjust(s,n){return _just(s,n)+s;}function dictsort(a,k){var A=_A_(a),o=[],O={},i;if(A){o=a.slice();o.sort(function(a,b){return _N_(a[k])&&_N_(b[k])?a[k]-b[k]:a[k]===b[k]?0:a[k]>b[k]?1:-1;});}else{for(i in a)o.push([i,a[i]]);if(_U_(k))o.sort(function(a,b){return _N_(a[0])&&_N_(b[0])?a[0]-b[0]:a[0]===b[0]?0:a[0]>b[0]?1:-1;});else o.sort(function(a,b){return _N_(a[1][k])&&_N_(b[1][k])?a[1][k]-b[1][k]:a[1][k]===b[1][k]?0:a[1][k]>b[1][k]?1:-1;});for(i in o)O[o[i][0]]=o[i][1];}return A?o:O;}function floatformat(s,n){var x=Math.pow(10,n);return Math.round(x*s)/x;}function abs(i){return Math.abs(parseFloat(i));}function _just(s,n){var r='',n=parseInt(n),s=s.toString();while(n-s.length>0){r+=" ";n--;}return r;}function _isUndefined(c){return typeof c==='undefined';}function _isArray(c){return !_isUndefined(c.slice);}function _isFunction(c){return typeof c==='function';}function _isString(c){return typeof c==='string';}function _isNumber(c){return typeof c==='number';}function _isInteger(c){return _N_(c)&&Math.abs(c);}var _U_=_isUndefined;var _A_=_isArray;var _F_=_isFunction;var _S_=_isString;var _N_=_isNumber;var _I_=_isInteger;function _extract(t,c){var k,A=t.split('|'),C=_isUndefined(c._)?false:c._,V=A[0].split('.');for(k in V)if(_isUndefined(c[V[k]]))return C?_extract(t,C):'';else c=c[V[k]];return _isUndefined(c)?'':(A[1]?_function(c,A[1],c):c);}function _replace(t,c,m){return t.substring(0,m.index)+c+t.substring(m.index+m[0].length);}function _function(n,f,c){var o="";if(f.match(/\w+:/)){var B=f.split(':'),F=B.shift(),A=B.join(':');if(A&&A[0]==='"')A=A.replace(/"/g,'');else A=_extract(A,c)||A;if(_isFunction(n[F]))o=n[F](A);else if(_isFunction(eval(F)))o=eval(F)(n,A);}else if(_isFunction(n[f]))o=n[f]();else if(_isFunction(eval(f)))o=eval(f)(n);return o||'';}function _parse(t,c,n){var z=20,m=XRegExp(n.r,'gsU').exec(t);while(m&&z-->=0){t=_replace(t,n.f(m.t,c,m),m);m=XRegExp(n.r,'gsU').exec(t);}return t;}function _forloop(t,c,m){var j,i=0,o='',C=c,A=_extract(m.C,c);for(j in A){var c={'_':C,'forloop':{"counter":i+1,"counter0":i,"first":!i,"key":j,"last":i===A.length-1}};c[m.c]=A[j];o+=t.render(c,O);i++;}return o;}function _ifcond(t,c,m){var C=_extract(m.c,c);if(C)return t.render(c,O);else return m.T?m.T.render(c,O):'';}function _variable(t,c,m){var a,A=m.c.split('|');var o=_extract(A.shift(),c);while(a=A.shift())if(a&&a.match(/\w+:/))o=_function(o,a,c);else if(!_isFunction(o[a])&&!_isUndefined(o[a]))o=o[a];else if(_isFunction(o[a]))o=o[a]();else if(_isFunction(eval(a)))o=eval(a)(o);return o;}for(i in N)t=_parse(t,c,N[i]);return t;};String.prototype.render.VERSION=1.03;