dropkiq
Version:
Liquid Expressions Simplified
1 lines • 7.36 kB
JavaScript
"use strict";var __assign=this&&this.__assign||function(){return(__assign=Object.assign||function(e){for(var r,t=1,n=arguments.length;t<n;t++)for(var i in r=arguments[t])Object.prototype.hasOwnProperty.call(r,i)&&(e[i]=r[i]);return e}).apply(this,arguments)},__awaiter=this&&this.__awaiter||function(e,r,t,n){return new(t||(t=Promise))((function(i,o){function a(e){try{u(n.next(e))}catch(e){o(e)}}function s(e){try{u(n.throw(e))}catch(e){o(e)}}function u(e){var r;e.done?i(e.value):(r=e.value,r instanceof t?r:new t((function(e){e(r)}))).then(a,s)}u((n=n.apply(e,r||[])).next())}))},__generator=this&&this.__generator||function(e,r){var t,n,i,o,a={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function s(o){return function(s){return function(o){if(t)throw new TypeError("Generator is already executing.");for(;a;)try{if(t=1,n&&(i=2&o[0]?n.return:o[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;switch(n=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return a.label++,{value:o[1],done:!1};case 5:a.label++,n=o[1],o=[0];continue;case 7:o=a.ops.pop(),a.trys.pop();continue;default:if(!(i=(i=a.trys).length>0&&i[i.length-1])&&(6===o[0]||2===o[0])){a=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]<i[3])){a.label=o[1];break}if(6===o[0]&&a.label<i[1]){a.label=i[1],i=o;break}if(i&&a.label<i[2]){a.label=i[2],a.ops.push(o);break}i[2]&&a.ops.pop(),a.trys.pop();continue}o=r.call(e,a)}catch(e){o=[6,e],n=0}finally{t=i=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,s])}}},__values=this&&this.__values||function(e){var r="function"==typeof Symbol&&Symbol.iterator,t=r&&e[r],n=0;if(t)return t.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&n>=e.length&&(e=void 0),{value:e&&e[n++],done:!e}}};throw new TypeError(r?"Object is not iterable.":"Symbol.iterator is not defined.")},__read=this&&this.__read||function(e,r){var t="function"==typeof Symbol&&e[Symbol.iterator];if(!t)return e;var n,i,o=t.call(e),a=[];try{for(;(void 0===r||r-- >0)&&!(n=o.next()).done;)a.push(n.value)}catch(e){i={error:e}}finally{try{n&&!n.done&&(t=o.return)&&t.call(o)}finally{if(i)throw i.error}}return a},__spread=this&&this.__spread||function(){for(var e=[],r=0;r<arguments.length;r++)e=e.concat(__read(arguments[r]));return e};function __export(e){for(var r in e)exports.hasOwnProperty(r)||(exports[r]=e[r])}Object.defineProperty(exports,"__esModule",{value:!0});var context_1=require("./context/context"),node_1=require("./fs/node"),_=require("./util/underscore"),tokenizer_1=require("./parser/tokenizer"),render_1=require("./render/render"),tag_1=require("./template/tag/tag"),filter_1=require("./template/filter/filter"),parser_1=require("./parser/parser"),value_1=require("./template/value"),tags_1=require("./builtin/tags"),filters_1=require("./builtin/filters"),liquid_options_1=require("./liquid-options"),async_1=require("./util/async");__export(require("./types"));var Liquid=function(){function e(e){var r=this;void 0===e&&(e={}),this.cache={},this.options=liquid_options_1.applyDefault(liquid_options_1.normalize(e)),this.parser=new parser_1.default(this),this.renderer=new render_1.Render,this.tokenizer=new tokenizer_1.Tokenizer(this.options),this.fs=e.fs||node_1.default,_.forOwn(tags_1.default,(function(e,t){return r.registerTag(t,e)})),_.forOwn(filters_1.default,(function(e,t){return r.registerFilter(t,e)}))}return e.prototype.parse=function(e,r){var t=this.tokenizer.tokenize(e,r);return this.parser.parse(t)},e.prototype._render=function(e,r,t,n){var i=__assign(__assign({},this.options),liquid_options_1.normalize(t)),o=new context_1.Context(r,i,n);return this.renderer.renderTemplates(e,o)},e.prototype.render=function(e,r,t){return __awaiter(this,void 0,void 0,(function(){return __generator(this,(function(n){return[2,async_1.toThenable(this._render(e,r,t,!1))]}))}))},e.prototype.renderSync=function(e,r,t){return async_1.toValue(this._render(e,r,t,!0))},e.prototype._parseAndRender=function(e,r,t,n){var i=this.parse(e);return this._render(i,r,t,n)},e.prototype.parseAndRender=function(e,r,t){return __awaiter(this,void 0,void 0,(function(){return __generator(this,(function(n){return[2,async_1.toThenable(this._parseAndRender(e,r,t,!1))]}))}))},e.prototype.parseAndRenderSync=function(e,r,t){return async_1.toValue(this._parseAndRender(e,r,t,!0))},e.prototype._parseFile=function(e,r,t){var n,i,o,a,s,u,l,c,p,_,f,h,d=this;return __generator(this,(function(y){switch(y.label){case 0:n=__assign(__assign({},this.options),liquid_options_1.normalize(r)),i=n.root.map((function(r){return d.fs.resolve(r,e,n.extname)})),void 0!==this.fs.fallback&&void 0!==(s=this.fs.fallback(e))&&i.push(s),y.label=1;case 1:y.trys.push([1,11,12,13]),o=__values(i),a=o.next(),y.label=2;case 2:return a.done?[3,10]:(s=a.value,this.options.cache&&this.cache[s]?[2,this.cache[s]]:t?(u=this.fs.existsSync(s),[3,5]):[3,3]);case 3:return[4,this.fs.exists(s)];case 4:u=y.sent(),y.label=5;case 5:return u?(c=this.parse,t?(p=this.fs.readFileSync(s),[3,8]):[3,6]):[3,9];case 6:return[4,this.fs.readFile(s)];case 7:p=y.sent(),y.label=8;case 8:return l=c.apply(this,[p,s]),[2,this.cache[s]=l];case 9:return a=o.next(),[3,2];case 10:return[3,13];case 11:return _=y.sent(),f={error:_},[3,13];case 12:try{a&&!a.done&&(h=o.return)&&h.call(o)}finally{if(f)throw f.error}return[7];case 13:throw this.lookupError(e,n.root)}}))},e.prototype.parseFile=function(e,r){return __awaiter(this,void 0,void 0,(function(){return __generator(this,(function(t){return[2,async_1.toThenable(this._parseFile(e,r,!1))]}))}))},e.prototype.parseFileSync=function(e,r){return async_1.toValue(this._parseFile(e,r,!0))},e.prototype.renderFile=function(e,r,t){return __awaiter(this,void 0,void 0,(function(){var n;return __generator(this,(function(i){switch(i.label){case 0:return[4,this.parseFile(e,t)];case 1:return n=i.sent(),[2,this.render(n,r,t)]}}))}))},e.prototype.renderFileSync=function(e,r,t){var n=liquid_options_1.normalize(t),i=this.parseFileSync(e,n);return this.renderSync(i,r,t)},e.prototype._evalValue=function(e,r){return new value_1.Value(e,this.options.strictFilters).value(r)},e.prototype.evalValue=function(e,r){return __awaiter(this,void 0,void 0,(function(){return __generator(this,(function(t){return[2,async_1.toThenable(this._evalValue(e,r))]}))}))},e.prototype.evalValueSync=function(e,r){return async_1.toValue(this._evalValue(e,r))},e.prototype.registerFilter=function(e,r){return filter_1.Filter.register(e,r)},e.prototype.registerTag=function(e,r){return tag_1.Tag.register(e,r)},e.prototype.plugin=function(r){return r.call(this,e)},e.prototype.express=function(){var e=this;return function(r,t,n){var i={root:__spread(liquid_options_1.normalizeStringArray(this.root),e.options.root)};e.renderFile(r,t,i).then((function(e){return n(null,e)}),n)}},e.prototype.lookupError=function(e,r){var t=new Error("ENOENT");return t.message='ENOENT: Failed to lookup "'+e+'" in "'+r+'"',t.code="ENOENT",t},e.prototype.getTemplate=function(e,r){return __awaiter(this,void 0,void 0,(function(){return __generator(this,(function(t){return[2,this.parseFile(e,r)]}))}))},e.prototype.getTemplateSync=function(e,r){return this.parseFileSync(e,r)},e}();exports.Liquid=Liquid;