-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathTangular.min.js
More file actions
1 lines (1 loc) · 5.92 KB
/
Tangular.min.js
File metadata and controls
1 lines (1 loc) · 5.92 KB
1
(function(e){if(e.Tangular)return;var t={};var v=t.helpers={};t.version='v5.0.5';t.cache={};e.Ta=e.Tangular=t;e.Thelpers=v;var s={null:true,undefined:true,true:true,false:true,Object:1,String:1,Number:1,Boolean:1,Date:1,Array:1,window:1,global:1,arguments:1,eval:1,Function:1,function:1,var:1,let:1,const:1,delete:1};var l=/\{\{.*?\}\}/g;var u=/\{\{|\}\}/g;var n=/[<>&"]/g;var o=/\n$/g;function w(e,r){var t='';var n=[];var i=0;for(var a=0;a<e.length;a++){var l=e.charCodeAt(a);if(!i){if(t&&l>47&&l<58||l>64&&l<91||l>96&&l<123||(l===95||l===36)){t+=e.charAt(a);continue}if(t){if(!s[t]&&n.indexOf(t)===-1&&(!r||r.indexOf(t)===-1))n.push(t);t=''}}if(l===46||l===124){i=l}else if((i===46||i===124)&&l===40){i=0}else if(l===96||l===34||l===39){if(l===i)i=0;else i=l}}if(t&&!s[t]&&n.indexOf(t)===-1&&(!r||r.indexOf(t)===-1))n.push(t);return n}t.toArray=function(e){var r=Object.keys(e);var t=[];for(var n=0,i=r.length;n<i;n++)t.push({key:r[n],value:e[r[n]]});return t};function i(){this.commands;this.variables;this.builder;this.split='\0'}i.prototype.compile=function(e,r,t){var g=this;var m=0;var b=0;var $=[];var a=l;var y=u;var x;if(r&&t){a=new RegExp(r+'.*?'+t,'g');y=new RegExp(r+'|'+t,'g')}g.template=e;e=e.replace(/\|\|/g,'');g.variables={};g.commands=[];g.builder=e.replace(a,function(f){var e=f.replace(y,'').trim();var r=null;var t=null;var n;var c=false;var v=false;var p=true;if(e==='fi'){m--}else if(e==='end'){b--;$.pop()}else if(e.substring(0,3)==='if '){m++;r=w(e.substring(3),$);if(r.length){for(var i=0;i<r.length;i++){var a=r[i];if(g.variables[a])g.variables[a]++;else g.variables[a]=1}}else r=null;c=true;p=true}else if(e.substring(0,8)==='foreach '){b++;x=e.substring(8).split(' ');$.push(x[0].trim());n=x[2].indexOf('.');if(n!==-1)x[2]=x[2].substring(0,n);r=x[2].trim();if($.indexOf(r)===-1){if(g.variables[r])g.variables[r]++;else g.variables[r]=1;r=[r]}else r=null;v=true}else if(e.substring(0,8)==='else if '){r=w(e.substring(8),$);if(r.length){for(var i=0;i<r.length;i++){var a=r[i];if(g.variables[a])g.variables[a]++;else g.variables[a]=1}}else r=null;c=true}else if(e!=='continue'&&e!=='break'&&e!=='else'){r=w(e);var l=false;for(var i=0;i<r.length;i++){var s=r[i];if(s+'('===e.substring(0,s.length+1)){l=true;continue}if(g.variables[s])g.variables[s]++;else g.variables[s]=1}if(!r.length)r=null;var u=e.indexOf('|');var h=null;if(l){h=e.substring(0,u===-1?e.length:u);if(u===-1)e='';else e=''+e.substring(n)}else if(!l&&u===-1)e+=' | encode';t=e.split('|');e=t[0];t=t.slice(1);if(l)t.unshift(h);if(t.length){for(var i=0;i<t.length;i++){var o=t[i].trim();var d=l&&!i;n=o.indexOf('(');if(n===-1){o='Thelpers.$execute($helpers,model,\''+o+'\','+(d?'':')')}else o='Thelpers.$execute($helpers,model,\''+o.substring(0,n)+'\','+(d?'':',')+o.substring(n+1);t[i]=o}}else t=null;e=g.safe(e.trim()||'model');p=false}g.commands.push({index:g.commands.length,cmd:e,ifcount:m,loopcount:b,variable:r,helpers:t,isloop:v,isif:c,iscode:p});return g.split}).split(g.split);for(var n=0;n<g.builder.length;n++){var i=g.builder[n];g.builder[n]=i?i.replace(o,''):i}return g.make()};i.prototype.safe=function(e){var r=e.split('.');var t=[];for(var n=1;n<r.length;n++){var i=r.slice(0,n).join('.');t.push(i+'==null?\'\':')}return t.join('')+r.join('.')};i.prototype.make=function(){var e=this;var r=['var $output=$text[0];var $tmp;var $index=0;'];for(var t=0,f=e.commands.length;t<f;t++){var n=e.commands[t];var i;t&&r.push('$output+=$text['+t+'];');if(n.iscode){if(n.isloop){var a='$i'+Math.random().toString(16).substring(3,6);var l=a+'a';var c=n.cmd.lastIndexOf(' in ');if(c===-1)c=n.cmd.lastIndexOf(' of ');i=n.cmd.substring(c+4).trim();i=l+'='+e.safe(i)+';if(!('+l+' instanceof Array)){if('+l+'&&typeof('+l+')===\'object\')'+l+'=Tangular.toArray('+l+')}if('+l+' instanceof Array&&'+l+'.length){for(var '+a+'=0,'+a+'l='+l+'.length;'+a+'<'+a+'l;'+a+'++){$index='+a+';var '+n.cmd.split(' ')[1]+'='+l+'['+a+'];';r.push(i)}else if(n.isif){if(n.cmd.substring(0,8)==='else if ')r.push('}'+n.cmd.substring(0,8).trim()+'('+n.cmd.substring(8).trim()+'){');else r.push(n.cmd.substring(0,3).trim()+'('+n.cmd.substring(3).trim()+'){')}else{switch(n.cmd){case'else':r.push('}else{');break;case'end':r.push('}}');break;case'fi':r.push('}');break;case'break':r.push('break;');break;case'continue':r.push('continue;');break}}}else{if(n.helpers){var s='';for(var u=0;u<n.helpers.length;u++){var v=n.helpers[u];if(u===0)s=v.replace('',n.cmd.trim()).trim();else s=v.replace('',s.trim())}r.push('$tmp='+s+';if($tmp!=null)$output+=$tmp;')}else r.push('if('+n.cmd+'!=null)$output+='+n.cmd+';')}}r.push((f?'$output+=$text['+f+'];':'')+'return $output.charAt(0) === \'\\n\'?$output.substring(1):$output;');delete e.variables.$;var o=Object.keys(e.variables);var p=['$helpers||{}','$||{}','model'];for(var t=0;t<o.length;t++)p.push('model.'+o[t]);for(var t=0;t<e.builder.length;t++)e.builder[t]=e.builder[t].replace(/\1/g,'||');var h='var tangular=function($helpers,$,model'+(o.length?','+o.join(','):'')+'){'+r.join('')+'};return function(model,$,$helpers){try{return tangular('+p.join(',')+')}catch(e){console.error(\'Tangular error:\',e + \'\',$template)}}';return new Function('$text','$template',h.replace(/\1/g,'||'))(e.builder,e.template)};v.$execute=function(e,r,t,n,i,a,l,s,u,f,c){var o=e[t]||v[t];if(!o){console&&console.warn('Tangular: missing helper','"'+t+'"');return n}return o.call(r,n,i,a,l,s,u,f,c)};v.encode=function(e,r){if(r){if(typeof r==='function')return r(e);if(r==='json')return JSON.stringify(e);if(r==='json2')return JSON.stringify(e,null,'\t');if(r==='url'||r==='urlencoded')return encodeURIComponent(e);if(r==='querify')return QUERIFY(e)}return e==null?'':(e+'').replace(n,function(e){switch(e){case'&':return'&';case'<':return'<';case'>':return'>';case'"':return'"'}return e})};v.raw=function(e){return e};t.render=function(e,r,t,n){var e=(new i).compile(e);return e(r==null?{}:r,t,n)};t.compile=function(e,r,t){return(new i).compile(e,r,t)};t.register=function(e,r){v[e]=r;return t}})(typeof window==='undefined'?global:window);