eta
Version:
Lightweight, fast, and powerful embedded JS template engine
3 lines (2 loc) • 5.96 kB
JavaScript
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).Eta={})}(this,(function(e){"use strict";function n(e){var t,r,i=new Error(e);return t=i,r=n.prototype,Object.setPrototypeOf?Object.setPrototypeOf(t,r):t.__proto__=r,i}function t(e,t,r){var i=t.slice(0,r).split(/\n/),a=i.length,o=i[a-1].length+1;throw n(e+=" at line "+a+" col "+o+":\n\n "+t.split(/\n/)[a-1]+"\n "+Array(o).join(" ")+"^")}n.prototype=Object.create(Error.prototype,{name:{value:"Eta Error",enumerable:!1}});var r=new Function("return this")().Promise;function i(e,n){for(var t in n)r=n,i=t,Object.prototype.hasOwnProperty.call(r,i)&&(e[t]=n[t]);var r,i;return e}function a(e,n,t,r){var i,a;return Array.isArray(n.autoTrim)?(i=n.autoTrim[1],a=n.autoTrim[0]):i=a=n.autoTrim,(t||!1===t)&&(i=t),(r||!1===r)&&(a=r),a||i?"slurp"===i&&"slurp"===a?e.trim():("_"===i||"slurp"===i?e=function(e){return String.prototype.trimLeft?e.trimLeft():e.replace(/^\s+/,"")}(e):"-"!==i&&"nl"!==i||(e=e.replace(/^(?:\r\n|\n|\r)/,"")),"_"===a||"slurp"===a?e=function(e){return String.prototype.trimRight?e.trimRight():e.replace(/\s+$/,"")}(e):"-"!==a&&"nl"!==a||(e=e.replace(/(?:\r\n|\n|\r)$/,"")),e):e}var o={"&":"&","<":"<",">":">",'"':""","'":"'"};function c(e){return o[e]}var s=/`(?:\\[\s\S]|\${(?:[^{}]|{(?:[^{}]|{[^}]*})*})*}|(?!\${)[^\\`])*`/g,l=/'(?:\\[\s\w"'\\`]|[^\n\r'\\])*?'/g,u=/"(?:\\[\s\w"'\\`]|[^\n\r"\\])*?"/g;function p(e){return e.replace(/[.*+\-?^${}()|[\]\\]/g,"\\$&")}function f(e,n){var r=[],i=!1,o=0,c=n.parse;if(n.plugins)for(var f=0;f<n.plugins.length;f++){(T=n.plugins[f]).processTemplate&&(e=T.processTemplate(e,n))}function d(e,t){e&&(e=a(e,n,i,t))&&(e=e.replace(/\\|'/g,"\\$&").replace(/\r\n|\n|\r/g,"\\n"),r.push(e))}n.rmWhitespace&&(e=e.replace(/[\r\n]+/g,"\n").replace(/^\s+|\s+$/gm,"")),s.lastIndex=0,l.lastIndex=0,u.lastIndex=0;for(var g,h=[c.exec,c.interpolate,c.raw].reduce((function(e,n){return e&&n?e+"|"+p(n):n?p(n):e}),""),m=new RegExp("([^]*?)"+p(n.tags[0])+"(-|_)?\\s*("+h+")?\\s*","g"),v=new RegExp("'|\"|`|\\/\\*|(\\s*(-|_)?"+p(n.tags[1])+")","g");g=m.exec(e);){o=g[0].length+g.index;var y=g[1],x=g[2],_=g[3]||"";d(y,x),v.lastIndex=o;for(var w=void 0,b=!1;w=v.exec(e);){if(w[1]){var E=e.slice(o,w.index);m.lastIndex=o=v.lastIndex,i=w[2],b={t:_===c.exec?"e":_===c.raw?"r":_===c.interpolate?"i":"",val:E};break}var I=w[0];if("/*"===I){var R=e.indexOf("*/",v.lastIndex);-1===R&&t("unclosed comment",e,w.index),v.lastIndex=R}else if("'"===I){l.lastIndex=w.index,l.exec(e)?v.lastIndex=l.lastIndex:t("unclosed string",e,w.index)}else if('"'===I){u.lastIndex=w.index,u.exec(e)?v.lastIndex=u.lastIndex:t("unclosed string",e,w.index)}else if("`"===I){s.lastIndex=w.index,s.exec(e)?v.lastIndex=s.lastIndex:t("unclosed string",e,w.index)}}b?r.push(b):t("unclosed tag",e,g.index+y.length)}if(d(e.slice(o,e.length),!1),n.plugins)for(f=0;f<n.plugins.length;f++){var T;(T=n.plugins[f]).processAST&&(r=T.processAST(r,n))}return r}function d(e,n){var t=f(e,n),r="var tR='',__l,__lP"+(n.include?",include=E.include.bind(E)":"")+(n.includeFile?",includeFile=E.includeFile.bind(E)":"")+"\nfunction layout(p,d){__l=p;__lP=d}\n"+(n.useWith?"with("+n.varName+"||{}){":"")+function(e,n){var t=0,r=e.length,i="";for(;t<r;t++){var a=e[t];if("string"==typeof a){i+="tR+='"+a+"'\n"}else{var o=a.t,c=a.val||"";"r"===o?(n.filter&&(c="E.filter("+c+")"),i+="tR+="+c+"\n"):"i"===o?(n.filter&&(c="E.filter("+c+")"),n.autoEscape&&(c="E.e("+c+")"),i+="tR+="+c+"\n"):"e"===o&&(i+=c+"\n")}}return i}(t,n)+(n.includeFile?"if(__l)tR="+(n.async?"await ":"")+"includeFile(__l,Object.assign("+n.varName+",{body:tR},__lP))\n":n.include?"if(__l)tR="+(n.async?"await ":"")+"include(__l,Object.assign("+n.varName+",{body:tR},__lP))\n":"")+"if(cb){cb(null,tR)} return tR"+(n.useWith?"}":"");if(n.plugins)for(var i=0;i<n.plugins.length;i++){var a=n.plugins[i];a.processFnString&&(r=a.processFnString(r,n))}return r}var g=new(function(){function e(e){this.cache=e}return e.prototype.define=function(e,n){this.cache[e]=n},e.prototype.get=function(e){return this.cache[e]},e.prototype.remove=function(e){delete this.cache[e]},e.prototype.reset=function(){this.cache={}},e.prototype.load=function(e){i(this.cache,e)},e}())({});var h={async:!1,autoEscape:!0,autoTrim:[!1,"nl"],cache:!1,e:function(e){var n=String(e);return/[&<>"']/.test(n)?n.replace(/[&<>"']/g,c):n},include:function(e,t){var r=this.templates.get(e);if(!r)throw n('Could not fetch template "'+e+'"');return r(t,this)},parse:{exec:"",interpolate:"=",raw:"~"},plugins:[],rmWhitespace:!1,tags:["<%","%>"],templates:g,useWith:!1,varName:"it"};function m(e,n){var t={};return i(t,h),n&&i(t,n),e&&i(t,e),t}function v(e,t){var r=m(t||{}),i=r.async?function(){try{return new Function("return (async function(){}).constructor")()}catch(e){throw e instanceof SyntaxError?n("This environment doesn't support async/await"):e}}():Function;try{return new i(r.varName,"E","cb",d(e,r))}catch(t){throw t instanceof SyntaxError?n("Bad template syntax\n\n"+t.message+"\n"+Array(t.message.length+1).join("=")+"\n"+d(e,r)+"\n"):t}}function y(e,n){if(n.cache&&n.name&&n.templates.get(n.name))return n.templates.get(n.name);var t="function"==typeof e?e:v(e,n);return n.cache&&n.name&&n.templates.define(n.name,t),t}function x(e,t,i,a){var o=m(i||{});if(!o.async)return y(e,o)(t,o);if(!a){if("function"==typeof r)return new r((function(n,r){try{n(y(e,o)(t,o))}catch(e){r(e)}}));throw n("Please provide a callback function, this env doesn't support Promises")}try{y(e,o)(t,o,a)}catch(e){return a(e)}}e.compile=v,e.compileToString=d,e.config=h,e.configure=function(e){return i(h,e)},e.defaultConfig=h,e.getConfig=m,e.parse=f,e.render=x,e.renderAsync=function(e,n,t,r){return x(e,n,Object.assign({},t,{async:!0}),r)},e.templates=g,Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=eta.min.js.map