@itrocks/template 0.0.41 → 0.1.0

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 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,13 @@ 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;
22
+ export declare class HtmlResponse {
23
+ html: string;
24
+ dependencies: string[];
25
+ constructor(html: string, ...dependencies: string[]);
26
+ toString(): string;
27
+ }
17
28
  export declare class Template {
18
29
  data?: any;
19
30
  containerData?: any;
@@ -60,6 +71,7 @@ export declare class Template {
60
71
  closeTag(shouldInLiteral: boolean, targetIndex: number): boolean;
61
72
  combineLiterals(text: string, parts?: string[]): string;
62
73
  debugEvents(): void;
74
+ embedHtmlResponse(htmlResponse: HtmlResponse): void;
63
75
  getCleanContext(): {
64
76
  addLinks: SortedArray<string>;
65
77
  doHeadLinks: boolean;
package/cjs/template.js CHANGED
@@ -1,14 +1,32 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Template = exports.frontScripts = void 0;
3
+ exports.Template = exports.HtmlResponse = 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
+ }
20
+ class HtmlResponse {
21
+ html;
22
+ dependencies;
23
+ constructor(html, ...dependencies) {
24
+ this.html = html;
25
+ this.dependencies = dependencies;
26
+ }
27
+ toString() { return this.html; }
28
+ }
29
+ exports.HtmlResponse = HtmlResponse;
12
30
  class Template {
13
31
  data;
14
32
  containerData;
@@ -111,6 +129,28 @@ class Template {
111
129
  this.onTagOpened = (name) => console.log('tag.opened =', name);
112
130
  this.onTagClose = (name) => console.log('tag.closed =', name);
113
131
  }
132
+ embedHtmlResponse(htmlResponse) {
133
+ for (let dependency of htmlResponse.dependencies) {
134
+ if (dependency[0] === '<') {
135
+ const script = dependency.match(/<script[^>]*\bsrc=["']([^"']+)["']/i)?.[1];
136
+ if (script) {
137
+ exports.frontScripts.insert(script);
138
+ }
139
+ this.headLinks.insert(dependency);
140
+ continue;
141
+ }
142
+ dependency = (0, node_path_1.normalize)(dependency).slice(app_dir_1.appDir.length);
143
+ switch (dependency.slice(dependency.lastIndexOf('.') + 1)) {
144
+ case 'css':
145
+ this.headLinks.insert('<link href="' + dependency + '" rel="stylesheet">');
146
+ continue;
147
+ case 'js':
148
+ exports.frontScripts.insert(dependency);
149
+ this.headLinks.insert('<script src="' + dependency + '" type="module"></script>');
150
+ continue;
151
+ }
152
+ }
153
+ }
114
154
  getCleanContext() {
115
155
  const addLinks = new sorted_array_1.SortedArray;
116
156
  const doneLinks = new sorted_array_1.SortedArray;
@@ -166,9 +206,9 @@ class Template {
166
206
  template.onTagOpen = this.onTagOpen;
167
207
  template.onTagOpened = this.onTagOpened;
168
208
  template.parsers = this.parsers;
169
- const parsed = await template.parseFile(((path[0] === node_path_1.sep) || (path[1] === ':'))
209
+ const parsed = await template.parseFile(((path[0] === node_path_2.sep) || (path[1] === ':'))
170
210
  ? path
171
- : (this.filePath + node_path_1.sep + path));
211
+ : (this.filePath + node_path_2.sep + path));
172
212
  if (!this.doHeadLinks) {
173
213
  this.addLinks.push(...template.headLinks);
174
214
  this.headTitle = template.headTitle;
@@ -342,8 +382,8 @@ class Template {
342
382
  this.data = Object.assign({ content: () => this.include(fileName, data) }, this.blockStack[0]?.data);
343
383
  return this.parseFile((0, node_path_1.normalize)(containerFileName));
344
384
  }
345
- this.fileName = fileName.substring(fileName.lastIndexOf(node_path_1.sep) + 1);
346
- this.filePath = fileName.substring(0, fileName.lastIndexOf(node_path_1.sep));
385
+ this.fileName = fileName.substring(fileName.lastIndexOf(node_path_2.sep) + 1);
386
+ this.filePath = fileName.substring(0, fileName.lastIndexOf(node_path_2.sep));
347
387
  return this.parseBuffer(await (0, promises_1.readFile)(fileName, 'utf-8'));
348
388
  }
349
389
  async parsePath(expression, data) {
@@ -388,6 +428,9 @@ class Template {
388
428
  for (const variable of expression.split('.')) {
389
429
  data = await this.parseVariable(variable, data);
390
430
  }
431
+ if (data instanceof HtmlResponse) {
432
+ this.embedHtmlResponse(data);
433
+ }
391
434
  return data;
392
435
  }
393
436
  async parseVariable(variable, data) {
@@ -416,7 +459,7 @@ class Template {
416
459
  }
417
460
  }
418
461
  if (data[variable] === undefined) {
419
- data = new rename_1.Str(data);
462
+ data = new rename_1.Str(await exports.depends.toString(data));
420
463
  }
421
464
  let value = data[variable];
422
465
  return (((typeof value)[0] === 'f') && ((value + '')[0] !== 'c'))
@@ -675,19 +718,19 @@ class Template {
675
718
  this.literalTarget(this.index);
676
719
  }
677
720
  if (inLinkHRef && attributeValue.endsWith('.css')) {
678
- let frontStyle = (0, node_path_1.normalize)(this.filePath + node_path_1.sep + this.source.substring(this.start, this.index))
721
+ let frontStyle = (0, node_path_1.normalize)(this.filePath + node_path_2.sep + this.source.substring(this.start, this.index))
679
722
  .substring(app_dir_1.appDir.length);
680
- if (node_path_1.sep !== '/') {
681
- frontStyle = frontStyle.replaceAll(node_path_1.sep, '/');
723
+ if (node_path_2.sep !== '/') {
724
+ frontStyle = frontStyle.replaceAll(node_path_2.sep, '/');
682
725
  }
683
726
  this.target += frontStyle;
684
727
  this.start = this.index;
685
728
  }
686
729
  if (inScriptSrc && attributeValue.endsWith('.js')) {
687
- let frontScript = (0, node_path_1.normalize)(this.filePath + node_path_1.sep + this.source.substring(this.start, this.index))
730
+ let frontScript = (0, node_path_1.normalize)(this.filePath + node_path_2.sep + this.source.substring(this.start, this.index))
688
731
  .substring(app_dir_1.appDir.length);
689
- if (node_path_1.sep !== '/') {
690
- frontScript = frontScript.replaceAll(node_path_1.sep, '/');
732
+ if (node_path_2.sep !== '/') {
733
+ frontScript = frontScript.replaceAll(node_path_2.sep, '/');
691
734
  }
692
735
  exports.frontScripts.insert(frontScript);
693
736
  this.target += frontScript;
@@ -725,7 +768,7 @@ class Template {
725
768
  this.index = this.source.indexOf('data-end', this.index) + 8;
726
769
  if (this.index < 8) {
727
770
  throw 'Missing data-end matching data-if at position ' + attributePosition
728
- + ' into template file ' + this.filePath + node_path_1.sep + this.fileName;
771
+ + ' into template file ' + this.filePath + node_path_2.sep + this.fileName;
729
772
  }
730
773
  }
731
774
  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,13 @@ 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;
22
+ export declare class HtmlResponse {
23
+ html: string;
24
+ dependencies: string[];
25
+ constructor(html: string, ...dependencies: string[]);
26
+ toString(): string;
27
+ }
17
28
  export declare class Template {
18
29
  data?: any;
19
30
  containerData?: any;
@@ -60,6 +71,7 @@ export declare class Template {
60
71
  closeTag(shouldInLiteral: boolean, targetIndex: number): boolean;
61
72
  combineLiterals(text: string, parts?: string[]): string;
62
73
  debugEvents(): void;
74
+ embedHtmlResponse(htmlResponse: HtmlResponse): void;
63
75
  getCleanContext(): {
64
76
  addLinks: SortedArray<string>;
65
77
  doHeadLinks: boolean;
package/esm/template.js CHANGED
@@ -2,10 +2,26 @@ 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, sep } from 'node:path';
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
+ }
16
+ export class HtmlResponse {
17
+ html;
18
+ dependencies;
19
+ constructor(html, ...dependencies) {
20
+ this.html = html;
21
+ this.dependencies = dependencies;
22
+ }
23
+ toString() { return this.html; }
24
+ }
9
25
  export class Template {
10
26
  data;
11
27
  containerData;
@@ -108,6 +124,28 @@ export class Template {
108
124
  this.onTagOpened = (name) => console.log('tag.opened =', name);
109
125
  this.onTagClose = (name) => console.log('tag.closed =', name);
110
126
  }
127
+ embedHtmlResponse(htmlResponse) {
128
+ for (let dependency of htmlResponse.dependencies) {
129
+ if (dependency[0] === '<') {
130
+ const script = dependency.match(/<script[^>]*\bsrc=["']([^"']+)["']/i)?.[1];
131
+ if (script) {
132
+ frontScripts.insert(script);
133
+ }
134
+ this.headLinks.insert(dependency);
135
+ continue;
136
+ }
137
+ dependency = normalize(dependency).slice(appDir.length);
138
+ switch (dependency.slice(dependency.lastIndexOf('.') + 1)) {
139
+ case 'css':
140
+ this.headLinks.insert('<link href="' + dependency + '" rel="stylesheet">');
141
+ continue;
142
+ case 'js':
143
+ frontScripts.insert(dependency);
144
+ this.headLinks.insert('<script src="' + dependency + '" type="module"></script>');
145
+ continue;
146
+ }
147
+ }
148
+ }
111
149
  getCleanContext() {
112
150
  const addLinks = new SortedArray;
113
151
  const doneLinks = new SortedArray;
@@ -385,6 +423,9 @@ export class Template {
385
423
  for (const variable of expression.split('.')) {
386
424
  data = await this.parseVariable(variable, data);
387
425
  }
426
+ if (data instanceof HtmlResponse) {
427
+ this.embedHtmlResponse(data);
428
+ }
388
429
  return data;
389
430
  }
390
431
  async parseVariable(variable, data) {
@@ -413,7 +454,7 @@ export class Template {
413
454
  }
414
455
  }
415
456
  if (data[variable] === undefined) {
416
- data = new Str(data);
457
+ data = new Str(await depends.toString(data));
417
458
  }
418
459
  let value = data[variable];
419
460
  return (((typeof value)[0] === 'f') && ((value + '')[0] !== 'c'))
package/package.json CHANGED
@@ -69,5 +69,5 @@
69
69
  "test": "test/test"
70
70
  },
71
71
  "types": "./esm/template.d.ts",
72
- "version": "0.0.41"
72
+ "version": "0.1.0"
73
73
  }