@itrocks/template 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/template.d.ts +2 -1
- package/cjs/template.js +39 -33
- package/package.json +1 -1
package/cjs/template.d.ts
CHANGED
|
@@ -68,6 +68,7 @@ export declare class Template {
|
|
|
68
68
|
publicUrl: (path: string) => string;
|
|
69
69
|
urlAttributes: string[];
|
|
70
70
|
constructor(data?: any | undefined, containerData?: any | undefined);
|
|
71
|
+
private addDependency;
|
|
71
72
|
applyLiterals(text: string, parts?: string[]): string;
|
|
72
73
|
closeTag(shouldInLiteral: boolean, targetIndex: number): boolean;
|
|
73
74
|
combineLiterals(text: string, parts?: string[]): string;
|
|
@@ -114,7 +115,7 @@ export declare class Template {
|
|
|
114
115
|
parseBuffer(buffer: string): Promise<string>;
|
|
115
116
|
parseExpression(data: any, open: Open | '<', close: Close, finalClose?: Final): Promise<boolean | undefined>;
|
|
116
117
|
parseFile(fileName: string, containerFileName?: string | false): Promise<string>;
|
|
117
|
-
parsePath(expression: string, data: any): Promise<any>;
|
|
118
|
+
parsePath(expression: string, data: any, preserveIncludes?: boolean): Promise<any>;
|
|
118
119
|
parseVariable(variable: string, data: any): Promise<any>;
|
|
119
120
|
parseVars(): Promise<string>;
|
|
120
121
|
setSource(source: string, index?: number, start?: number, target?: string): void;
|
package/cjs/template.js
CHANGED
|
@@ -28,6 +28,12 @@ class HtmlResponse {
|
|
|
28
28
|
toString() { return this.html; }
|
|
29
29
|
}
|
|
30
30
|
exports.HtmlResponse = HtmlResponse;
|
|
31
|
+
class IncludedHtml {
|
|
32
|
+
html;
|
|
33
|
+
constructor(html) {
|
|
34
|
+
this.html = html;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
31
37
|
class Template {
|
|
32
38
|
data;
|
|
33
39
|
containerData;
|
|
@@ -89,6 +95,9 @@ class Template {
|
|
|
89
95
|
this.blockStack.push({ blockStart: 0, data: containerData, iteration: done });
|
|
90
96
|
}
|
|
91
97
|
}
|
|
98
|
+
addDependency(link) {
|
|
99
|
+
this.addLinks.push(link.replace(/(\s(?:href|src)\s*=\s*)(["'])(.*?)\2/g, (_match, attribute, quote, url) => attribute + quote + this.publicUrl(url) + quote));
|
|
100
|
+
}
|
|
92
101
|
applyLiterals(text, parts = []) {
|
|
93
102
|
return text.replace(/\$([0-9]+)/g, (_, index) => parts[+index]);
|
|
94
103
|
}
|
|
@@ -141,7 +150,7 @@ class Template {
|
|
|
141
150
|
}
|
|
142
151
|
if (DEBUG)
|
|
143
152
|
console.log('addLink(', dependency, ')');
|
|
144
|
-
this.
|
|
153
|
+
this.addDependency(dependency);
|
|
145
154
|
continue;
|
|
146
155
|
}
|
|
147
156
|
dependency = (0, node_path_1.normalize)(dependency).slice(app_dir_1.appDir.length);
|
|
@@ -149,13 +158,13 @@ class Template {
|
|
|
149
158
|
case 'css':
|
|
150
159
|
if (DEBUG)
|
|
151
160
|
console.log('addLink(', '<link href="' + dependency + '" rel="stylesheet">', ')');
|
|
152
|
-
this.
|
|
161
|
+
this.addDependency('<link href="' + dependency + '" rel="stylesheet">');
|
|
153
162
|
continue;
|
|
154
163
|
case 'js':
|
|
155
164
|
exports.frontScripts.push(dependency);
|
|
156
165
|
if (DEBUG)
|
|
157
166
|
console.log('addLink(', '<script src="' + dependency + '" type="module"></script>', ')');
|
|
158
|
-
this.
|
|
167
|
+
this.addDependency('<script src="' + dependency + '" type="module"></script>');
|
|
159
168
|
continue;
|
|
160
169
|
}
|
|
161
170
|
}
|
|
@@ -289,29 +298,15 @@ class Template {
|
|
|
289
298
|
let addLink;
|
|
290
299
|
const addLinks = new Array;
|
|
291
300
|
while (addLink = this.addLinks.shift()) {
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
const stop = addLink.indexOf(quote, start);
|
|
302
|
-
const link = this.publicUrl(addLink.substring(start, stop));
|
|
303
|
-
addLink = addLink.slice(0, start) + link + addLink.slice(stop);
|
|
304
|
-
if (DEBUG)
|
|
305
|
-
console.log('check(', link, ')');
|
|
306
|
-
if (!this.doneLinks.includes(link)) {
|
|
307
|
-
if (DEBUG)
|
|
308
|
-
console.log('+ addLink(', addLink, ')');
|
|
309
|
-
if (DEBUG)
|
|
310
|
-
console.log('+ doneLink(', link, ')');
|
|
311
|
-
addLinks.push(addLink);
|
|
312
|
-
this.doneLinks.push(link);
|
|
313
|
-
}
|
|
314
|
-
}
|
|
301
|
+
const link = addLink.match(/\s(?:href|src)\s*=\s*(["'])(.*?)\1/)?.[2];
|
|
302
|
+
if (link === undefined || this.doneLinks.includes(link))
|
|
303
|
+
continue;
|
|
304
|
+
if (DEBUG)
|
|
305
|
+
console.log('+ addLink(', addLink, ')');
|
|
306
|
+
if (DEBUG)
|
|
307
|
+
console.log('+ doneLink(', link, ')');
|
|
308
|
+
addLinks.push(addLink);
|
|
309
|
+
this.doneLinks.push(link);
|
|
315
310
|
}
|
|
316
311
|
if (addLinks.length) {
|
|
317
312
|
const position = this.target.lastIndexOf('>', this.target.indexOf('</head>')) + 1;
|
|
@@ -362,9 +357,12 @@ class Template {
|
|
|
362
357
|
}
|
|
363
358
|
const expression = this.target + this.source.substring(this.start, this.index - minus);
|
|
364
359
|
const lastTarget = this.targetStack.pop();
|
|
365
|
-
let parsed = await this.parsePath(expression, data);
|
|
360
|
+
let parsed = await this.parsePath(expression, data, true);
|
|
366
361
|
const html = (parsed instanceof HtmlResponse) ? parsed.html : parsed;
|
|
367
|
-
if (
|
|
362
|
+
if (parsed instanceof IncludedHtml) {
|
|
363
|
+
parsed = parsed.html;
|
|
364
|
+
}
|
|
365
|
+
else if ((typeof html === 'string') && /<[a-z]/i.test(html) && (this.publicUrl('/') !== '/')) {
|
|
368
366
|
const template = new Template({});
|
|
369
367
|
template.applicationPath = this.applicationPath;
|
|
370
368
|
template.doExpression = false;
|
|
@@ -433,7 +431,9 @@ class Template {
|
|
|
433
431
|
console.log('----- parseFile', containerFileName ? 'contained' : 'fetched', this.included ? 'included' : 'final', fileName);
|
|
434
432
|
if (containerFileName) {
|
|
435
433
|
const data = this.data;
|
|
436
|
-
this.data = Object.assign({
|
|
434
|
+
this.data = Object.assign({
|
|
435
|
+
content: async () => new IncludedHtml(await this.include(fileName, data))
|
|
436
|
+
}, this.blockStack[0]?.data);
|
|
437
437
|
return this.parseFile((0, node_path_1.normalize)(containerFileName));
|
|
438
438
|
}
|
|
439
439
|
this.fileName = fileName.substring(fileName.lastIndexOf(node_path_2.sep) + 1);
|
|
@@ -447,7 +447,7 @@ class Template {
|
|
|
447
447
|
}
|
|
448
448
|
return target;
|
|
449
449
|
}
|
|
450
|
-
async parsePath(expression, data) {
|
|
450
|
+
async parsePath(expression, data, preserveIncludes = false) {
|
|
451
451
|
if (DEBUG)
|
|
452
452
|
console.log('parsePath', expression);
|
|
453
453
|
if (expression === '') {
|
|
@@ -455,6 +455,12 @@ class Template {
|
|
|
455
455
|
}
|
|
456
456
|
if (((expression[0] === '.') && ((expression[1] === '/') || ((expression[1] === '.') && (expression[2] === '/'))))
|
|
457
457
|
|| (expression[0] === '/')) {
|
|
458
|
+
const include = async (path, content) => {
|
|
459
|
+
const html = await this.include(this.includePath(path), content);
|
|
460
|
+
return preserveIncludes
|
|
461
|
+
? new IncludedHtml(html)
|
|
462
|
+
: html;
|
|
463
|
+
};
|
|
458
464
|
let expressionEnd = expression.length - 1;
|
|
459
465
|
if (expression[expressionEnd] === '-') {
|
|
460
466
|
let blockBack = 1;
|
|
@@ -464,13 +470,13 @@ class Template {
|
|
|
464
470
|
expressionEnd--;
|
|
465
471
|
}
|
|
466
472
|
const blockStack = this.blockStack;
|
|
467
|
-
return
|
|
473
|
+
return include(expression.slice(0, expressionEnd), blockStack[blockStack.length - blockBack].data);
|
|
468
474
|
}
|
|
469
475
|
if (expression[expressionEnd] === ')') {
|
|
470
476
|
const openPosition = expression.lastIndexOf('(');
|
|
471
|
-
return
|
|
477
|
+
return include(expression.slice(0, openPosition), await this.parsePath(expression.slice(openPosition + 1, expression.length - 1), data));
|
|
472
478
|
}
|
|
473
|
-
return
|
|
479
|
+
return include(expression, data);
|
|
474
480
|
}
|
|
475
481
|
let onlyDots = true;
|
|
476
482
|
for (const c of expression) {
|
package/package.json
CHANGED