miki-template 1.2.0 → 1.3.6

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.
Files changed (105) hide show
  1. package/.github/release-notes/v1.3.1.md +55 -0
  2. package/.github/release-notes/v1.3.3.md +77 -0
  3. package/.github/workflows/ci.yml +38 -54
  4. package/.github/workflows/release.yml +106 -0
  5. package/AGENT.md +71 -71
  6. package/API_REFERENCE.md +314 -314
  7. package/CHANGELOG.md +173 -97
  8. package/CODE_OF_CONDUCT.md +14 -14
  9. package/CONTRIBUTING.md +27 -27
  10. package/README.md +342 -304
  11. package/ROADMAP.md +40 -40
  12. package/assets/banner.png +0 -0
  13. package/benchmarks/report.json +16 -16
  14. package/benchmarks/run.js +49 -49
  15. package/benchmarks/stress.mjs +647 -0
  16. package/benchmarks/templates/large.dtpl +7 -7
  17. package/benchmarks/templates/medium.dtpl +3 -3
  18. package/benchmarks/templates/small.dtpl +7 -7
  19. package/context/component.md +109 -109
  20. package/context/prd.md +131 -131
  21. package/context/project-structure.md +33 -33
  22. package/dir/base.html +23 -0
  23. package/dir/cmpnt.html +11 -0
  24. package/dir/footer.html +3 -0
  25. package/dir/home.html +80 -0
  26. package/dir/index.html +80 -0
  27. package/dir/navbar.html +9 -0
  28. package/docs/README.md +18 -18
  29. package/docs/advanced_usage.md +71 -71
  30. package/docs/api.md +119 -102
  31. package/docs/filters.md +708 -540
  32. package/docs/installation.md +106 -106
  33. package/docs/overview.md +57 -57
  34. package/docs/partialdef.md +70 -41
  35. package/docs/security.md +27 -27
  36. package/docs/tags.md +673 -610
  37. package/docs/usage.md +646 -599
  38. package/eslint.config.mjs +42 -34
  39. package/ex.mjs +33 -0
  40. package/miki-template-extension/.github/workflows/ci.yml +116 -0
  41. package/miki-template-extension/.vscodeignore +7 -0
  42. package/miki-template-extension/CHANGELOG.md +99 -0
  43. package/miki-template-extension/LICENSE +21 -21
  44. package/miki-template-extension/README.md +273 -82
  45. package/miki-template-extension/extension.js +1013 -0
  46. package/miki-template-extension/icon.png +0 -0
  47. package/miki-template-extension/icon.svg +10 -10
  48. package/miki-template-extension/miki-template-1.7.1.vsix +0 -0
  49. package/miki-template-extension/package.json +280 -46
  50. package/miki-template-extension/snippets/miki-template.json +717 -177
  51. package/miki-template-extension/syntaxes/language-configuration.json +114 -26
  52. package/miki-template-extension/syntaxes/miki-template.tmLanguage.json +355 -146
  53. package/miki-template-extension/tests/grammar-tests.json +162 -0
  54. package/miki-template-extension/tests/run-grammar-tests.js +82 -0
  55. package/package.json +37 -31
  56. package/sample-app/package-lock.json +901 -0
  57. package/sample-app/package.json +9 -0
  58. package/sample-app/server.js +14 -0
  59. package/sample-app/views/index.html +1 -0
  60. package/scripts/build-vsix.js +129 -0
  61. package/scripts/build-vsix.ps1 +15 -0
  62. package/snippets/miki-template.json +177 -177
  63. package/src/asyncRender.js +20 -20
  64. package/src/cache.js +80 -41
  65. package/src/context.js +126 -122
  66. package/src/context_processors.js +48 -41
  67. package/src/esm.mjs +84 -72
  68. package/src/filters.js +975 -527
  69. package/src/i18n.js +171 -171
  70. package/src/index.js +974 -454
  71. package/src/lexer.js +114 -92
  72. package/src/libraries.js +371 -240
  73. package/src/parser.js +270 -250
  74. package/src/security.js +53 -51
  75. package/src/tags/control.js +719 -590
  76. package/src/tags/extra.js +154 -0
  77. package/src/tags/helpers.js +26 -26
  78. package/src/tags/i18n.js +256 -230
  79. package/src/tags/inheritance.js +335 -216
  80. package/src/tags/registry.js +18 -18
  81. package/src/tags/util.js +400 -322
  82. package/src/types.d.ts +107 -107
  83. package/syntaxes/language-configuration.json +26 -26
  84. package/syntaxes/miki-template.tmLanguage.json +146 -146
  85. package/tests/asyncRender.test.js +17 -17
  86. package/tests/base.html +6 -6
  87. package/tests/child.html +3 -3
  88. package/tests/context_processors.test.js +13 -13
  89. package/tests/esm.test.mjs +61 -26
  90. package/tests/filters.test.js +254 -99
  91. package/tests/include_security.test.js +9 -9
  92. package/tests/integration/README.md +32 -0
  93. package/tests/integration/features.test.cjs +1681 -0
  94. package/tests/integration/features.test.mjs +1697 -0
  95. package/tests/integration/templates/base.miki +6 -0
  96. package/tests/integration/templates/child.miki +6 -0
  97. package/tests/integration/templates/index.html +17 -0
  98. package/tests/lexer.test.js +45 -45
  99. package/tests/parser.test.js +57 -55
  100. package/tests/partial.html +1 -1
  101. package/tests/partialdef.test.js +79 -40
  102. package/tests/production_checks.js +57 -57
  103. package/tests/security.test.js +28 -28
  104. package/tests/tags.test.js +233 -203
  105. package/miki-template-1.2.0.vsix +0 -0
