forked from janl/mustache.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmplat-mustache.min.js
1 lines (1 loc) · 5.82 KB
/
tmplat-mustache.min.js
1
!function(t,e){"object"==typeof exports&&exports&&"string"!=typeof exports.nodeName?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):(t.TmplatMustache={},e(t.TmplatMustache))}(this,function(j){var c=Object.prototype.hasOwnProperty,o=Object.prototype.toString,C=Array.isArray||function(t){return"[object Array]"===o.call(t)};function u(t){return"function"==typeof t}function S(t){return t.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g,"\\$&")}function p(t){if(null==t)return null;var e,n={};for(e in t)n[e.toLowerCase()]=e;return n}function l(t,e,n){t=t&&t[e];return t&&n[t]}function h(t,e){return null!=t&&e in t}var e=RegExp.prototype.test;var n=/\S/;function O(t){return!e.call(n,t)}var r={"&":"&","<":"<",">":">",'"':""","'":"'","/":"/","`":"`","=":"="};var A=/\s*/,I=/\s+/,R=/\s*=/,M=/\s*\}/,P=/#|\^|\/|>|\{|&|=|!/;function i(t,e){if(!t)return[];var n,r,i,a=[],o=[],s=[],c=!1,u=!1;function p(t){if("string"==typeof t&&(t=t.split(I,2)),!C(t)||2!==t.length)throw new Error("Invalid tags: "+t);n=new RegExp(S(t[0])+"\\s*"),r=new RegExp("\\s*"+S(t[1])),i=new RegExp("\\s*"+S("}"+t[1]))}p(e||j.tags);for(var l,h,f,w,d,y,g=new L(t);!g.eos();){if(l=g.pos,f=g.scanUntil(n))for(var v=0,m=f.length;v<m;++v)if(O(w=f.charAt(v))?s.push(o.length):u=!0,o.push(["text",w,l,l+1]),l+=1,"\n"===w){if(c&&!u)for(;s.length;)delete o[s.pop()];else s=[];u=c=!1}if(!g.scan(n))break;if(c=!0,h=g.scan(P)||"name",g.scan(A),"="===h?(f=g.scanUntil(R),g.scan(R),g.scanUntil(r)):"{"===h?(f=g.scanUntil(i),g.scan(M),g.scanUntil(r),h="&"):f=g.scanUntil(r),!g.scan(r))throw new Error("Unclosed tag at "+g.pos);if(d=[h,f,l,g.pos],o.push(d),"#"===h||"^"===h)a.push(d);else if("/"===h){if(!(y=a.pop()))throw new Error('Unopened section "'+f+'" at '+l);if(y[1]!==f)throw new Error('Unclosed section "'+y[1]+'" at '+l)}else"name"===h||"{"===h||"&"===h?u=!0:"="===h&&p(f)}if(y=a.pop())throw new Error('Unclosed section "'+y[1]+'" at '+g.pos);for(var b,x=function(t){for(var e,n,r=[],i=0,a=t.length;i<a;++i)(e=t[i])&&("text"===e[0]&&n&&"text"===n[0]?(n[1]+=e[1],n[3]=e[3]):(r.push(e),n=e));return r}(o),k=[],E=k,T=[],U=0,V=x.length;U<V;++U)switch((b=x[U])[0]){case"#":case"^":E.push(b),T.push(b),E=b[4]=[];break;case"/":T.pop()[5]=b[2],E=0<T.length?T[T.length-1][4]:k;break;default:E.push(b)}return k}function L(t){this.string=t,this.tail=t,this.pos=0}function a(t,e){this.view=t,this.cache={".":this.view},this.parent=e}function t(){this.cache={}}L.prototype.eos=function(){return""===this.tail},L.prototype.scan=function(t){t=this.tail.match(t);if(!t||0!==t.index)return"";t=t[0];return this.tail=this.tail.substring(t.length),this.pos+=t.length,t},L.prototype.scanUntil=function(t){var e,n=this.tail.search(t);switch(n){case-1:e=this.tail,this.tail="";break;case 0:e="";break;default:e=this.tail.substring(0,n),this.tail=this.tail.substring(n)}return this.pos+=e.length,e},a.prototype.push=function(t){return new a(t,this)},a.prototype.lookup=async function(t){t=t&&t.toLowerCase();var e,n=this.cache;if(c.call(n,t))e=n[t];else{for(var r,i,a,o=this,s=!1;o;){if(0<t.indexOf("."))for(e=o.view,r=t.split("."),a=0;null!=e&&a<r.length;)i=p(e),a===r.length-1&&(s=h(i,r[a])),e=l(i,r[a++],e);else e=l(i=p(o.view),t,o.view),s=h(i,t);if(s)break;o=o.parent}n[t]=e}return e=u(e)?await e.call(this.view):e},t.prototype.clearCache=function(){this.cache={}},t.prototype.parse=function(t,e){var n=this.cache,r=n[t];return r=null==r?n[t]=i(t,e):r},t.prototype.render=async function(t,e,n){var r=this.parse(t),e=e instanceof a?e:new a(e);return this.renderTokens(r,e,n,t)},t.prototype.renderTokens=async function(t,e,n,r){for(var i,a,o,s="",c=0,u=t.length;c<u;++c)o=void 0,"#"===(a=(i=t[c])[0])?o=await this.renderSection(i,e,n,r):"^"===a?o=await this.renderInverted(i,e,n,r):">"===a?o=await this.renderPartial(i,e,n,r):"&"===a?o=await this.escapedValue(i,e):"name"===a?o=await this.unescapedValue(i,e):"text"===a&&(o=this.rawValue(i)),void 0!==o&&(s+=o);return s},t.prototype.renderSection=async function(t,e,n,r){var i=this,a="",o=await e.lookup(t[1]);if(o){if(C(o))for(var s=0,c=o.length;s<c;++s)a+=await this.renderTokens(t[4],e.push(o[s]),n,r);else if("object"==typeof o||"string"==typeof o||"number"==typeof o)a+=await this.renderTokens(t[4],e.push(o),n,r);else if(u(o)){if("string"!=typeof r)throw new Error("Cannot use higher-order sections without the original template");null!=(o=await o.call(e.view,r.slice(t[3],t[5]),async function(t){return i.render(t,e,n)}))&&(a+=o)}else a+=await this.renderTokens(t[4],e,n,r);return a}},t.prototype.renderInverted=async function(t,e,n,r){var i=await e.lookup(t[1]);if(!i||C(i)&&0===i.length)return this.renderTokens(t[4],e,n,r)},t.prototype.renderPartial=async function(t,e,n){if(n)return t=u(n)?n(t[1]):n[t[1]],null!=t?this.renderTokens(this.parse(t),e,n,t):void 0},t.prototype.decoratedValue=function(t,e){if(null!=t){if(C(t))for(var n=[],r=0,i=t.length;r<i;++r)n.push(e(t[r]));else if("[object Object]"===o.call(t))for(var a in n=[],t)c.call(t,a)&&n.push(e(t[a]));return n?n.length?n.join(","):"":e(t)}},t.prototype.unescapedValue=async function(t,e){return this.decoratedValue(await e.lookup(t[1]),String)},t.prototype.escapedValue=async function(t,e){return this.decoratedValue(await e.lookup(t[1]),j.escape)},t.prototype.rawValue=function(t){return t[1]},j.name="tmplat-mustache",j.version="2.5.0",j.tags=["{","}"];var s=new t;return j.clearCache=function(){return s.clearCache()},j.parse=function(t,e){return s.parse(t,e)},j.render=async function(t,e,n){if("string"!=typeof t)throw new TypeError('Invalid template! Template should be a "string" but "'+(C(r=t)?"array":typeof r)+'" was given as the first argument for tmplatMustache#render(template, view, partials)');var r;return s.render(t,e,n)},j.to_html=async function(t,e,n,r){t=await j.render(t,e,n);if(!u(r))return t;r(t)},j.escape=function(t){return String(t).replace(/[&<>"'`=\/]/g,function(t){return r[t]})},j.Scanner=L,j.Context=a,j.Writer=t,j});