forked from patrickpissurno/li-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lit.js
214 lines (182 loc) · 7.11 KB
/
lit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* li-template was developed by Patrick Pissurno
* and is licensed under the MIT license.
*/
const crypto = require('crypto');
const util = require('util');
const fs = require('fs');
const path = require('path');
const randomBytes = util.promisify(crypto.randomBytes);
const readFile = util.promisify(fs.readFile);
async function rand(){
return (await randomBytes(48)).toString('hex');
}
// Used to find the matching } of a given {
function getClosing(code, sI){
let limit = 10000;
let index = sI;
let nest = 0;
while(index < code.length)
{
let open = code.indexOf('{', index);
let close = code.indexOf('}', index);
if(close == -1)
return index;
else if(open == -1)
open = close + 1;
index = open < close ? open : close;
nest += open < close ? 1 : -1;
index ++;
if(nest <= 0)
return index;
limit -= 1;
if(limit < 0)
return -9;
}
return index;
}
// Transpiles the short-hand syntax-sugar into plain ES6 template literals
function transpile(str){
let patt = RegExp(/\$\(/);
let index = 0;
let lastMatchIndex = 0;
let result = '';
while (match = patt.exec(str.substring(index, str.length))) {
let start = index + match.index + 2;
let end = str.indexOf(')', start);
if(str.substring(end - 1, end) === '?') //if and else syntax
{
result += str.substring(lastMatchIndex + 1, start - 2);
let variable = str.substring(start, end - 1);
let blockStart = str.indexOf('{', end) + 1;
let blockEnd = getClosing(str, blockStart - 1);
let block = str.substring(blockStart, blockEnd - 1);
lastMatchIndex = blockEnd - 1;
index = blockEnd + 1;
let inner = transpile(' ' + block + ' ');
result += `\${${ variable + (variable.indexOf('!') !== -1 ? ' || ' + variable.replace('!', '') + '.length == 0' : '') } ? \`${ inner == '' ? block : inner }\` : ''}`;
}
else if(str.substring(start, end).indexOf(':') !== -1) //loops syntax
{
result += str.substring(lastMatchIndex + 1, start - 2);
let arr = str.substring(start, end).split(':').map(x => x.trim());
let blockStart = str.indexOf('{', end) + 1;
let blockEnd = getClosing(str, blockStart - 1);
let block = str.substring(blockStart, blockEnd - 1);
lastMatchIndex = blockEnd - 1;
index = blockEnd + 1;
let inner = transpile(' ' + block + ' ');
result += `\${${ arr[0] } ? \`\${${arr[0]}.map(${arr[1]} => \`${ inner == '' ? block : inner }\`).join('')}\` : ''}`;
}
else //no pattern found, skip
{
index += 1;
continue;
}
}
result += str.substring(index - 1, str.length + (lastMatchIndex == 0 ? -1 : 0));
return result.trim();
};
class Lit {
/**
* Pre-compiles a template in order to use the view engine by passing its raw content
* @param {string} src Template source
* @returns The pre-compiled render module that is used to render the template as a string
* @memberof Lit
*/
precompile(src){
return `module.exports = function(){ return \`${
transpile(` ${
src.replace(/\\\$\(/g, '\\$@(')
} `).replace(/\\\$@\(/g, '\\$(')}
\`; };`;
}
/**
* Compile a template in order to use the view engine by passing its raw content as string
* @param {string} raw Template raw content as string
* @param {Object[]} partials Array of already compiled partial files
* @param {string} [filename] A string name that will be used as Node compiled module name. If omitted, random string is used
* @returns The compiled render function that is used to render the template
* @memberof Lit
*/
async compile(raw, partials, opt = { filename: null, precompiled: null }){
const m = new module.constructor();
m.paths = module.paths;
let template = `
const htmlEscapes = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
'/': '/'
};
const htmlEscaper = /[&<>"'\/]/g;
function safe(str) {
return ('' + str).replace(htmlEscaper, function(match) {
return htmlEscapes[match];
});
};
${opt.precompiled == null ? this.precompile(raw) : opt.precompiled}`;
try
{
m._compile(template, opt.filename == null ? await rand() : opt.filename);
}
catch(ex)
{
console.error(ex);
}
return (data) => {
if(partials != null)
{
let temp = {};
partials.forEach(x => temp[x.name] = x.render(data));
data.partials = temp;
}
return m.exports.bind(data)();
};
}
/**
* Compile a template in order to use the view engine by passing its file name
* @param {string} filename Template file name
* @param {string[]} partials An array containing the file names of all partials needed for this template
* @returns The compiled render function that is used to render the template
* @memberof Lit
*/
async compileFile(filename, partials){
if(partials != null)
partials = await this.compilePartials(partials, filename);
return await this.compile((await readFile(filename)).toString(), partials, filename);
}
/**
* Compile partials from a string array of file names
* @param {string[]} partials An array containing the file names of all partials needed for this template
* @param {string} [filename] Base template file name
* @returns An array containing the partials as objects
* @memberof Lit
*/
async compilePartials(partials, filename){
if(partials == null)
return null;
return await Promise.all(partials.map(async x => {
return typeof(x) == 'string' ? {
render: await this.compile((await readFile(x)).toString(), null, path.parse(x).name + '_' + (filename == null ? await rand() : filename)),
name: path.parse(x).name
} :
{
render: await this.compile((await readFile(x.file)).toString(), await this.compilePartials(x.partials, x.file), path.parse(x.file).name + '_' + (filename == null ? await rand() : filename)),
name: path.parse(x.file).name
};
}));
}
/**
* Pre-compiles a template in order to use the view engine by passing its file name
* @param {string} filename Template file name
* @returns The pre-compiled render function that is used to render the template as a string
* @memberof Lit
*/
async precompileFile(filename){
return this.precompile((await readFile(filename)).toString());
}
};
module.exports = new Lit();