@itrocks/template 0.0.40 → 0.0.42
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 +5 -0
- package/cjs/template.js +22 -14
- package/esm/template.d.ts +5 -0
- package/esm/template.js +10 -3
- package/package.json +1 -1
package/cjs/template.d.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import { SortedArray } from '@itrocks/sorted-array';
|
|
2
|
+
export type Dependencies = {
|
|
3
|
+
toString: (value: any) => Promise<string>;
|
|
4
|
+
};
|
|
5
|
+
export declare const depends: Dependencies;
|
|
2
6
|
type BlockStackEntry = {
|
|
3
7
|
blockStart: number;
|
|
4
8
|
condition?: boolean;
|
|
@@ -14,6 +18,7 @@ type Final = '' | '-->';
|
|
|
14
18
|
type Open = '(' | '{';
|
|
15
19
|
export type VariableParser = [parser: string, (variable: string, data: any) => any];
|
|
16
20
|
export declare const frontScripts: SortedArray<string>;
|
|
21
|
+
export declare function templateDependsOn(dependencies: Partial<Dependencies>): void;
|
|
17
22
|
export declare class Template {
|
|
18
23
|
data?: any;
|
|
19
24
|
containerData?: any;
|
package/cjs/template.js
CHANGED
|
@@ -1,14 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Template = exports.frontScripts = void 0;
|
|
3
|
+
exports.Template = exports.frontScripts = exports.depends = void 0;
|
|
4
|
+
exports.templateDependsOn = templateDependsOn;
|
|
4
5
|
const app_dir_1 = require("@itrocks/app-dir");
|
|
5
6
|
const rename_1 = require("@itrocks/rename");
|
|
6
7
|
const sorted_array_1 = require("@itrocks/sorted-array");
|
|
7
8
|
const promises_1 = require("node:fs/promises");
|
|
8
9
|
const node_path_1 = require("node:path");
|
|
10
|
+
const node_path_2 = require("node:path");
|
|
11
|
+
exports.depends = {
|
|
12
|
+
toString: async (value) => '' + value
|
|
13
|
+
};
|
|
9
14
|
const done = { done: true };
|
|
10
15
|
exports.frontScripts = new sorted_array_1.SortedArray();
|
|
11
16
|
exports.frontScripts.distinct = true;
|
|
17
|
+
function templateDependsOn(dependencies) {
|
|
18
|
+
Object.assign(exports.depends, dependencies);
|
|
19
|
+
}
|
|
12
20
|
class Template {
|
|
13
21
|
data;
|
|
14
22
|
containerData;
|
|
@@ -166,9 +174,9 @@ class Template {
|
|
|
166
174
|
template.onTagOpen = this.onTagOpen;
|
|
167
175
|
template.onTagOpened = this.onTagOpened;
|
|
168
176
|
template.parsers = this.parsers;
|
|
169
|
-
const parsed = await template.parseFile(((path[0] ===
|
|
177
|
+
const parsed = await template.parseFile(((path[0] === node_path_2.sep) || (path[1] === ':'))
|
|
170
178
|
? path
|
|
171
|
-
: (this.filePath +
|
|
179
|
+
: (this.filePath + node_path_2.sep + path));
|
|
172
180
|
if (!this.doHeadLinks) {
|
|
173
181
|
this.addLinks.push(...template.headLinks);
|
|
174
182
|
this.headTitle = template.headTitle;
|
|
@@ -342,8 +350,8 @@ class Template {
|
|
|
342
350
|
this.data = Object.assign({ content: () => this.include(fileName, data) }, this.blockStack[0]?.data);
|
|
343
351
|
return this.parseFile((0, node_path_1.normalize)(containerFileName));
|
|
344
352
|
}
|
|
345
|
-
this.fileName = fileName.substring(fileName.lastIndexOf(
|
|
346
|
-
this.filePath = fileName.substring(0, fileName.lastIndexOf(
|
|
353
|
+
this.fileName = fileName.substring(fileName.lastIndexOf(node_path_2.sep) + 1);
|
|
354
|
+
this.filePath = fileName.substring(0, fileName.lastIndexOf(node_path_2.sep));
|
|
347
355
|
return this.parseBuffer(await (0, promises_1.readFile)(fileName, 'utf-8'));
|
|
348
356
|
}
|
|
349
357
|
async parsePath(expression, data) {
|
|
@@ -412,11 +420,11 @@ class Template {
|
|
|
412
420
|
}
|
|
413
421
|
for (const [prefix, callback] of this.parsers) {
|
|
414
422
|
if (firstChar === prefix) {
|
|
415
|
-
return callback(variable, data);
|
|
423
|
+
return await callback(variable, data);
|
|
416
424
|
}
|
|
417
425
|
}
|
|
418
426
|
if (data[variable] === undefined) {
|
|
419
|
-
data = new rename_1.Str(data);
|
|
427
|
+
data = new rename_1.Str(await exports.depends.toString(data));
|
|
420
428
|
}
|
|
421
429
|
let value = data[variable];
|
|
422
430
|
return (((typeof value)[0] === 'f') && ((value + '')[0] !== 'c'))
|
|
@@ -675,19 +683,19 @@ class Template {
|
|
|
675
683
|
this.literalTarget(this.index);
|
|
676
684
|
}
|
|
677
685
|
if (inLinkHRef && attributeValue.endsWith('.css')) {
|
|
678
|
-
let frontStyle = (0, node_path_1.normalize)(this.filePath +
|
|
686
|
+
let frontStyle = (0, node_path_1.normalize)(this.filePath + node_path_2.sep + this.source.substring(this.start, this.index))
|
|
679
687
|
.substring(app_dir_1.appDir.length);
|
|
680
|
-
if (
|
|
681
|
-
frontStyle = frontStyle.replaceAll(
|
|
688
|
+
if (node_path_2.sep !== '/') {
|
|
689
|
+
frontStyle = frontStyle.replaceAll(node_path_2.sep, '/');
|
|
682
690
|
}
|
|
683
691
|
this.target += frontStyle;
|
|
684
692
|
this.start = this.index;
|
|
685
693
|
}
|
|
686
694
|
if (inScriptSrc && attributeValue.endsWith('.js')) {
|
|
687
|
-
let frontScript = (0, node_path_1.normalize)(this.filePath +
|
|
695
|
+
let frontScript = (0, node_path_1.normalize)(this.filePath + node_path_2.sep + this.source.substring(this.start, this.index))
|
|
688
696
|
.substring(app_dir_1.appDir.length);
|
|
689
|
-
if (
|
|
690
|
-
frontScript = frontScript.replaceAll(
|
|
697
|
+
if (node_path_2.sep !== '/') {
|
|
698
|
+
frontScript = frontScript.replaceAll(node_path_2.sep, '/');
|
|
691
699
|
}
|
|
692
700
|
exports.frontScripts.insert(frontScript);
|
|
693
701
|
this.target += frontScript;
|
|
@@ -725,7 +733,7 @@ class Template {
|
|
|
725
733
|
this.index = this.source.indexOf('data-end', this.index) + 8;
|
|
726
734
|
if (this.index < 8) {
|
|
727
735
|
throw 'Missing data-end matching data-if at position ' + attributePosition
|
|
728
|
-
+ ' into template file ' + this.filePath +
|
|
736
|
+
+ ' into template file ' + this.filePath + node_path_2.sep + this.fileName;
|
|
729
737
|
}
|
|
730
738
|
}
|
|
731
739
|
this.start = this.index;
|
package/esm/template.d.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import { SortedArray } from '@itrocks/sorted-array';
|
|
2
|
+
export type Dependencies = {
|
|
3
|
+
toString: (value: any) => Promise<string>;
|
|
4
|
+
};
|
|
5
|
+
export declare const depends: Dependencies;
|
|
2
6
|
type BlockStackEntry = {
|
|
3
7
|
blockStart: number;
|
|
4
8
|
condition?: boolean;
|
|
@@ -14,6 +18,7 @@ type Final = '' | '-->';
|
|
|
14
18
|
type Open = '(' | '{';
|
|
15
19
|
export type VariableParser = [parser: string, (variable: string, data: any) => any];
|
|
16
20
|
export declare const frontScripts: SortedArray<string>;
|
|
21
|
+
export declare function templateDependsOn(dependencies: Partial<Dependencies>): void;
|
|
17
22
|
export declare class Template {
|
|
18
23
|
data?: any;
|
|
19
24
|
containerData?: any;
|
package/esm/template.js
CHANGED
|
@@ -2,10 +2,17 @@ import { appDir } from '@itrocks/app-dir';
|
|
|
2
2
|
import { Str } from '@itrocks/rename';
|
|
3
3
|
import { SortedArray } from '@itrocks/sorted-array';
|
|
4
4
|
import { readFile } from 'node:fs/promises';
|
|
5
|
-
import { normalize
|
|
5
|
+
import { normalize } from 'node:path';
|
|
6
|
+
import { sep } from 'node:path';
|
|
7
|
+
export const depends = {
|
|
8
|
+
toString: async (value) => '' + value
|
|
9
|
+
};
|
|
6
10
|
const done = { done: true };
|
|
7
11
|
export const frontScripts = new SortedArray();
|
|
8
12
|
frontScripts.distinct = true;
|
|
13
|
+
export function templateDependsOn(dependencies) {
|
|
14
|
+
Object.assign(depends, dependencies);
|
|
15
|
+
}
|
|
9
16
|
export class Template {
|
|
10
17
|
data;
|
|
11
18
|
containerData;
|
|
@@ -409,11 +416,11 @@ export class Template {
|
|
|
409
416
|
}
|
|
410
417
|
for (const [prefix, callback] of this.parsers) {
|
|
411
418
|
if (firstChar === prefix) {
|
|
412
|
-
return callback(variable, data);
|
|
419
|
+
return await callback(variable, data);
|
|
413
420
|
}
|
|
414
421
|
}
|
|
415
422
|
if (data[variable] === undefined) {
|
|
416
|
-
data = new Str(data);
|
|
423
|
+
data = new Str(await depends.toString(data));
|
|
417
424
|
}
|
|
418
425
|
let value = data[variable];
|
|
419
426
|
return (((typeof value)[0] === 'f') && ((value + '')[0] !== 'c'))
|
package/package.json
CHANGED