@itrocks/template 0.2.4 → 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/README.md +12 -0
- package/cjs/template.d.ts +5 -1
- package/cjs/template.js +76 -42
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -110,6 +110,18 @@ You can write your pages in your primary language and apply translations automat
|
|
|
110
110
|
|
|
111
111
|
## Syntax and Detailed Features
|
|
112
112
|
|
|
113
|
+
### Public URLs
|
|
114
|
+
|
|
115
|
+
`template.publicUrl` is an optional `(path: string) => string` callback, defaulting to the identity function.
|
|
116
|
+
It converts rendered URL attribute values, including evaluated expressions, included templates, generated controls
|
|
117
|
+
and embedded asset dependencies. Script discovery retains application-relative paths.
|
|
118
|
+
`template.applicationPath` provides the inverse conversion for recognising already prefixed asset URLs.
|
|
119
|
+
|
|
120
|
+
`template.urlAttributes` lists the attributes using that callback: `action`, `formaction`, `href`, `location`, `poster`
|
|
121
|
+
and `src`. Integrations can append their own URL-valued data attributes. Both settings are inherited by includes.
|
|
122
|
+
|
|
123
|
+
The it.rocks framework binds these settings from the current request automatically.
|
|
124
|
+
|
|
113
125
|
### Delimiters Outside of HTML element tags
|
|
114
126
|
|
|
115
127
|
**Simple Expressions**: Use braces `{expression}`.
|
package/cjs/template.d.ts
CHANGED
|
@@ -28,6 +28,7 @@ export declare class HtmlResponse {
|
|
|
28
28
|
export declare class Template {
|
|
29
29
|
data?: any | undefined;
|
|
30
30
|
containerData?: any | undefined;
|
|
31
|
+
applicationPath: (path: string) => string;
|
|
31
32
|
blockBack: number;
|
|
32
33
|
blockStack: BlockStackEntry[];
|
|
33
34
|
doExpression: boolean;
|
|
@@ -64,7 +65,10 @@ export declare class Template {
|
|
|
64
65
|
onTagClose?: (name: string) => void;
|
|
65
66
|
parsers: VariableParser[];
|
|
66
67
|
prefixes: string;
|
|
68
|
+
publicUrl: (path: string) => string;
|
|
69
|
+
urlAttributes: string[];
|
|
67
70
|
constructor(data?: any | undefined, containerData?: any | undefined);
|
|
71
|
+
private addDependency;
|
|
68
72
|
applyLiterals(text: string, parts?: string[]): string;
|
|
69
73
|
closeTag(shouldInLiteral: boolean, targetIndex: number): boolean;
|
|
70
74
|
combineLiterals(text: string, parts?: string[]): string;
|
|
@@ -111,7 +115,7 @@ export declare class Template {
|
|
|
111
115
|
parseBuffer(buffer: string): Promise<string>;
|
|
112
116
|
parseExpression(data: any, open: Open | '<', close: Close, finalClose?: Final): Promise<boolean | undefined>;
|
|
113
117
|
parseFile(fileName: string, containerFileName?: string | false): Promise<string>;
|
|
114
|
-
parsePath(expression: string, data: any): Promise<any>;
|
|
118
|
+
parsePath(expression: string, data: any, preserveIncludes?: boolean): Promise<any>;
|
|
115
119
|
parseVariable(variable: string, data: any): Promise<any>;
|
|
116
120
|
parseVars(): Promise<string>;
|
|
117
121
|
setSource(source: string, index?: number, start?: number, target?: string): void;
|
package/cjs/template.js
CHANGED
|
@@ -28,13 +28,21 @@ 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;
|
|
34
|
-
//
|
|
40
|
+
// Application path
|
|
41
|
+
applicationPath = (path) => path;
|
|
42
|
+
// Block stack
|
|
35
43
|
blockBack = 0;
|
|
36
44
|
blockStack = [];
|
|
37
|
-
//
|
|
45
|
+
// Parser
|
|
38
46
|
doExpression = true;
|
|
39
47
|
index = 0;
|
|
40
48
|
length = 0;
|
|
@@ -45,17 +53,17 @@ class Template {
|
|
|
45
53
|
target = '';
|
|
46
54
|
targetReplace = '';
|
|
47
55
|
targetStack = [];
|
|
48
|
-
//
|
|
56
|
+
// Literal
|
|
49
57
|
doLiteral = false;
|
|
50
58
|
inLiteral = false;
|
|
51
59
|
literalParts = [];
|
|
52
60
|
literalPartStack = [];
|
|
53
61
|
lockLiteral = false;
|
|
54
|
-
//
|
|
62
|
+
// HTML head
|
|
55
63
|
addLinks = new sorted_array_1.SortedArray();
|
|
56
64
|
doneLinks = new sorted_array_1.SortedArray();
|
|
57
65
|
headTitle;
|
|
58
|
-
//
|
|
66
|
+
// File
|
|
59
67
|
fileName;
|
|
60
68
|
filePath;
|
|
61
69
|
included = false;
|
|
@@ -75,6 +83,9 @@ class Template {
|
|
|
75
83
|
// Additional parsers
|
|
76
84
|
parsers = [];
|
|
77
85
|
prefixes = '';
|
|
86
|
+
// URL management
|
|
87
|
+
publicUrl = (path) => path;
|
|
88
|
+
urlAttributes = ['action', 'formaction', 'href', 'location', 'poster', 'src'];
|
|
78
89
|
constructor(data, containerData) {
|
|
79
90
|
this.data = data;
|
|
80
91
|
this.containerData = containerData;
|
|
@@ -84,6 +95,9 @@ class Template {
|
|
|
84
95
|
this.blockStack.push({ blockStart: 0, data: containerData, iteration: done });
|
|
85
96
|
}
|
|
86
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
|
+
}
|
|
87
101
|
applyLiterals(text, parts = []) {
|
|
88
102
|
return text.replace(/\$([0-9]+)/g, (_, index) => parts[+index]);
|
|
89
103
|
}
|
|
@@ -132,11 +146,11 @@ class Template {
|
|
|
132
146
|
if (dependency[0] === '<') {
|
|
133
147
|
const script = dependency.match(/<script[^>]*\bsrc=["']([^"']+)["']/i)?.[1];
|
|
134
148
|
if (script) {
|
|
135
|
-
exports.frontScripts.push(script);
|
|
149
|
+
exports.frontScripts.push(this.applicationPath(script));
|
|
136
150
|
}
|
|
137
151
|
if (DEBUG)
|
|
138
152
|
console.log('addLink(', dependency, ')');
|
|
139
|
-
this.
|
|
153
|
+
this.addDependency(dependency);
|
|
140
154
|
continue;
|
|
141
155
|
}
|
|
142
156
|
dependency = (0, node_path_1.normalize)(dependency).slice(app_dir_1.appDir.length);
|
|
@@ -144,13 +158,13 @@ class Template {
|
|
|
144
158
|
case 'css':
|
|
145
159
|
if (DEBUG)
|
|
146
160
|
console.log('addLink(', '<link href="' + dependency + '" rel="stylesheet">', ')');
|
|
147
|
-
this.
|
|
161
|
+
this.addDependency('<link href="' + dependency + '" rel="stylesheet">');
|
|
148
162
|
continue;
|
|
149
163
|
case 'js':
|
|
150
164
|
exports.frontScripts.push(dependency);
|
|
151
165
|
if (DEBUG)
|
|
152
166
|
console.log('addLink(', '<script src="' + dependency + '" type="module"></script>', ')');
|
|
153
|
-
this.
|
|
167
|
+
this.addDependency('<script src="' + dependency + '" type="module"></script>');
|
|
154
168
|
continue;
|
|
155
169
|
}
|
|
156
170
|
}
|
|
@@ -197,6 +211,7 @@ class Template {
|
|
|
197
211
|
async include(path, data) {
|
|
198
212
|
const template = new (Object.getPrototypeOf(this).constructor)(data, this.blockStack[0]?.data);
|
|
199
213
|
template.addLinks = this.addLinks;
|
|
214
|
+
template.applicationPath = this.applicationPath;
|
|
200
215
|
template.doExpression = this.doExpression;
|
|
201
216
|
template.doLiteral = this.doLiteral;
|
|
202
217
|
template.doneLinks = this.doneLinks;
|
|
@@ -206,6 +221,8 @@ class Template {
|
|
|
206
221
|
template.onTagOpen = this.onTagOpen;
|
|
207
222
|
template.onTagOpened = this.onTagOpened;
|
|
208
223
|
template.parsers = this.parsers;
|
|
224
|
+
template.publicUrl = this.publicUrl;
|
|
225
|
+
template.urlAttributes = this.urlAttributes;
|
|
209
226
|
const parsed = await template.parseFile(((path[0] === node_path_2.sep) || (path[1] === ':'))
|
|
210
227
|
? path
|
|
211
228
|
: (this.filePath + node_path_2.sep + path));
|
|
@@ -262,8 +279,9 @@ class Template {
|
|
|
262
279
|
this.start = index;
|
|
263
280
|
}
|
|
264
281
|
normalizeLink(link) {
|
|
265
|
-
if (link[0] === '/')
|
|
266
|
-
return link;
|
|
282
|
+
if ((link[0] === '/') || /^[a-z][a-z\d+.-]*:/i.test(link)) {
|
|
283
|
+
return this.applicationPath(link);
|
|
284
|
+
}
|
|
267
285
|
const result = (0, node_path_1.normalize)(this.filePath + node_path_2.sep + link).substring(app_dir_1.appDir.length);
|
|
268
286
|
return (((node_path_2.sep === '/') ? result : result.replaceAll(node_path_2.sep, '/')))
|
|
269
287
|
.replace(/^\/node_modules\/@itrocks\//, '/@itrocks/')
|
|
@@ -280,28 +298,15 @@ class Template {
|
|
|
280
298
|
let addLink;
|
|
281
299
|
const addLinks = new Array;
|
|
282
300
|
while (addLink = this.addLinks.shift()) {
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
const stop = addLink.indexOf(quote, start);
|
|
293
|
-
const link = addLink.substring(start, stop);
|
|
294
|
-
if (DEBUG)
|
|
295
|
-
console.log('check(', link, ')');
|
|
296
|
-
if (!this.doneLinks.includes(link)) {
|
|
297
|
-
if (DEBUG)
|
|
298
|
-
console.log('+ addLink(', addLink, ')');
|
|
299
|
-
if (DEBUG)
|
|
300
|
-
console.log('+ doneLink(', link, ')');
|
|
301
|
-
addLinks.push(addLink);
|
|
302
|
-
this.doneLinks.push(link);
|
|
303
|
-
}
|
|
304
|
-
}
|
|
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);
|
|
305
310
|
}
|
|
306
311
|
if (addLinks.length) {
|
|
307
312
|
const position = this.target.lastIndexOf('>', this.target.indexOf('</head>')) + 1;
|
|
@@ -352,7 +357,22 @@ class Template {
|
|
|
352
357
|
}
|
|
353
358
|
const expression = this.target + this.source.substring(this.start, this.index - minus);
|
|
354
359
|
const lastTarget = this.targetStack.pop();
|
|
355
|
-
|
|
360
|
+
let parsed = await this.parsePath(expression, data, true);
|
|
361
|
+
const html = (parsed instanceof HtmlResponse) ? parsed.html : parsed;
|
|
362
|
+
if (parsed instanceof IncludedHtml) {
|
|
363
|
+
parsed = parsed.html;
|
|
364
|
+
}
|
|
365
|
+
else if ((typeof html === 'string') && /<[a-z]/i.test(html) && (this.publicUrl('/') !== '/')) {
|
|
366
|
+
const template = new Template({});
|
|
367
|
+
template.applicationPath = this.applicationPath;
|
|
368
|
+
template.doExpression = false;
|
|
369
|
+
template.filePath = this.filePath;
|
|
370
|
+
template.publicUrl = this.publicUrl;
|
|
371
|
+
template.urlAttributes = this.urlAttributes;
|
|
372
|
+
const prefix = '<html><head></head><body>';
|
|
373
|
+
const suffix = '</body></html>';
|
|
374
|
+
parsed = (await template.parseBuffer(prefix + html + suffix)).slice(prefix.length, -suffix.length);
|
|
375
|
+
}
|
|
356
376
|
this.index += (char === close) ? 1 : finalClose.length;
|
|
357
377
|
this.start = this.index;
|
|
358
378
|
this.target = '';
|
|
@@ -411,7 +431,9 @@ class Template {
|
|
|
411
431
|
console.log('----- parseFile', containerFileName ? 'contained' : 'fetched', this.included ? 'included' : 'final', fileName);
|
|
412
432
|
if (containerFileName) {
|
|
413
433
|
const data = this.data;
|
|
414
|
-
this.data = Object.assign({
|
|
434
|
+
this.data = Object.assign({
|
|
435
|
+
content: async () => new IncludedHtml(await this.include(fileName, data))
|
|
436
|
+
}, this.blockStack[0]?.data);
|
|
415
437
|
return this.parseFile((0, node_path_1.normalize)(containerFileName));
|
|
416
438
|
}
|
|
417
439
|
this.fileName = fileName.substring(fileName.lastIndexOf(node_path_2.sep) + 1);
|
|
@@ -425,7 +447,7 @@ class Template {
|
|
|
425
447
|
}
|
|
426
448
|
return target;
|
|
427
449
|
}
|
|
428
|
-
async parsePath(expression, data) {
|
|
450
|
+
async parsePath(expression, data, preserveIncludes = false) {
|
|
429
451
|
if (DEBUG)
|
|
430
452
|
console.log('parsePath', expression);
|
|
431
453
|
if (expression === '') {
|
|
@@ -433,6 +455,12 @@ class Template {
|
|
|
433
455
|
}
|
|
434
456
|
if (((expression[0] === '.') && ((expression[1] === '/') || ((expression[1] === '.') && (expression[2] === '/'))))
|
|
435
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
|
+
};
|
|
436
464
|
let expressionEnd = expression.length - 1;
|
|
437
465
|
if (expression[expressionEnd] === '-') {
|
|
438
466
|
let blockBack = 1;
|
|
@@ -442,13 +470,13 @@ class Template {
|
|
|
442
470
|
expressionEnd--;
|
|
443
471
|
}
|
|
444
472
|
const blockStack = this.blockStack;
|
|
445
|
-
return
|
|
473
|
+
return include(expression.slice(0, expressionEnd), blockStack[blockStack.length - blockBack].data);
|
|
446
474
|
}
|
|
447
475
|
if (expression[expressionEnd] === ')') {
|
|
448
476
|
const openPosition = expression.lastIndexOf('(');
|
|
449
|
-
return
|
|
477
|
+
return include(expression.slice(0, openPosition), await this.parsePath(expression.slice(openPosition + 1, expression.length - 1), data));
|
|
450
478
|
}
|
|
451
|
-
return
|
|
479
|
+
return include(expression, data);
|
|
452
480
|
}
|
|
453
481
|
let onlyDots = true;
|
|
454
482
|
for (const c of expression) {
|
|
@@ -739,9 +767,11 @@ class Template {
|
|
|
739
767
|
}
|
|
740
768
|
const inLinkHRef = inLink && (attributeChar === 'h') && (attributeName === 'href');
|
|
741
769
|
const inScriptSrc = inScript && (attributeChar === 's') && (attributeName === 'src');
|
|
742
|
-
|
|
770
|
+
const inUrl = this.urlAttributes.includes(attributeName);
|
|
771
|
+
if ((inUrl || this.inLiteral) && (this.index > this.start)) {
|
|
743
772
|
this.sourceToTarget();
|
|
744
773
|
}
|
|
774
|
+
const urlStart = this.target.length;
|
|
745
775
|
const valuePosition = this.index;
|
|
746
776
|
const shortQuote = !(quote.length - 1);
|
|
747
777
|
if (shortQuote && (attributeBlock !== undefined)) {
|
|
@@ -768,7 +798,7 @@ class Template {
|
|
|
768
798
|
if (!(inHead && this.included)) {
|
|
769
799
|
if (DEBUG)
|
|
770
800
|
console.log('doneLink(', frontStyle, ')');
|
|
771
|
-
this.doneLinks.push(frontStyle);
|
|
801
|
+
this.doneLinks.push(this.publicUrl(frontStyle));
|
|
772
802
|
}
|
|
773
803
|
}
|
|
774
804
|
if (inScriptSrc && attributeValue.endsWith('.js')) {
|
|
@@ -779,9 +809,13 @@ class Template {
|
|
|
779
809
|
if (!(inHead && this.included)) {
|
|
780
810
|
if (DEBUG)
|
|
781
811
|
console.log('doneLink(', frontScript, ')');
|
|
782
|
-
this.doneLinks.push(frontScript);
|
|
812
|
+
this.doneLinks.push(this.publicUrl(frontScript));
|
|
783
813
|
}
|
|
784
814
|
}
|
|
815
|
+
if (inUrl) {
|
|
816
|
+
this.sourceToTarget();
|
|
817
|
+
this.target = this.target.slice(0, urlStart) + this.publicUrl(this.target.slice(urlStart));
|
|
818
|
+
}
|
|
785
819
|
if (this.onAttribute)
|
|
786
820
|
this.onAttribute(attributeName, attributeValue);
|
|
787
821
|
if (char !== '>')
|
package/package.json
CHANGED