package/src/tags/i18n.js CHANGED
@@ -1,230 +1,256 @@
1
- /**
2
- * i18n template tags: trans, blocktrans, language.
3
- */
4
- const i18n = require('../i18n');
5
-
6
- /**
7
- * {% trans "translation key" %}
8
- * {% trans "Hello, %s!" name %}
9
- * Optional context: {% trans context "key" %}
10
- */
11
- class TransNode {
12
- constructor(key, args) {
13
- this.key = key;
14
- this.args = args; // { name: 'userName', ... }
15
- }
16
-
17
- render(context) {
18
- const params = {};
19
- if (this.args) {
20
- for (const [k, v] of Object.entries(this.args)) {
21
- params[k] = context.get(v);
22
- }
23
- }
24
- // For unquoted keys: if the key is a variable name, resolve it
25
- let key = this.key;
26
- if (!key.startsWith('"') && !key.startsWith('\'') && !key.includes(' ')) {
27
- const resolved = context.get(key);
28
- if (typeof resolved === 'string' && resolved.length > 0) {
29
- key = resolved;
30
- }
31
- } else {
32
- key = key.slice(1, -1);
33
- }
34
- return i18n.lookup(key, params);
35
- }
36
- }
37
-
38
- class LanguageNode {
39
- constructor(lang, body) {
40
- this.lang = lang;
41
- this.body = body;
42
- }
43
-
44
- render(context) {
45
- // Strip quotes
46
- const lang = this.lang.startsWith('"') || this.lang.startsWith('\'')
47
- ? this.lang.slice(1, -1)
48
- : (context.get(this.lang) || this.lang);
49
- const previous = i18n.getLanguage();
50
- i18n.setLanguage(lang);
51
- try {
52
- return this.body.map(n => n.render(context)).join('');
53
- } finally {
54
- i18n.setLanguage(previous);
55
- }
56
- }
57
- }
58
-
59
- /**
60
- * {% blocktrans %}...{% endblocktrans %}
61
- * Supports {% with name=value %}, {% plural count %}, {% endblocktrans %}
62
- * Inner text may contain {% with %} and {% plural %} tags.
63
- */
64
- class BlockTransNode {
65
- constructor(textParts, withMappings, pluralMappings, body) {
66
- // textParts: array of raw string segments between variable interpolations
67
- // e.g. "Hello, " + [name] + "!" => ['Hello, ', { type: 'var', name: 'name' }, '!']
68
- this.textParts = textParts;
69
- this.withMappings = withMappings || [];
70
- this.pluralMappings = pluralMappings || [];
71
- this.body = body; // for nested tags (unused currently)
72
- }
73
-
74
- render(context) {
75
- // 1. Apply {% with %} mappings to a local scope
76
- const localParams = {};
77
- for (const m of this.withMappings) {
78
- localParams[m.name] = context.get(m.valPath);
79
- }
80
- // 2. Apply {% plural count %} mappings
81
- for (const m of this.pluralMappings) {
82
- localParams[m.name] = context.get(m.valPath);
83
- }
84
-
85
- // 3. Reassemble the full text
86
- let fullText = '';
87
- for (const part of this.textParts) {
88
- if (typeof part === 'string') {
89
- fullText += part;
90
- } else if (part.type === 'var') {
91
- const val = localParams[part.name] !== undefined
92
- ? localParams[part.name]
93
- : context.get(part.name);
94
- fullText += String(val);
95
- }
96
- }
97
-
98
- // 4. Look up translation
99
- const count = Object.values(localParams).find(v => typeof v === 'number');
100
- return i18n.lookup(fullText, localParams, count);
101
- }
102
- }
103
-
104
- // --- Parsers ---
105
-
106
- function parseTrans(tagContent, _parser) {
107
- // {% trans "key" %}
108
- // {% trans "Hello %s" name=user.name %}
109
- // {% trans context "key" %}
110
- const trimmed = tagContent.slice(5).trim(); // strip "trans"
111
- const tokens = trimmed.match(/(?:"[^"]*"|'[^']*'|\S+)/g) || [];
112
-
113
- let keyIdx = 0;
114
- if (tokens[0] && tokens[0] === 'context') {
115
- keyIdx = 2;
116
- }
117
- const key = tokens[keyIdx] || '';
118
- const args = {};
119
- for (let i = keyIdx + 1; i < tokens.length; i++) {
120
- const eq = tokens[i].indexOf('=');
121
- if (eq > 0) {
122
- const name = tokens[i].slice(0, eq);
123
- let val = tokens[i].slice(eq + 1);
124
- if ((val.startsWith('"') && val.endsWith('"')) || (val.startsWith('\'') && val.endsWith('\''))) {
125
- val = val.slice(1, -1);
126
- }
127
- args[name] = val;
128
- }
129
- }
130
- return new TransNode(key, Object.keys(args).length ? args : null);
131
- }
132
-
133
- function parseLanguage(tagContent, parser) {
134
- const lang = tagContent.slice(9).trim(); // strip "language"
135
- const body = parser.parse(['endlanguage']);
136
- const next = parser.peek();
137
- if (next && next.type === 'block' && next.content.split(/\s+/)[0] === 'endlanguage') {
138
- parser.advance();
139
- }
140
- return new LanguageNode(lang, body);
141
- }
142
-
143
- function parseBlockTrans(tagContent, parser) {
144
- // Collect body until {% endblocktrans %}
145
- const body = parser.parse(['blocktrans_internal_marker']); // placeholder
146
- // Actually we need a different approach: scan raw body and parse it ourselves
147
- // because blocktrans parses its own text content as a template.
148
- // We'll rewind and use the lexer on the body tokens.
149
- // For simplicity here, we re-parse the entire slice as a mini-template.
150
- // The body tokens between {% blocktrans %} and {% endblocktrans %} are still
151
- // available because parseUntil was called.
152
- // For now, treat the body as a list of nodes that we'll re-render:
153
- return new BlockTransNode(
154
- extractTextAndVars(body),
155
- extractWithMappings(body),
156
- extractPluralMappings(body),
157
- body
158
- );
159
- }
160
-
161
- /** Extract raw text + {% with %}-bound variable references from a body. */
162
- function extractTextAndVars(body) {
163
- const parts = [];
164
- for (const node of body) {
165
- if (node.constructor.name === 'TextNode') {
166
- parts.push(node.content);
167
- } else if (node.constructor.name === 'VariableNode') {
168
- // Top-level var: bind it to the parsed varPath so it can be filled
169
- parts.push({ type: 'var', name: node.varPath });
170
- } else {
171
- parts.push('');
172
- }
173
- }
174
- return parts;
175
- }
176
-
177
- function extractWithMappings(body) {
178
- const mappings = [];
179
- for (const node of body) {
180
- if (node.constructor.name === 'WithNode' && node.mappings) {
181
- for (const m of node.mappings) {
182
- mappings.push({ name: m.name, valPath: m.valPath });
183
- }
184
- }
185
- }
186
- return mappings;
187
- }
188
-
189
- function extractPluralMappings(body) {
190
- // Look for a custom PluralNode (we mark it via a special tag)
191
- // For now, treat WithNodes that have a count attribute as plural mappings.
192
- const mappings = [];
193
- for (const node of body) {
194
- if (node.constructor.name === 'PluralMappingNode') {
195
- mappings.push({ name: node.name, valPath: node.valPath });
196
- }
197
- }
198
- return mappings;
199
- }
200
-
201
- class PluralMappingNode {
202
- constructor(name, valPath) {
203
- this.name = name;
204
- this.valPath = valPath;
205
- }
206
- render() { return ''; }
207
- }
208
-
209
- function parsePlural(tagContent, _parser) {
210
- // {% plural count %}
211
- const rest = tagContent.slice(6).trim();
212
- const eq = rest.indexOf('=');
213
- if (eq > 0) {
214
- return new PluralMappingNode(rest.slice(0, eq).trim(), rest.slice(eq + 1).trim());
215
- }
216
- return new PluralMappingNode('count', rest);
217
- }
218
-
219
- module.exports = {
220
- TransNode,
221
- LanguageNode,
222
- BlockTransNode,
223
- PluralMappingNode,
224
- parsers: {
225
- trans: parseTrans,
226
- language: parseLanguage,
227
- blocktrans: parseBlockTrans,
228
- plural: parsePlural
229
- }
230
- };
1
+ /**
2
+ * i18n template tags: trans, blocktrans, language.
3
+ */
4
+ const i18n = require('../i18n');
5
+
6
+ /**
7
+ * {% trans "translation key" %}
8
+ * {% trans "Hello, %s!" name %}
9
+ * Optional context: {% trans context "key" %}
10
+ */
11
+ class TransNode {
12
+ constructor(key, args) {
13
+ this.key = key;
14
+ this.args = args; // { name: 'userName', ... }
15
+ }
16
+
17
+ render(context) {
18
+ const params = {};
19
+ if (this.args) {
20
+ for (const [k, v] of Object.entries(this.args)) {
21
+ params[k] = context.get(v);
22
+ }
23
+ }
24
+ // For unquoted keys: if the key is a variable name, resolve it
25
+ let key = this.key;
26
+ if (!key.startsWith('"') && !key.startsWith('\'') && !key.includes(' ')) {
27
+ const resolved = context.get(key);
28
+ if (typeof resolved === 'string' && resolved.length > 0) {
29
+ key = resolved;
30
+ }
31
+ } else {
32
+ key = key.slice(1, -1);
33
+ }
34
+ return i18n.lookup(key, params);
35
+ }
36
+ }
37
+
38
+ class LanguageNode {
39
+ constructor(lang, body) {
40
+ this.lang = lang;
41
+ this.body = body;
42
+ }
43
+
44
+ render(context) {
45
+ // Strip quotes
46
+ const lang = this.lang.startsWith('"') || this.lang.startsWith('\'')
47
+ ? this.lang.slice(1, -1)
48
+ : (context.get(this.lang) || this.lang);
49
+ const previous = i18n.getLanguage();
50
+ i18n.setLanguage(lang);
51
+ try {
52
+ return this.body.map(n => n.render(context)).join('');
53
+ } finally {
54
+ i18n.setLanguage(previous);
55
+ }
56
+ }
57
+ }
58
+
59
+ /**
60
+ * {% blocktrans %}...{% endblocktrans %}
61
+ * Supports {% with name=value %}, {% plural count %}, {% endblocktrans %}
62
+ * Inner text may contain {% with %} and {% plural %} tags.
63
+ */
64
+ class BlockTransNode {
65
+ constructor(textParts, withMappings, pluralMappings, body) {
66
+ // textParts: array of raw string segments between variable interpolations
67
+ // e.g. "Hello, " + [name] + "!" => ['Hello, ', { type: 'var', name: 'name' }, '!']
68
+ this.textParts = textParts;
69
+ this.withMappings = withMappings || [];
70
+ this.pluralMappings = pluralMappings || [];
71
+ this.body = body; // for nested tags (unused currently)
72
+ }
73
+
74
+ render(context) {
75
+ // 1. Apply {% with %} mappings to a local scope
76
+ const localParams = {};
77
+ for (const m of this.withMappings) {
78
+ localParams[m.name] = context.get(m.valPath);
79
+ }
80
+ // 2. Apply {% plural count %} mappings
81
+ for (const m of this.pluralMappings) {
82
+ localParams[m.name] = context.get(m.valPath);
83
+ }
84
+
85
+ // 3. Reassemble the full text
86
+ let fullText = '';
87
+ for (const part of this.textParts) {
88
+ if (typeof part === 'string') {
89
+ fullText += part;
90
+ } else if (part.type === 'var') {
91
+ const val = localParams[part.name] !== undefined
92
+ ? localParams[part.name]
93
+ : context.get(part.name);
94
+ fullText += String(val);
95
+ }
96
+ }
97
+
98
+ // 4. Look up translation
99
+ const count = Object.values(localParams).find(v => typeof v === 'number');
100
+ return i18n.lookup(fullText, localParams, count);
101
+ }
102
+ }
103
+
104
+ // --- Parsers ---
105
+
106
+ function parseTrans(tagContent, _parser) {
107
+ // {% trans "key" %}
108
+ // {% trans "Hello %s" name=user.name %}
109
+ // {% trans context "key" %}
110
+ const trimmed = tagContent.slice(5).trim(); // strip "trans"
111
+ const tokens = trimmed.match(/(?:"[^"]*"|'[^']*'|\S+)/g) || [];
112
+
113
+ let keyIdx = 0;
114
+ if (tokens[0] && tokens[0] === 'context') {
115
+ keyIdx = 2;
116
+ }
117
+ const key = tokens[keyIdx] || '';
118
+ const args = {};
119
+ for (let i = keyIdx + 1; i < tokens.length; i++) {
120
+ const eq = tokens[i].indexOf('=');
121
+ if (eq > 0) {
122
+ const name = tokens[i].slice(0, eq);
123
+ let val = tokens[i].slice(eq + 1);
124
+ if ((val.startsWith('"') && val.endsWith('"')) || (val.startsWith('\'') && val.endsWith('\''))) {
125
+ val = val.slice(1, -1);
126
+ }
127
+ args[name] = val;
128
+ }
129
+ }
130
+ return new TransNode(key, Object.keys(args).length ? args : null);
131
+ }
132
+
133
+ function parseLanguage(tagContent, parser) {
134
+ const lang = tagContent.slice(9).trim(); // strip "language"
135
+ const body = parser.parse(['endlanguage']);
136
+ const next = parser.peek();
137
+ if (next && next.type === 'block' && next.content.split(/\s+/)[0] === 'endlanguage') {
138
+ parser.advance();
139
+ }
140
+ return new LanguageNode(lang, body);
141
+ }
142
+
143
+ function parseBlockTrans(tagContent, parser) {
144
+ // Collect body until {% endblocktrans %}. The parser will stop at the
145
+ // first matching end tag it encounters in its untilTags list.
146
+ const body = parser.parse(['endblocktrans']);
147
+ // Consume the endblocktrans token if it's still pending.
148
+ const next = parser.peek();
149
+ if (next && next.type === 'block' && next.content.split(/\s+/)[0] === 'endblocktrans') {
150
+ parser.advance();
151
+ }
152
+
153
+ // Parse `with name=value` and `count` from the blocktrans tag content.
154
+ // Format: {% blocktrans with name1=var1 name2=var2 count n %}...{% endblocktrans %}
155
+ const withMappings = [];
156
+ const pluralMappings = [];
157
+ const afterTrans = tagContent.replace(/^blocktrans\s*/, '').trim();
158
+ // Split on "with" / "count" boundaries, keeping the keywords
159
+ const tokens = afterTrans.match(/(?:"[^"]*"|'[^']*'|\S+)/g) || [];
160
+ let i = 0;
161
+ // Skip any leading context (e.g. "context")
162
+ if (tokens[i] === 'context') i += 2;
163
+ // Check for `with` or `count` after the main text
164
+ while (i < tokens.length) {
165
+ const tok = tokens[i];
166
+ if (tok === 'with') {
167
+ i++;
168
+ // Read name=var pairs until we hit end or another keyword
169
+ while (i < tokens.length && tokens[i].indexOf('=') > 0) {
170
+ const pair = tokens[i];
171
+ const eq = pair.indexOf('=');
172
+ const name = pair.slice(0, eq);
173
+ let val = pair.slice(eq + 1);
174
+ if ((val.startsWith('"') && val.endsWith('"')) || (val.startsWith('\'') && val.endsWith('\''))) {
175
+ val = val.slice(1, -1);
176
+ }
177
+ withMappings.push({ name, valPath: val });
178
+ i++;
179
+ }
180
+ } else if (tok === 'count') {
181
+ i++;
182
+ // Next token is the count variable
183
+ if (i < tokens.length) {
184
+ pluralMappings.push({ name: 'count', valPath: tokens[i++] });
185
+ }
186
+ } else {
187
+ i++;
188
+ }
189
+ }
190
+
191
+ return new BlockTransNode(
192
+ extractTextAndVars(body),
193
+ withMappings.length ? withMappings : extractWithMappings(body),
194
+ pluralMappings,
195
+ body
196
+ );
197
+ }
198
+
199
+ /** Extract raw text + {% with %}-bound variable references from a body. */
200
+ function extractTextAndVars(body) {
201
+ const parts = [];
202
+ for (const node of body) {
203
+ if (node.constructor.name === 'TextNode') {
204
+ parts.push(node.content);
205
+ } else if (node.constructor.name === 'VariableNode') {
206
+ // Top-level var: bind it to the parsed varPath so it can be filled
207
+ parts.push({ type: 'var', name: node.varPath });
208
+ } else {
209
+ parts.push('');
210
+ }
211
+ }
212
+ return parts;
213
+ }
214
+
215
+ function extractWithMappings(body) {
216
+ const mappings = [];
217
+ for (const node of body) {
218
+ if (node.constructor.name === 'WithNode' && node.mappings) {
219
+ for (const m of node.mappings) {
220
+ mappings.push({ name: m.name, valPath: m.valPath });
221
+ }
222
+ }
223
+ }
224
+ return mappings;
225
+ }
226
+
227
+ class PluralMappingNode {
228
+ constructor(name, valPath) {
229
+ this.name = name;
230
+ this.valPath = valPath;
231
+ }
232
+ render() { return ''; }
233
+ }
234
+
235
+ function parsePlural(tagContent, _parser) {
236
+ // {% plural count %}
237
+ const rest = tagContent.slice(6).trim();
238
+ const eq = rest.indexOf('=');
239
+ if (eq > 0) {
240
+ return new PluralMappingNode(rest.slice(0, eq).trim(), rest.slice(eq + 1).trim());
241
+ }
242
+ return new PluralMappingNode('count', rest);
243
+ }
244
+
245
+ module.exports = {
246
+ TransNode,
247
+ LanguageNode,
248
+ BlockTransNode,
249
+ PluralMappingNode,
250
+ parsers: {
251
+ trans: parseTrans,
252
+ language: parseLanguage,
253
+ blocktrans: parseBlockTrans,
254
+ plural: parsePlural
255
+ }
256
+ };