@itrocks/template 0.0.42 → 0.1.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 CHANGED
@@ -410,6 +410,11 @@ Any expression starting with `/`, `./` or `../` is considered a template include
410
410
  </div>
411
411
  ```
412
412
 
413
+ The path prefix defines how the file location is resolved:
414
+ - `./` refers to the directory of the current template file
415
+ - `../` refers to the parent directory of the current template file
416
+ - `/` refers to the root of the current project (not the filesystem root)
417
+
413
418
  The default contextual data is the one in the current scope.
414
419
 
415
420
  You can pass parent or sub-data to your included template as an alternative context:
package/cjs/template.d.ts CHANGED
@@ -19,6 +19,12 @@ type Open = '(' | '{';
19
19
  export type VariableParser = [parser: string, (variable: string, data: any) => any];
20
20
  export declare const frontScripts: SortedArray<string>;
21
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
+ }
22
28
  export declare class Template {
23
29
  data?: any;
24
30
  containerData?: any;
@@ -65,6 +71,7 @@ export declare class Template {
65
71
  closeTag(shouldInLiteral: boolean, targetIndex: number): boolean;
66
72
  combineLiterals(text: string, parts?: string[]): string;
67
73
  debugEvents(): void;
74
+ embedHtmlResponse(htmlResponse: HtmlResponse): void;
68
75
  getCleanContext(): {
69
76
  addLinks: SortedArray<string>;
70
77
  doHeadLinks: boolean;
@@ -101,6 +108,7 @@ export declare class Template {
101
108
  targetStack: string[];
102
109
  };
103
110
  include(path: string, data: any): Promise<string>;
111
+ includePath(filePath: string): string;
104
112
  isContextClean(): boolean;
105
113
  literalTarget(index: number, isTitle?: boolean): void;
106
114
  parseBuffer(buffer: string): Promise<string>;
package/cjs/template.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Template = exports.frontScripts = exports.depends = void 0;
3
+ exports.Template = exports.HtmlResponse = exports.frontScripts = exports.depends = void 0;
4
4
  exports.templateDependsOn = templateDependsOn;
5
5
  const app_dir_1 = require("@itrocks/app-dir");
6
6
  const rename_1 = require("@itrocks/rename");
@@ -17,6 +17,16 @@ exports.frontScripts.distinct = true;
17
17
  function templateDependsOn(dependencies) {
18
18
  Object.assign(exports.depends, dependencies);
19
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;
20
30
  class Template {
21
31
  data;
22
32
  containerData;
@@ -119,6 +129,28 @@ class Template {
119
129
  this.onTagOpened = (name) => console.log('tag.opened =', name);
120
130
  this.onTagClose = (name) => console.log('tag.closed =', name);
121
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
+ }
122
154
  getCleanContext() {
123
155
  const addLinks = new sorted_array_1.SortedArray;
124
156
  const doneLinks = new sorted_array_1.SortedArray;
@@ -194,6 +226,11 @@ class Template {
194
226
  ? parsed.slice(beginPosition + 12, (endPosition > -1) ? endPosition : parsed.length)
195
227
  : parsed;
196
228
  }
229
+ includePath(filePath) {
230
+ return (filePath[0] === '/')
231
+ ? (app_dir_1.appDir + ((filePath[1] === '@') ? '/node_modules' : '') + filePath)
232
+ : filePath;
233
+ }
197
234
  isContextClean() {
198
235
  const clean = this.getCleanContext();
199
236
  const context = this.getContext();
@@ -369,13 +406,13 @@ class Template {
369
406
  expressionEnd--;
370
407
  }
371
408
  const blockStack = this.blockStack;
372
- return this.include(expression.slice(0, expressionEnd), blockStack[blockStack.length - blockBack].data);
409
+ return this.include(this.includePath(expression.slice(0, expressionEnd)), blockStack[blockStack.length - blockBack].data);
373
410
  }
374
411
  if (expression[expressionEnd] === ')') {
375
412
  const openPosition = expression.lastIndexOf('(');
376
- return this.include(expression.slice(0, openPosition), await this.parsePath(expression.slice(openPosition + 1, expression.length - 1), data));
413
+ return this.include(this.includePath(expression.slice(0, openPosition)), await this.parsePath(expression.slice(openPosition + 1, expression.length - 1), data));
377
414
  }
378
- return this.include(expression, data);
415
+ return this.include(this.includePath(expression), data);
379
416
  }
380
417
  let onlyDots = true;
381
418
  for (const c of expression) {
@@ -396,6 +433,9 @@ class Template {
396
433
  for (const variable of expression.split('.')) {
397
434
  data = await this.parseVariable(variable, data);
398
435
  }
436
+ if (data instanceof HtmlResponse) {
437
+ this.embedHtmlResponse(data);
438
+ }
399
439
  return data;
400
440
  }
401
441
  async parseVariable(variable, data) {
package/esm/template.d.ts CHANGED
@@ -19,6 +19,12 @@ type Open = '(' | '{';
19
19
  export type VariableParser = [parser: string, (variable: string, data: any) => any];
20
20
  export declare const frontScripts: SortedArray<string>;
21
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
+ }
22
28
  export declare class Template {
23
29
  data?: any;
24
30
  containerData?: any;
@@ -65,6 +71,7 @@ export declare class Template {
65
71
  closeTag(shouldInLiteral: boolean, targetIndex: number): boolean;
66
72
  combineLiterals(text: string, parts?: string[]): string;
67
73
  debugEvents(): void;
74
+ embedHtmlResponse(htmlResponse: HtmlResponse): void;
68
75
  getCleanContext(): {
69
76
  addLinks: SortedArray<string>;
70
77
  doHeadLinks: boolean;
@@ -101,6 +108,7 @@ export declare class Template {
101
108
  targetStack: string[];
102
109
  };
103
110
  include(path: string, data: any): Promise<string>;
111
+ includePath(filePath: string): string;
104
112
  isContextClean(): boolean;
105
113
  literalTarget(index: number, isTitle?: boolean): void;
106
114
  parseBuffer(buffer: string): Promise<string>;
package/esm/template.js CHANGED
@@ -13,6 +13,15 @@ frontScripts.distinct = true;
13
13
  export function templateDependsOn(dependencies) {
14
14
  Object.assign(depends, dependencies);
15
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
+ }
16
25
  export class Template {
17
26
  data;
18
27
  containerData;
@@ -115,6 +124,28 @@ export class Template {
115
124
  this.onTagOpened = (name) => console.log('tag.opened =', name);
116
125
  this.onTagClose = (name) => console.log('tag.closed =', name);
117
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
+ }
118
149
  getCleanContext() {
119
150
  const addLinks = new SortedArray;
120
151
  const doneLinks = new SortedArray;
@@ -190,6 +221,11 @@ export class Template {
190
221
  ? parsed.slice(beginPosition + 12, (endPosition > -1) ? endPosition : parsed.length)
191
222
  : parsed;
192
223
  }
224
+ includePath(filePath) {
225
+ return (filePath[0] === '/')
226
+ ? (appDir + ((filePath[1] === '@') ? '/node_modules' : '') + filePath)
227
+ : filePath;
228
+ }
193
229
  isContextClean() {
194
230
  const clean = this.getCleanContext();
195
231
  const context = this.getContext();
@@ -365,13 +401,13 @@ export class Template {
365
401
  expressionEnd--;
366
402
  }
367
403
  const blockStack = this.blockStack;
368
- return this.include(expression.slice(0, expressionEnd), blockStack[blockStack.length - blockBack].data);
404
+ return this.include(this.includePath(expression.slice(0, expressionEnd)), blockStack[blockStack.length - blockBack].data);
369
405
  }
370
406
  if (expression[expressionEnd] === ')') {
371
407
  const openPosition = expression.lastIndexOf('(');
372
- return this.include(expression.slice(0, openPosition), await this.parsePath(expression.slice(openPosition + 1, expression.length - 1), data));
408
+ return this.include(this.includePath(expression.slice(0, openPosition)), await this.parsePath(expression.slice(openPosition + 1, expression.length - 1), data));
373
409
  }
374
- return this.include(expression, data);
410
+ return this.include(this.includePath(expression), data);
375
411
  }
376
412
  let onlyDots = true;
377
413
  for (const c of expression) {
@@ -392,6 +428,9 @@ export class Template {
392
428
  for (const variable of expression.split('.')) {
393
429
  data = await this.parseVariable(variable, data);
394
430
  }
431
+ if (data instanceof HtmlResponse) {
432
+ this.embedHtmlResponse(data);
433
+ }
395
434
  return data;
396
435
  }
397
436
  async parseVariable(variable, data) {
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.42"
72
+ "version": "0.1.1"
73
73
  }