|
| 1 | +const ESCAPE = /[&"<]/g, CHARS = { |
| 2 | + '"': '"', |
| 3 | + '&': '&', |
| 4 | + '<': '<', |
| 5 | +}; |
| 6 | + |
| 7 | +const ENDLINES = /[\r\n]+$/g; |
| 8 | +const CURLY = /{{{?\s*([\s\S]*?)\s*}}}?/g; |
| 9 | +const ARGS = /([a-zA-Z$_][^\s=]*)\s*=\s*((["`'])(?:(?=(\\?))\4.)*?\3|{[^}]*}|\[[^\]]*]|\S+)/g; |
| 10 | + |
| 11 | +// $$1 = escape() |
| 12 | +// $$2 = extra blocks |
| 13 | +// $$3 = template values |
| 14 | +function gen(input, options) { |
| 15 | + options = options || {}; |
| 16 | + |
| 17 | + let char, num, action, tmp; |
| 18 | + let last = CURLY.lastIndex = 0; |
| 19 | + let wip='', txt='', match, inner; |
| 20 | + |
| 21 | + let extra=options.blocks||{}, stack=[]; |
| 22 | + let initials = new Set(options.props||[]); |
| 23 | + |
| 24 | + function close() { |
| 25 | + if (wip.length > 0) { |
| 26 | + txt += (txt ? 'x+=' : '=') + '`' + wip + '`;'; |
| 27 | + } else if (txt.length === 0) { |
| 28 | + txt = '="";' |
| 29 | + } |
| 30 | + wip = ''; |
| 31 | + } |
| 32 | + |
| 33 | + while (match = CURLY.exec(input)) { |
| 34 | + wip += input.substring(last, match.index).replace(ENDLINES, ''); |
| 35 | + last = match.index + match[0].length; |
| 36 | + |
| 37 | + inner = match[1].trim(); |
| 38 | + char = inner.charAt(0); |
| 39 | + |
| 40 | + if (char === '!') { |
| 41 | + // comment, continue |
| 42 | + } else if (char === '#') { |
| 43 | + close(); |
| 44 | + [, action, inner] = /^#\s*(\w[\w\d]+)\s*([^]*)/.exec(inner); |
| 45 | + |
| 46 | + if (action === 'expect') { |
| 47 | + inner.split(/[\n\r\s\t]*,[\n\r\s\t]*/g).forEach(key => { |
| 48 | + initials.add(key); |
| 49 | + }); |
| 50 | + } else if (action === 'var') { |
| 51 | + num = inner.indexOf('='); |
| 52 | + tmp = inner.substring(0, num++).trim(); |
| 53 | + inner = inner.substring(num).trim().replace(/[;]$/, ''); |
| 54 | + txt += `var ${tmp}=${inner};`; |
| 55 | + } else if (action === 'each') { |
| 56 | + num = inner.indexOf(' as '); |
| 57 | + stack.push(action); |
| 58 | + if (!~num) { |
| 59 | + txt += `for(var i=0,$$a=${inner};i<$$a.length;i++){`; |
| 60 | + } else { |
| 61 | + tmp = inner.substring(0, num).trim(); |
| 62 | + inner = inner.substring(num + 4).trim(); |
| 63 | + let [item, idx='i'] = inner.replace(/[()\s]/g, '').split(','); // (item, idx?) |
| 64 | + txt += `for(var ${idx}=0,${item},$$a=${tmp};${idx}<$$a.length;${idx}++){${item}=$$a[${idx}];`; |
| 65 | + } |
| 66 | + } else if (action === 'if') { |
| 67 | + txt += `if(${inner}){`; |
| 68 | + stack.push(action); |
| 69 | + } else if (action === 'elif') { |
| 70 | + txt += `}else if(${inner}){`; |
| 71 | + } else if (action === 'else') { |
| 72 | + txt += `}else{`; |
| 73 | + } else if (action in extra) { |
| 74 | + if (inner) { |
| 75 | + tmp = []; |
| 76 | + // parse arguments, `defer=true` -> `{ defer: true }` |
| 77 | + while (match = ARGS.exec(inner)) tmp.push(match[1] + ':' + match[2]); |
| 78 | + inner = tmp.length ? '{' + tmp.join() + '}' : ''; |
| 79 | + } |
| 80 | + inner = inner || '{}'; |
| 81 | + tmp = options.async ? 'await ' : ''; |
| 82 | + wip += '${' + tmp + '$$2.' + action + '(' + inner + ',$$2)}'; |
| 83 | + } else { |
| 84 | + throw new Error(`Unknown "${action}" block`); |
| 85 | + } |
| 86 | + } else if (char === '/') { |
| 87 | + action = inner.substring(1); |
| 88 | + inner = stack.pop(); |
| 89 | + close(); |
| 90 | + if (action === inner) txt += '}'; |
| 91 | + else throw new Error(`Expected to close "${inner}" block; closed "${action}" instead`); |
| 92 | + } else if (match[0].charAt(2) === '{') { |
| 93 | + wip += '${' + inner + '}'; // {{{ raw }}} |
| 94 | + } else { |
| 95 | + wip += '${$$1(' + inner + ')}'; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + if (stack.length > 0) { |
| 100 | + throw new Error(`Unterminated "${stack.pop()}" block`); |
| 101 | + } |
| 102 | + |
| 103 | + if (last < input.length) { |
| 104 | + wip += input.substring(last).replace(ENDLINES, ''); |
| 105 | + } |
| 106 | + |
| 107 | + close(); |
| 108 | + |
| 109 | + tmp = initials.size ? `{${ [...initials].join() }}=$$3,x` : ' x'; |
| 110 | + return `var${tmp + txt}return x`; |
| 111 | +} |
| 112 | + |
| 113 | +export function esc(value) { |
| 114 | + if (typeof value !== 'string') return value; |
| 115 | + let last=ESCAPE.lastIndex=0, tmp=0, out=''; |
| 116 | + while (ESCAPE.test(value)) { |
| 117 | + tmp = ESCAPE.lastIndex - 1; |
| 118 | + out += value.substring(last, tmp) + CHARS[value[tmp]]; |
| 119 | + last = tmp + 1; |
| 120 | + } |
| 121 | + return out + value.substring(last); |
| 122 | +} |
| 123 | + |
| 124 | +export function compile(input, options={}) { |
| 125 | + return new (options.async ? (async()=>{}).constructor : Function)( |
| 126 | + '$$1', '$$2', '$$3', gen(input, options) |
| 127 | + ).bind(0, options.escape || esc, options.blocks); |
| 128 | +} |
| 129 | + |
| 130 | +export function transform(input, options={}) { |
| 131 | + return ( |
| 132 | + options.format === 'cjs' |
| 133 | + ? 'var $$1=require("tempura").esc;module.exports=' |
| 134 | + : 'import{esc as $$1}from"tempura";export default ' |
| 135 | + ) + ( |
| 136 | + options.async ? 'async ' : '' |
| 137 | + ) + 'function($$3,$$2){'+gen(input, options)+'}'; |
| 138 | +} |
0 commit comments