@trustme24/flext 2.0.0 → 2.0.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trustme24/flext",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "A technology for building reliable document templates",
5
5
  "keywords": ["templates", "templating engine", "documents", "document engine", "handlebars"],
6
6
  "repository": "https://github.com/TrustMe-kz/flext.git",
@@ -1,3 +1,2 @@
1
1
  export { default as Latest } from './latest';
2
- export { default as Legacy_1_0_beta3 } from './legacy-1-0-beta3';
3
2
  export { default as Legacy_1_0_beta4 } from './legacy-1-0-beta4';
@@ -1,7 +1,75 @@
1
- import { Dialect } from '@flext/core';
1
+ import { AST } from '@handlebars/parser';
2
+ import { types, lib, Dialect, BaseError, BaseWarning } from '@flext/core';
3
+
4
+
5
+ // Constants
6
+
7
+ export const HANDLEBARS_COMMENT_BEGIN = '{{!--';
8
+
9
+ export const DIALECT_MACRO = 'syntax';
10
+
11
+ export const LEGACY_DIALECT_MACRO = 'v';
12
+
2
13
 
3
14
  export class LegacyDialect extends Dialect {
4
15
  public name = '1.0.beta4';
16
+
17
+ public testAst(val: AST.Program, doWarn?: boolean|null): boolean {
18
+
19
+ // Doing some checks
20
+
21
+ if (!this.name)
22
+ throw new BaseError(`Flext: Unable to test the template: 'name' param is not set in the dialect`);
23
+
24
+
25
+ // Doing some checks
26
+
27
+ const macros = lib.astToMacros(val);
28
+ const macro = macros?.find(m => m?.name === LEGACY_DIALECT_MACRO) ?? null;
29
+
30
+ if (!macro) {
31
+ if (doWarn)
32
+ throw new BaseWarning(`Flext: Unable to test the template: '@v' macro is not set`);
33
+ else
34
+ return false;
35
+ }
36
+
37
+
38
+ // Getting the dialect
39
+
40
+ const [ param ] = macro?.params ?? [];
41
+
42
+ if (!param) {
43
+ if (doWarn)
44
+ throw new BaseWarning(`Flext: Unable to test the template: Bad '@v' macro`);
45
+ else
46
+ return false;
47
+ }
48
+
49
+
50
+ if (this.aliases && this.aliases?.length)
51
+ return param?.value === this.name || lib.inarr(param?.value, ...this.aliases);
52
+ else
53
+ return param?.value === this.name;
54
+ }
55
+
56
+ public templateToStandard(template: types.Template): types.StandardTemplate {
57
+ let result = template;
58
+
59
+
60
+ // Defining the functions
61
+
62
+ const filter = (search: string, val: string): void => { result = result.replace(search, val); };
63
+
64
+
65
+ // Getting the template
66
+
67
+ filter(HANDLEBARS_COMMENT_BEGIN + ' @' + LEGACY_DIALECT_MACRO, HANDLEBARS_COMMENT_BEGIN + ' @' + DIALECT_MACRO); // '{{!-- @v' --> '{{!-- @syntax'
68
+ filter(HANDLEBARS_COMMENT_BEGIN + '@' + LEGACY_DIALECT_MACRO, HANDLEBARS_COMMENT_BEGIN + '@' + DIALECT_MACRO); // '{{!--@v' --> '{{!--@syntax'
69
+
70
+
71
+ return result;
72
+ }
5
73
  }
6
74
 
7
75
  export default LegacyDialect;
package/src/index.ts CHANGED
@@ -4,6 +4,13 @@ import * as core from '@flext/core';
4
4
  import * as dialects from '@/dialects';
5
5
 
6
6
 
7
+ // Constants
8
+
9
+ export const DIALECT_MACRO = 'syntax';
10
+
11
+ export const LEGACY_DIALECT_MACRO = 'v';
12
+
13
+
7
14
  // Variables
8
15
 
9
16
  export const latestDialect = new dialects.Latest();
@@ -17,7 +24,7 @@ export class Flext extends Processor implements types.FlextInterface {
17
24
 
18
25
  // Getting the macro
19
26
 
20
- const macro = macros?.find(m => m?.name === 'syntax') ?? null;
27
+ const macro = macros?.find(m => lib.inarr(m?.name, DIALECT_MACRO, LEGACY_DIALECT_MACRO)) ?? null;
21
28
 
22
29
  if (!macro) {
23
30
  this.setDialect(latestDialect);
@@ -1,5 +0,0 @@
1
- import { Dialect } from '@flext/core';
2
- export declare class LegacyDialect extends Dialect {
3
- name: string;
4
- }
5
- export default LegacyDialect;
@@ -1,7 +0,0 @@
1
- import { Dialect } from '@flext/core';
2
-
3
- export class LegacyDialect extends Dialect {
4
- public name = '1.0.beta3';
5
- }
6
-
7
- export default LegacyDialect;