mirror of
https://github.com/alantang1977/hipy.git
synced 2024-11-25 07:53:09 +02:00
1 line
12 KiB
JavaScript
1 line
12 KiB
JavaScript
|
(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?factory(exports):typeof define==="function"&&define.amd?define(["exports"],factory):(global=typeof globalThis!=="undefined"?globalThis:global||self,factory(global.jinja={}))})(this,function(jinja){"use strict";var STRINGS=/'(\\.|[^'])*'|"(\\.|[^"'"])*"/g;var IDENTS_AND_NUMS=/([$_a-z][$\w]*)|([+-]?\d+(\.\d+)?)/g;var NUMBER=/^[+-]?\d+(\.\d+)?$/;var NON_PRIMITIVES=/\[[@#~](,[@#~])*\]|\[\]|\{([@i]:[@#~])(,[@i]:[@#~])*\}|\{\}/g;var IDENTIFIERS=/[$_a-z][$\w]*/gi;var VARIABLES=/i(\.i|\[[@#i]\])*/g;var ACCESSOR=/(\.i|\[[@#i]\])/g;var OPERATORS=/(===?|!==?|>=?|<=?|&&|\|\||[+\-\*\/%])/g;var EOPS=/(^|[^$\w])(and|or|not|is|isnot)([^$\w]|$)/g;var LEADING_SPACE=/^\s+/;var TRAILING_SPACE=/\s+$/;var START_TOKEN=/\{\{\{|\{\{|\{%|\{#/;var TAGS={"{{{":/^('(\\.|[^'])*'|"(\\.|[^"'"])*"|.)+?\}\}\}/,"{{":/^('(\\.|[^'])*'|"(\\.|[^"'"])*"|.)+?\}\}/,"{%":/^('(\\.|[^'])*'|"(\\.|[^"'"])*"|.)+?%\}/,"{#":/^('(\\.|[^'])*'|"(\\.|[^"'"])*"|.)+?#\}/};var delimeters={"{%":"directive","{{":"output","{#":"comment"};var operators={and:"&&",or:"||",not:"!",is:"==",isnot:"!="};var constants={true:true,false:false,null:null};function Parser(){this.nest=[];this.compiled=[];this.childBlocks=0;this.parentBlocks=0;this.isSilent=false}Parser.prototype.push=function(line){if(!this.isSilent){this.compiled.push(line)}};Parser.prototype.parse=function(src){this.tokenize(src);return this.compiled};Parser.prototype.tokenize=function(src){var lastEnd=0,parser=this,trimLeading=false;matchAll(src,START_TOKEN,function(open,index,src){var match=src.slice(index+open.length).match(TAGS[open]);match=match?match[0]:"";var simplified=match.replace(STRINGS,"@");if(!match||~simplified.indexOf(open)){return index+1}var inner=match.slice(0,0-open.length);if(inner.charAt(0)==="-")var wsCollapseLeft=true;if(inner.slice(-1)==="-")var wsCollapseRight=true;inner=inner.replace(/^-|-$/g,"").trim();if(parser.rawMode&&open+inner!=="{%endraw"){return index+1}var text=src.slice(lastEnd,index);lastEnd=index+open.length+match.length;if(trimLeading)text=trimLeft(text);if(wsCollapseLeft)text=trimRight(text);if(wsCollapseRight)trimLeading=true;if(open==="{{{"){open="{{";inner+="|safe"}parser.textHandler(text);parser.tokenHandler(open,inner)});var text=src.slice(lastEnd);if(trimLeading)text=trimLeft(text);this.textHandler(text)};Parser.prototype.textHandler=function(text){this.push("write("+JSON.stringify(text)+");")};Parser.prototype.tokenHandler=function(open,inner){var type=delimeters[open];if(type==="directive"){this.compileTag(inner)}else if(type==="output"){var extracted=this.extractEnt(inner,STRINGS,"@");extracted.src=extracted.src.replace(/\|\|/g,"~").split("|");extracted.src=extracted.src.map(function(part){return part.split("~").join("||")});var parts=this.injectEnt(extracted,"@");if(parts.length>1){var filters=parts.slice(1).map(this.parseFilter.bind(this));this.push("filter("+this.parseExpr(parts[0])+","+filters.join(",")+");")}else{this.push("filter("+this.parseExpr(parts[0])+");")}}};Parser.prototype.compileTag=function(str){var directive=str.split(" ")[0];var handler=tagHandlers[directive];if(!handler){throw new Error("Invalid tag: "+str)}handler.call(this,str.slice(directive.length).trim())};Parser.prototype.parseFilter=function(src){src=src.trim();var match=src.match(/[:(]/);var i=match?match.index:-1;if(i<0)return JSON.stringify([src]);var name=src.slice(0,i);var args=src.charAt(i)===":"?src.slice(i+1):src.slice(i+1,-1);args=this.parseExpr(args,{terms:true});return"["+JSON.stringify(name)+","+args+"]"};Parser.prototype.extractEnt=function(src,regex,placeholder){var subs=[],isFunc=typeof placeholder=="function";src=src.replace(regex,function(str){var replacement=isFunc?placeholder(str):placeholder;if(replacement){subs.push(str);return replacement}return str});return{src:src,subs:subs}};Parser.prototype.injectEnt=function(extracted,placeholder){var src=extracted.src,subs=extracted.subs,isArr=Array.isArray(src);var arr=isArr?src:[src];var re=new RegExp("["+placeholder+"]","g"),i=0;arr.forEach(function(src,i
|