hongs-template 0.1.0 → 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 +18 -19
- package/dist/blocks.d.ts +2 -2
- package/dist/blocks.js +24 -6
- package/dist/parser.js +3 -2
- package/package.json +26 -26
package/README.md
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
极简 Jinja 风格模板引擎,表达式直接使用 JavaScript 语法。
|
|
4
4
|
|
|
5
|
-
>
|
|
5
|
+
> **注意:模板、数据以及数据里的函数均需由开发者维护,视为安全可信。**
|
|
6
6
|
>
|
|
7
|
-
> 本引擎不做沙箱:表达式通过 `new Function` 执行,模板作者可以执行任意 JS
|
|
7
|
+
> 本引擎不做沙箱:表达式通过 `new Function` 执行,模板作者可以执行任意 JS。用于**LLM prompt 模板渲染**、**报告生成**等内部可信场景,不要用于渲染终端用户提交的模板。
|
|
8
8
|
|
|
9
9
|
- `{{ expr }}` 输出、`{% if / for / set / include %}` 指令、`{# 注释 #}`
|
|
10
10
|
- 表达式在编译期用 `new Function` 预编译,`render` 只执行不再解析
|
|
@@ -61,7 +61,7 @@ tpl.render({ title: 'Hi' });
|
|
|
61
61
|
```
|
|
62
62
|
|
|
63
63
|
表达式是标准 JS,可选链、`??`、模板字面量、正则、箭头函数都可用。
|
|
64
|
-
|
|
64
|
+
强烈建议用 `?.` 处理可能缺失的路径;未声明的顶层标识符返回 `undefined`,不会抛 `ReferenceError`。
|
|
65
65
|
|
|
66
66
|
### 条件
|
|
67
67
|
|
|
@@ -87,9 +87,17 @@ tpl.render({ title: 'Hi' });
|
|
|
87
87
|
{% endfor %}
|
|
88
88
|
```
|
|
89
89
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
90
|
+
对象/映射解包:
|
|
91
|
+
|
|
92
|
+
```jinja
|
|
93
|
+
{% for key in obj %}{{ key }}={{ obj[key] }}{% endfor %}
|
|
94
|
+
{% for key, val in obj %}{{ key }}={{ val }}{% endfor %}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
- `Set` / 数组 / 迭代对象 / 字符串 :`for item in value` 逐项遍历
|
|
98
|
+
- `Map` / 普通对象 :`for k in obj` 取键,`for k,v in obj` 取键值
|
|
99
|
+
- `null` / `undefined` 转入 else, 无则忽略
|
|
100
|
+
- 非可迭代值抛 `TemplateError`
|
|
93
101
|
|
|
94
102
|
### 设置变量
|
|
95
103
|
|
|
@@ -124,11 +132,11 @@ tpl.render({ title: 'Hi' });
|
|
|
124
132
|
## 表达式作用域
|
|
125
133
|
|
|
126
134
|
- 标识符先查 render 数据(含循环子作用域),未命中再查 `globalData`
|
|
127
|
-
-
|
|
135
|
+
- 仅顶层遮蔽:`data.abc` 存在时,`globalData.abc` 整体不可见,不做深合并
|
|
128
136
|
- `{% set %}` 只写 render 数据,不修改 `globalData` / `GLOBAL`
|
|
129
137
|
- `process` / `require` / `globalThis` 等宿主全局不可见
|
|
130
138
|
- `this` 也是该作用域,可写 `this.user.name`、`this['x-y']`
|
|
131
|
-
-
|
|
139
|
+
- 模板与数据应视为可信,这是标识符级隔离,不是沙箱
|
|
132
140
|
|
|
133
141
|
## GLOBAL
|
|
134
142
|
|
|
@@ -164,7 +172,7 @@ const tpl = compile(source, {
|
|
|
164
172
|
```ts
|
|
165
173
|
compile(source, {
|
|
166
174
|
baseDir: '/templates', // include 相对目录,可选
|
|
167
|
-
rootDir: '/templates', // include
|
|
175
|
+
rootDir: '/templates', // include 安全根,默认同 baseDir
|
|
168
176
|
globalData: GLOBAL, // 全局数据,默认 GLOBAL
|
|
169
177
|
});
|
|
170
178
|
|
|
@@ -204,21 +212,12 @@ try {
|
|
|
204
212
|
|
|
205
213
|
## 非 Node 环境
|
|
206
214
|
|
|
207
|
-
核心的 `compile` / `render`(含 JS 表达式、`$
|
|
215
|
+
核心的 `compile` / `render`(含 JS 表达式、`$format` 等)不依赖 Node 内置模块,可用于浏览器 / Edge Runtime(经打包器处理 CommonJS)。
|
|
208
216
|
|
|
209
217
|
- `fs` / `path` 是**惰性加载**:`import` 本库不会失败
|
|
210
218
|
- `compileFile`、`include` 在调用/编译到时才提示需要 Node.js `fs` / `path`
|
|
211
219
|
- `$format` 全部为纯 JS 实现,浏览器与 Node 行为一致
|
|
212
220
|
|
|
213
|
-
## 开发
|
|
214
|
-
|
|
215
|
-
```bash
|
|
216
|
-
npm install
|
|
217
|
-
npm run check # 类型检查
|
|
218
|
-
npm test # 构建测试并运行
|
|
219
|
-
npm run build # 输出 dist
|
|
220
|
-
```
|
|
221
|
-
|
|
222
221
|
## License
|
|
223
222
|
|
|
224
223
|
MIT
|
package/dist/blocks.d.ts
CHANGED
|
@@ -38,13 +38,13 @@ export declare class IfBlock implements Block {
|
|
|
38
38
|
render(state: RenderState, scope: Scope): void;
|
|
39
39
|
}
|
|
40
40
|
export declare class ForBlock implements Block {
|
|
41
|
-
private readonly
|
|
41
|
+
private readonly variables;
|
|
42
42
|
private readonly iterable;
|
|
43
43
|
private readonly body;
|
|
44
44
|
private readonly elseBody;
|
|
45
45
|
private readonly line?;
|
|
46
46
|
private readonly file?;
|
|
47
|
-
constructor(
|
|
47
|
+
constructor(variables: string[], iterable: Evaluator, body: Block[], elseBody: Block[], line?: number | undefined, file?: string | undefined);
|
|
48
48
|
render(state: RenderState, scope: Scope): void;
|
|
49
49
|
}
|
|
50
50
|
export declare class IncludeBlock implements Block {
|
package/dist/blocks.js
CHANGED
|
@@ -71,8 +71,8 @@ class IfBlock {
|
|
|
71
71
|
}
|
|
72
72
|
exports.IfBlock = IfBlock;
|
|
73
73
|
class ForBlock {
|
|
74
|
-
constructor(
|
|
75
|
-
this.
|
|
74
|
+
constructor(variables, iterable, body, elseBody, line, file) {
|
|
75
|
+
this.variables = variables;
|
|
76
76
|
this.iterable = iterable;
|
|
77
77
|
this.body = body;
|
|
78
78
|
this.elseBody = elseBody;
|
|
@@ -81,19 +81,28 @@ class ForBlock {
|
|
|
81
81
|
}
|
|
82
82
|
render(state, scope) {
|
|
83
83
|
const value = this.iterable(scope.view);
|
|
84
|
+
const duoed = this.variables.length > 1;
|
|
84
85
|
let entries = null;
|
|
86
|
+
let keyed = false;
|
|
85
87
|
if (value === null || value === undefined) {
|
|
86
88
|
entries = null;
|
|
87
89
|
}
|
|
88
|
-
else if (
|
|
90
|
+
else if (value instanceof Map) {
|
|
91
|
+
entries = value.entries();
|
|
92
|
+
keyed = true;
|
|
93
|
+
}
|
|
94
|
+
else if (value instanceof Set
|
|
89
95
|
|| Array.isArray(value)
|
|
90
|
-
|| value
|
|
91
|
-
|| value instanceof Set
|
|
96
|
+
|| typeof value === 'string'
|
|
92
97
|
|| typeof value[Symbol.iterator] === 'function') {
|
|
98
|
+
if (duoed) {
|
|
99
|
+
throw new expr_1.TemplateError('for k, v requires a Map or plain object', this.line, this.file);
|
|
100
|
+
}
|
|
93
101
|
entries = value;
|
|
94
102
|
}
|
|
95
103
|
else if ((0, data_1.isPlainObject)(value)) {
|
|
96
104
|
entries = Object.entries(value);
|
|
105
|
+
keyed = true;
|
|
97
106
|
}
|
|
98
107
|
else {
|
|
99
108
|
throw new expr_1.TemplateError(`Non-iterable value in for`, this.line, this.file);
|
|
@@ -103,7 +112,16 @@ class ForBlock {
|
|
|
103
112
|
for (const item of entries) {
|
|
104
113
|
hasItems = true;
|
|
105
114
|
const child = createScope(Object.create(scope.data), state.globalData);
|
|
106
|
-
|
|
115
|
+
if (duoed) {
|
|
116
|
+
child.data[this.variables[0]] = item[0];
|
|
117
|
+
child.data[this.variables[1]] = item[1];
|
|
118
|
+
}
|
|
119
|
+
else if (keyed) {
|
|
120
|
+
child.data[this.variables[0]] = item[0];
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
child.data[this.variables[0]] = item;
|
|
124
|
+
}
|
|
107
125
|
renderBlocks(this.body, state, child);
|
|
108
126
|
}
|
|
109
127
|
}
|
package/dist/parser.js
CHANGED
|
@@ -296,10 +296,11 @@ class Parser {
|
|
|
296
296
|
}
|
|
297
297
|
parseFor(token) {
|
|
298
298
|
const content = token.content.trim();
|
|
299
|
-
const match = /^for\s+([A-Za-z_$][\w$]*)\s+in\s+([\s\S]+)$/.exec(content);
|
|
299
|
+
const match = /^for\s+([A-Za-z_$][\w$]*)\s*(?:,\s*([A-Za-z_$][\w$]*))?\s+in\s+([\s\S]+)$/.exec(content);
|
|
300
300
|
if (!match) {
|
|
301
301
|
throw this.error(`Invalid for statement: ${content}`, token.start);
|
|
302
302
|
}
|
|
303
|
+
const variables = match[2] ? [match[1], match[2]] : [match[1]];
|
|
303
304
|
this.pos++;
|
|
304
305
|
let result = this.parseBlocks(['else', 'endfor']);
|
|
305
306
|
const body = result.blocks;
|
|
@@ -314,7 +315,7 @@ class Parser {
|
|
|
314
315
|
throw this.error('Unclosed for statement', token.start);
|
|
315
316
|
}
|
|
316
317
|
this.pos++;
|
|
317
|
-
return new blocks_1.ForBlock(
|
|
318
|
+
return new blocks_1.ForBlock(variables, this.compile(match[3], token), body, elseBlocks, lineAt(this.source, token.start), this.options.file);
|
|
318
319
|
}
|
|
319
320
|
parseSet(token) {
|
|
320
321
|
const content = token.content.trim();
|
package/package.json
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "hongs-template",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "Hong's minimal template engine with JavaScript expressions",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"types": "dist/index.d.ts",
|
|
7
|
-
"exports": {
|
|
8
|
-
".": {
|
|
9
|
-
"types": "./dist/index.d.ts",
|
|
10
|
-
"default": "./dist/index.js"
|
|
11
|
-
}
|
|
12
|
-
},
|
|
13
|
-
"files": [
|
|
14
|
-
"dist"
|
|
15
|
-
],
|
|
16
|
-
"scripts": {
|
|
17
|
-
"build": "tsc",
|
|
18
|
-
"check": "tsc --noEmit",
|
|
19
|
-
"test": "tsc -p tsconfig.test.json && node dist-test/test/template.js"
|
|
20
|
-
},
|
|
21
|
-
"devDependencies": {
|
|
22
|
-
"@types/node": "^20.0.0",
|
|
23
|
-
"typescript": "^5.0.0"
|
|
24
|
-
},
|
|
25
|
-
"license": "MIT"
|
|
26
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "hongs-template",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Hong's minimal template engine with JavaScript expressions",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"default": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "tsc",
|
|
18
|
+
"check": "tsc --noEmit",
|
|
19
|
+
"test": "tsc -p tsconfig.test.json && node dist-test/test/template.js"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@types/node": "^20.0.0",
|
|
23
|
+
"typescript": "^5.0.0"
|
|
24
|
+
},
|
|
25
|
+
"license": "MIT"
|
|
26
|
+
}
|