miki-template 1.2.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.
Files changed (73) hide show
  1. package/.github/workflows/ci.yml +54 -0
  2. package/AGENT.md +71 -0
  3. package/API_REFERENCE.md +314 -0
  4. package/CHANGELOG.md +97 -0
  5. package/CODE_OF_CONDUCT.md +14 -0
  6. package/CONTRIBUTING.md +27 -0
  7. package/README.md +304 -0
  8. package/ROADMAP.md +40 -0
  9. package/benchmarks/report.json +17 -0
  10. package/benchmarks/run.js +49 -0
  11. package/benchmarks/templates/large.dtpl +7 -0
  12. package/benchmarks/templates/medium.dtpl +3 -0
  13. package/benchmarks/templates/small.dtpl +7 -0
  14. package/context/component.md +109 -0
  15. package/context/prd.md +131 -0
  16. package/context/project-structure.md +33 -0
  17. package/docs/README.md +18 -0
  18. package/docs/advanced_usage.md +71 -0
  19. package/docs/api.md +102 -0
  20. package/docs/filters.md +540 -0
  21. package/docs/installation.md +106 -0
  22. package/docs/overview.md +57 -0
  23. package/docs/partialdef.md +41 -0
  24. package/docs/security.md +27 -0
  25. package/docs/tags.md +610 -0
  26. package/docs/usage.md +599 -0
  27. package/eslint.config.mjs +34 -0
  28. package/miki-template-1.2.0.vsix +0 -0
  29. package/miki-template-extension/LICENSE +21 -0
  30. package/miki-template-extension/README.md +82 -0
  31. package/miki-template-extension/icon.png +0 -0
  32. package/miki-template-extension/icon.svg +10 -0
  33. package/miki-template-extension/package.json +46 -0
  34. package/miki-template-extension/snippets/miki-template.json +177 -0
  35. package/miki-template-extension/syntaxes/language-configuration.json +26 -0
  36. package/miki-template-extension/syntaxes/miki-template.tmLanguage.json +146 -0
  37. package/package.json +31 -0
  38. package/snippets/miki-template.json +177 -0
  39. package/src/asyncRender.js +21 -0
  40. package/src/cache.js +41 -0
  41. package/src/context.js +122 -0
  42. package/src/context_processors.js +41 -0
  43. package/src/esm.mjs +72 -0
  44. package/src/filters.js +527 -0
  45. package/src/i18n.js +171 -0
  46. package/src/index.js +454 -0
  47. package/src/lexer.js +92 -0
  48. package/src/libraries.js +240 -0
  49. package/src/parser.js +250 -0
  50. package/src/security.js +51 -0
  51. package/src/tags/control.js +591 -0
  52. package/src/tags/helpers.js +27 -0
  53. package/src/tags/i18n.js +230 -0
  54. package/src/tags/inheritance.js +216 -0
  55. package/src/tags/registry.js +18 -0
  56. package/src/tags/util.js +322 -0
  57. package/src/types.d.ts +107 -0
  58. package/syntaxes/language-configuration.json +26 -0
  59. package/syntaxes/miki-template.tmLanguage.json +146 -0
  60. package/tests/asyncRender.test.js +17 -0
  61. package/tests/base.html +6 -0
  62. package/tests/child.html +3 -0
  63. package/tests/context_processors.test.js +13 -0
  64. package/tests/esm.test.mjs +26 -0
  65. package/tests/filters.test.js +99 -0
  66. package/tests/include_security.test.js +9 -0
  67. package/tests/lexer.test.js +45 -0
  68. package/tests/parser.test.js +55 -0
  69. package/tests/partial.html +1 -0
  70. package/tests/partialdef.test.js +40 -0
  71. package/tests/production_checks.js +57 -0
  72. package/tests/security.test.js +28 -0
  73. package/tests/tags.test.js +203 -0
@@ -0,0 +1,177 @@
1
+ {
2
+ "miki-template": {
3
+ "if": {
4
+ "prefix": "if",
5
+ "body": [
6
+ "{% if ${1:condition} %}",
7
+ " ${2:content}",
8
+ "{% endif %}"
9
+ ],
10
+ "description": "If block"
11
+ },
12
+ "ifelse": {
13
+ "prefix": "ifelse",
14
+ "body": [
15
+ "{% if ${1:condition} %}",
16
+ " ${2:content}",
17
+ "{% else %}",
18
+ " ${3:fallback}",
19
+ "{% endif %}"
20
+ ],
21
+ "description": "If/else block"
22
+ },
23
+ "for": {
24
+ "prefix": "for",
25
+ "body": [
26
+ "{% for ${1:item} in ${2:items} %}",
27
+ " ${3:{{ ${1} }}}",
28
+ "{% empty %}",
29
+ " ${4:No items}",
30
+ "{% endfor %}"
31
+ ],
32
+ "description": "For loop"
33
+ },
34
+ "block": {
35
+ "prefix": "block",
36
+ "body": [
37
+ "{% block ${1:name} %}",
38
+ " ${2:content}",
39
+ "{% endblock %}"
40
+ ],
41
+ "description": "Block definition"
42
+ },
43
+ "extends": {
44
+ "prefix": "extends",
45
+ "body": "{% extends \"${1:base.html}\" %}",
46
+ "description": "Extends parent template"
47
+ },
48
+ "include": {
49
+ "prefix": "include",
50
+ "body": "{% include \"${1:header.html}\" %}",
51
+ "description": "Include template"
52
+ },
53
+ "with": {
54
+ "prefix": "with",
55
+ "body": [
56
+ "{% with ${1:var} as ${2:alias} %}",
57
+ " ${3:{{ ${2} }}}",
58
+ "{% endwith %}"
59
+ ],
60
+ "description": "With block"
61
+ },
62
+ "cycle": {
63
+ "prefix": "cycle",
64
+ "body": "{% cycle '${1:odd}' '${2:even}' %}",
65
+ "description": "Cycle tag"
66
+ },
67
+ "trans": {
68
+ "prefix": "trans",
69
+ "body": "{% trans \"${1:Hello, World!}\" %}",
70
+ "description": "Translate string"
71
+ },
72
+ "blocktrans": {
73
+ "prefix": "blocktrans",
74
+ "body": [
75
+ "{% blocktrans with ${1:name}=${2:user.name} %}",
76
+ " ${3:Hello {{ ${1} }}}",
77
+ "{% endblocktrans %}"
78
+ ],
79
+ "description": "Translate block"
80
+ },
81
+ "language": {
82
+ "prefix": "language",
83
+ "body": [
84
+ "{% language \"${1:fr}\" %}",
85
+ " ${2}",
86
+ "{% endlanguage %}"
87
+ ],
88
+ "description": "Switch language"
89
+ },
90
+ "partialdef": {
91
+ "prefix": "partialdef",
92
+ "body": [
93
+ "{% partialdef ${1:name} %}",
94
+ " ${2}",
95
+ "{% endpartialdef %}"
96
+ ],
97
+ "description": "Define partial"
98
+ },
99
+ "partial": {
100
+ "prefix": "partial",
101
+ "body": "{% partial ${1:name} %}",
102
+ "description": "Render partial"
103
+ },
104
+ "csrf": {
105
+ "prefix": "csrf",
106
+ "body": "{% csrf_token %}",
107
+ "description": "CSRF token input"
108
+ },
109
+ "csp": {
110
+ "prefix": "csp",
111
+ "body": "<script {% csp_nonce_attr %}></script>",
112
+ "description": "CSP nonce attribute"
113
+ },
114
+ "static": {
115
+ "prefix": "static",
116
+ "body": "{% static \"${1:css/app.css}\" %}",
117
+ "description": "Static file URL"
118
+ },
119
+ "url": {
120
+ "prefix": "url",
121
+ "body": "{% url \"${1:route_name}\" ${2:args} %}",
122
+ "description": "URL for named route"
123
+ },
124
+ "widthratio": {
125
+ "prefix": "widthratio",
126
+ "body": "{% widthratio ${1:value} ${2:max} ${3:max_width} %}",
127
+ "description": "Proportional width"
128
+ },
129
+ "debug": {
130
+ "prefix": "debug",
131
+ "body": "{% debug %}",
132
+ "description": "Dump template context"
133
+ },
134
+ "load": {
135
+ "prefix": "load",
136
+ "body": "{% load ${1:i18n} %}",
137
+ "description": "Load tag library"
138
+ },
139
+ "comment": {
140
+ "prefix": "comment",
141
+ "body": [
142
+ "{#",
143
+ " ${1:comment}",
144
+ "#}"
145
+ ],
146
+ "description": "Comment block"
147
+ },
148
+ "verbatim": {
149
+ "prefix": "verbatim",
150
+ "body": [
151
+ "{% verbatim %}",
152
+ " ${1}",
153
+ "{% endverbatim %}"
154
+ ],
155
+ "description": "Verbatim block"
156
+ },
157
+ "regroup": {
158
+ "prefix": "regroup",
159
+ "body": "{% for ${1:group} in ${2:items}|regroup:\"${3:category}\" %}{{ ${1}.grouper }}{% endfor %}",
160
+ "description": "Regroup by attribute"
161
+ },
162
+ "firstof": {
163
+ "prefix": "firstof",
164
+ "body": "{% firstof ${1:var1} ${2:var2} \"${3:fallback}\" %}",
165
+ "description": "First truthy value"
166
+ },
167
+ "filter_block": {
168
+ "prefix": "filter",
169
+ "body": [
170
+ "{% filter ${1:lower} %}",
171
+ " ${2}",
172
+ "{% endfilter %}"
173
+ ],
174
+ "description": "Filter block"
175
+ }
176
+ }
177
+ }
@@ -0,0 +1,21 @@
1
+ // Async rendering utilities
2
+
3
+ /**
4
+ * Async version of renderAST that awaits any Promise returned by node.render.
5
+ * Handles async nodes anywhere in the AST (inside conditionals, loops, etc.)
6
+ */
7
+ async function asyncRenderAST(nodes, context) {
8
+ // Helper to render a single node, awaiting if needed, and recursively handling children
9
+ const renderNode = async (node) => {
10
+ const result = node.render(context);
11
+ return result instanceof Promise ? await result : result;
12
+ };
13
+
14
+ const parts = [];
15
+ for (const node of nodes) {
16
+ parts.push(await renderNode(node));
17
+ }
18
+ return parts.join('');
19
+ }
20
+
21
+ module.exports = { asyncRenderAST };
package/src/cache.js ADDED
@@ -0,0 +1,41 @@
1
+ // Simple AST cache module
2
+
3
+ /**
4
+ * In‑memory LRU cache for compiled templates.
5
+ * For demo purposes we use a plain Map limited to 100 entries.
6
+ */
7
+ const CACHE_LIMIT = 100;
8
+ const cache = new Map();
9
+
10
+ /**
11
+ * Retrieve a compiled template from cache or compile it and store.
12
+ * @param {string} templateStr – template source
13
+ * @param {object} compileOptions – options passed to compile()
14
+ * @param {function} compileFn – the original compile function from src/index.js
15
+ * @returns {object} compiled template object
16
+ */
17
+ function getCompiled(templateStr, compileOptions, compileFn) {
18
+ const key = JSON.stringify({ templateStr, compileOptions });
19
+ if (cache.has(key)) {
20
+ // Move to end to mark as recently used
21
+ const value = cache.get(key);
22
+ cache.delete(key);
23
+ cache.set(key, value);
24
+ return value;
25
+ }
26
+ const compiled = compileFn(templateStr, compileOptions);
27
+ cache.set(key, compiled);
28
+ // Trim if we exceed limit
29
+ if (cache.size > CACHE_LIMIT) {
30
+ const firstKey = cache.keys().next().value;
31
+ cache.delete(firstKey);
32
+ }
33
+ return compiled;
34
+ }
35
+
36
+ /** Clear the cache – useful for tests */
37
+ function clearCache() {
38
+ cache.clear();
39
+ }
40
+
41
+ module.exports = { getCompiled, clearCache };
package/src/context.js ADDED
@@ -0,0 +1,122 @@
1
+ /**
2
+ * Context manager handling scopes and dotted variable lookup.
3
+ */
4
+ class Context {
5
+ constructor(initial = {}, options = {}) {
6
+ this.scopes = [initial];
7
+ this.options = options;
8
+ this.autoescape = true; // Autoescape state
9
+ this.blocks = {}; // For template inheritance blocks
10
+ this.parentTemplate = null; // For extends tag
11
+ this.cycleStates = new Map(); // Store state for cycle tags
12
+ this.partialDefs = new Map(); // Store partial definitions
13
+ }
14
+
15
+ /**
16
+ * Reset cycle state and blocks for a fresh render.
17
+ * Called at the start of each render pass.
18
+ */
19
+ reset() {
20
+ this.cycleStates.clear();
21
+ this.blocks = {};
22
+ this.parentTemplate = null;
23
+ }
24
+
25
+ /**
26
+ * Push a new scope onto the stack.
27
+ */
28
+ push(scope = {}) {
29
+ this.scopes.unshift(scope);
30
+ return this;
31
+ }
32
+
33
+ /**
34
+ * Pop the top scope from the stack.
35
+ */
36
+ pop() {
37
+ if (this.scopes.length > 1) {
38
+ this.scopes.shift();
39
+ }
40
+ return this;
41
+ }
42
+
43
+ /**
44
+ * Resolve a dotted path lookup.
45
+ * Examples: 'user.name', 'items.0', 'user.profile.age'
46
+ */
47
+ get(path) {
48
+ if (path === undefined || path === null || path === '') {
49
+ return '';
50
+ }
51
+
52
+ const parts = path.split('.');
53
+ const baseName = parts[0];
54
+
55
+ let current = null;
56
+ let found = false;
57
+
58
+ // Search scopes from top (most local) to bottom (most global)
59
+ for (const scope of this.scopes) {
60
+ if (scope && typeof scope === 'object' && baseName in scope) {
61
+ current = scope[baseName];
62
+ found = true;
63
+ break;
64
+ }
65
+ }
66
+
67
+ if (!found) {
68
+ return '';
69
+ }
70
+
71
+ // Traverse the rest of the dotted segments
72
+ for (let i = 1; i < parts.length; i++) {
73
+ if (current === undefined || current === null) {
74
+ return '';
75
+ }
76
+
77
+ const parent = current;
78
+ const part = parts[i];
79
+
80
+ // Resolve segment on the current value
81
+ if (typeof current === 'object' && part in current) {
82
+ current = current[part];
83
+ } else if (Array.isArray(current) && !isNaN(part)) {
84
+ // Handle array index resolution, e.g. items.0
85
+ current = current[parseInt(part, 10)];
86
+ } else {
87
+ return '';
88
+ }
89
+
90
+ // If the property value is a function, evaluate it (Django style)
91
+ if (typeof current === 'function') {
92
+ current = current.call(parent);
93
+ }
94
+ }
95
+
96
+ // If the final resolved value is a function, call it with no arguments
97
+ if (typeof current === 'function') {
98
+ current = current.call(null);
99
+ }
100
+
101
+ return current !== undefined && current !== null ? current : '';
102
+ }
103
+
104
+ /**
105
+ * Register a partial definition.
106
+ */
107
+ registerPartial(name, node) {
108
+ this.partialDefs.set(name, node);
109
+ }
110
+
111
+ /**
112
+ * Retrieve a registered partial.
113
+ */
114
+ getPartial(name) {
115
+ return this.partialDefs.get(name);
116
+ }
117
+
118
+ }
119
+
120
+ module.exports = {
121
+ Context
122
+ };
@@ -0,0 +1,41 @@
1
+ // Context Processors – similar to Django's custom context processors
2
+ // Allows users to register functions that inject additional variables into the rendering context.
3
+
4
+ const processors = [];
5
+
6
+ /**
7
+ * Register a context processor function.
8
+ * The function receives the current context object (plain JS object) and may return
9
+ * an object of key/value pairs to be merged into the context.
10
+ * @param {function(Object): (Object|undefined)} fn
11
+ */
12
+ function registerContextProcessor(fn) {
13
+ if (typeof fn !== 'function') {
14
+ throw new Error('Context processor must be a function');
15
+ }
16
+ processors.push(fn);
17
+ }
18
+
19
+ /**
20
+ * Clear all registered context processors.
21
+ */
22
+ function clearContextProcessors() {
23
+ processors.length = 0;
24
+ }
25
+
26
+ /**
27
+ * Apply all registered processors to the given context object.
28
+ * Mutates the context object by merging any returned values.
29
+ * @param {Object} ctx
30
+ */
31
+ function applyContextProcessors(ctx) {
32
+ for (const fn of processors) {
33
+ const result = fn(ctx);
34
+ if (result && typeof result === 'object') {
35
+ Object.assign(ctx, result);
36
+ }
37
+ }
38
+ return ctx;
39
+ }
40
+
41
+ module.exports = { registerContextProcessor, applyContextProcessors, clearContextProcessors };
package/src/esm.mjs ADDED
@@ -0,0 +1,72 @@
1
+ // ESM wrapper for miki-template
2
+ // Re-exports the CommonJS module for use with `import` syntax.
3
+ // Usage:
4
+ // import miki from 'miki-template';
5
+ // // or:
6
+ // import { render, compile, __express, SafeString, markSafe } from 'miki-template';
7
+
8
+ import cjsModule from './index.js';
9
+
10
+ const {
11
+ compile,
12
+ render,
13
+ asyncRender,
14
+ __express,
15
+ __expressAsync,
16
+ stripExpressContext,
17
+ clearCache,
18
+ registerTag,
19
+ registerFilter,
20
+ registerHelper,
21
+ registerContextProcessor,
22
+ registerTranslation,
23
+ unregisterTranslation,
24
+ setLanguage,
25
+ getLanguage,
26
+ setFallbackLanguage,
27
+ getFallbackLanguage,
28
+ getAvailableLanguages,
29
+ registerLibrary,
30
+ unregisterLibrary,
31
+ getLibrary,
32
+ getLibraryNames,
33
+ hasLibrary,
34
+ registerLibraryFromPath,
35
+ SafeString,
36
+ markSafe,
37
+ isSafe,
38
+ escapeHtml
39
+ } = cjsModule;
40
+
41
+ export {
42
+ compile,
43
+ render,
44
+ asyncRender,
45
+ __express,
46
+ __expressAsync,
47
+ stripExpressContext,
48
+ clearCache,
49
+ registerTag,
50
+ registerFilter,
51
+ registerHelper,
52
+ registerContextProcessor,
53
+ registerTranslation,
54
+ unregisterTranslation,
55
+ setLanguage,
56
+ getLanguage,
57
+ setFallbackLanguage,
58
+ getFallbackLanguage,
59
+ getAvailableLanguages,
60
+ registerLibrary,
61
+ unregisterLibrary,
62
+ getLibrary,
63
+ getLibraryNames,
64
+ hasLibrary,
65
+ registerLibraryFromPath,
66
+ SafeString,
67
+ markSafe,
68
+ isSafe,
69
+ escapeHtml
70
+ };
71
+
72
+ export default cjsModule;