docx-handlebars 0.1.7

A Rust library for processing DOCX files with Handlebars templates, supporting WASM, Node.js, Deno, and browsers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# docx-handlebars

[![Crates.io](https://img.shields.io/crates/v/docx-handlebars.svg)](https://crates.io/crates/docx-handlebars)
[![Documentation](https://docs.rs/docx-handlebars/badge.svg)](https://docs.rs/docx-handlebars)
[![License](https://img.shields.io/crates/l/docx-handlebars.svg)](https://github.com/sail-sail/docx-handlebars#license)

一个用于处理 DOCX 文件 Handlebars 模板的 Rust 库,支持多平台使用:
- 🦀 Rust 原生
- 🌐 WebAssembly (WASM)
- 📦 npm 包
- 🟢 Node.js
- 🦕 Deno
- 🌍 浏览器端
- 📋 JSR (JavaScript Registry)

## 功能特性

- **智能合并**:自动处理被 XML 标签分割的 Handlebars 语法
-**DOCX 验证**:内置文件格式验证,确保输入文件有效
-**Handlebars 支持**:完整的模板引擎,支持变量、条件、循环、Helper 函数
-**跨平台**:Rust 原生 + WASM 支持多种运行时
-**TypeScript**:完整的类型定义和智能提示
-**零依赖**:WASM 二进制文件,无外部依赖
-**错误处理**:详细的错误信息和类型安全的错误处理

## 安装

### Rust

```bash
cargo add docx-handlebars
```

### npm

```bash
npm install docx-handlebars
```

### Deno

```typescript
import { render, init } from "https://deno.land/x/docx_handlebars/mod.ts";
```

### JSR

```bash
npx jsr add @sail/docx-handlebars
```

## 使用示例

### Rust

```rust
use docx_handlebars::render_handlebars;
use serde_json::json;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 读取 DOCX 模板文件
    let template_bytes = std::fs::read("template.docx")?;
    
    // 准备数据
    let data = json!({
        "name": "张三",
        "company": "ABC科技有限公司",
        "position": "软件工程师",
        "projects": [
            {"name": "项目A", "status": "已完成"},
            {"name": "项目B", "status": "进行中"}
        ],
        "has_bonus": true,
        "bonus_amount": 5000
    });
    
    // 渲染模板
    let result = render_handlebars(template_bytes, &data)?;
    
    // 保存结果
    std::fs::write("output.docx", result)?;
    
    Ok(())
}
```

### JavaScript/TypeScript (Node.js)

```javascript
import { render, init } from 'docx-handlebars';
import fs from 'fs';

async function processTemplate() {
    // 初始化 WASM 模块
    await init();
    
    // 读取模板文件
    const templateBytes = fs.readFileSync('template.docx');
    
    // 准备数据
    const data = {
        name: "李明",
        company: "XYZ技术有限公司",
        position: "高级开发工程师",
        projects: [
            { name: "E-commerce平台", status: "已完成" },
            { name: "移动端APP", status: "开发中" }
        ],
        has_bonus: true,
        bonus_amount: 8000
    };
    
    // 渲染模板
    const result = render(templateBytes, JSON.stringify(data));
    
    // 保存结果
    fs.writeFileSync('output.docx', new Uint8Array(result));
}

processTemplate().catch(console.error);
```

### Deno

```typescript
import { render, init } from "https://deno.land/x/docx_handlebars/mod.ts";

async function processTemplate() {
    // 初始化 WASM 模块
    await init();
    
    // 读取模板文件
    const templateBytes = await Deno.readFile("template.docx");
    
    // 准备数据
    const data = {
        name: "王小明",
        department: "研发部",
        projects: [
            { name: "智能客服系统", status: "已上线" },
            { name: "数据可视化平台", status: "开发中" }
        ]
    };
    
    // 渲染模板
    const result = render(templateBytes, JSON.stringify(data));
    
    // 保存结果
    await Deno.writeFile("output.docx", new Uint8Array(result));
}

if (import.meta.main) {
    await processTemplate();
}
```

### 浏览器端

```html
<!DOCTYPE html>
<html>
<head>
    <title>DOCX Handlebars 示例</title>
</head>
<body>
    <input type="file" id="fileInput" accept=".docx">
    <button onclick="processFile()">处理模板</button>
    
    <script type="module">
        import { render, init } from './pkg/docx_handlebars.js';
        
        // 初始化 WASM
        await init();
        
        window.processFile = async function() {
            const fileInput = document.getElementById('fileInput');
            const file = fileInput.files[0];
            
            if (!file) return;
            
            const arrayBuffer = await file.arrayBuffer();
            const templateBytes = new Uint8Array(arrayBuffer);
            
            const data = {
                name: "张三",
                company: "示例公司"
            };
            
            try {
                const result = render(templateBytes, JSON.stringify(data));
                
                // 下载结果
                const blob = new Blob([new Uint8Array(result)], {
                    type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
                });
                const url = URL.createObjectURL(blob);
                const a = document.createElement('a');
                a.href = url;
                a.download = 'processed.docx';
                a.click();
            } catch (error) {
                console.error('处理失败:', error);
            }
        };
    </script>
</body>
</html>
```

## 模板语法

### 基础变量替换

```handlebars
员工姓名: {{name}}
公司: {{company}}
职位: {{position}}
```

### 条件渲染

```handlebars
{{#if has_bonus}}
奖金: ¥{{bonus_amount}}
{{else}}
无奖金
{{/if}}

{{#unless is_intern}}
正式员工
{{/unless}}
```

### 循环渲染

```handlebars
项目经历:
{{#each projects}}
- {{name}}: {{description}} ({{status}})
{{/each}}

技能列表:
{{#each skills}}
{{@index}}. {{this}}
{{/each}}
```

### Helper 函数

内置的 Helper 函数:

```handlebars
{{upper name}}           <!-- 转大写 -->
{{lower company}}        <!-- 转小写 -->
{{len projects}}         <!-- 数组长度 -->
{{#if (eq status "completed")}}已完成{{/if}}    <!-- 相等比较 -->
{{#if (gt score 90)}}优秀{{/if}}               <!-- 大于比较 -->
{{#if (lt age 30)}}年轻{{/if}}                 <!-- 小于比较 -->
```

### 复杂示例

```handlebars
=== 员工报告 ===

基本信息:
姓名: {{employee.name}}
部门: {{employee.department}}
职位: {{employee.position}}
入职时间: {{employee.hire_date}}

{{#if employee.has_bonus}}
💰 奖金: ¥{{employee.bonus_amount}}
{{/if}}

项目经历 (共{{len projects}}个):
{{#each projects}}
{{@index}}. {{name}}
   描述: {{description}}
   状态: {{status}}
   团队规模: {{team_size}}人
   
{{/each}}

技能评估:
{{#each skills}}
- {{name}}: {{level}}/10 ({{years}}年经验)
{{/each}}

{{#if (gt performance.score 90)}}
🎉 绩效评级: 优秀
{{else if (gt performance.score 80)}}
👍 绩效评级: 良好
{{else}}
📈 绩效评级: 需改进
{{/if}}
```

## 错误处理

库提供了详细的错误类型和消息:

### Rust

```rust
use docx_handlebars::{render_handlebars, DocxError};

match render_handlebars(template_bytes, &data) {
    Ok(result) => {
        println!("处理成功!");
        std::fs::write("output.docx", result)?;
    }
    Err(e) => match e.downcast_ref::<DocxError>() {
        Some(DocxError::FileTooSmall) => {
            eprintln!("错误: 文件太小,不是有效的 DOCX 文件");
        }
        Some(DocxError::InvalidZipSignature) => {
            eprintln!("错误: 文件不是有效的 ZIP/DOCX 格式");
        }
        Some(DocxError::MissingRequiredFile(filename)) => {
            eprintln!("错误: 缺少必需的 DOCX 文件: {}", filename);
        }
        _ => {
            eprintln!("其他错误: {}", e);
        }
    }
}
```

### JavaScript/TypeScript

```javascript
try {
    const result = render(templateBytes, JSON.stringify(data));
    console.log('处理成功!');
} catch (error) {
    if (error.message.includes('文件大小不足')) {
        console.error('文件太小,不是有效的 DOCX 文件');
    } else if (error.message.includes('无效的 ZIP 签名')) {
        console.error('文件不是有效的 ZIP/DOCX 格式');
    } else if (error.message.includes('缺少必需的 DOCX 文件')) {
        console.error('文件不包含必需的 DOCX 组件');
    } else if (error.message.includes('模板渲染失败')) {
        console.error('Handlebars 模板语法错误或数据不匹配');
    } else {
        console.error('处理失败:', error.message);
    }
}
```

## 构建和开发

### 构建 WASM 包

```bash
# 构建所有目标
npm run build

# 或分别构建
npm run build:web    # 浏览器版本
npm run build:npm    # Node.js 版本 
npm run build:jsr    # Deno 版本
```

### 运行示例

```bash
# Rust 示例
cargo run --example rust_example

# Node.js 示例
node examples/node_example.js

# Deno 示例  
deno run --allow-read --allow-write examples/deno_example.ts

# 浏览器示例
# 启动本地服务器并打开 examples/browser_demo.html
```

## 技术特性

### 智能合并算法

该库的核心创新是智能合并被 XML 标签分割的 Handlebars 语法。在 DOCX 文件中,当用户输入模板语法时,Word 可能会将其拆分成多个 XML 标签:

**原始分割状态:**
```xml
<w:t>员工姓名: {{</w:t><w:t>employee.name</w:t><w:t>}}</w:t>
```

**智能合并后:**
```xml
<w:t>员工姓名: {{employee.name}}</w:t>
```

支持的合并模式:
- 简单分割: `<w:t>{{</w:t><w:t>variable}}</w:t>`
- 部分分割: `<w:t>{{part1</w:t><w:t>part2}}</w:t>`
- 三段分割: `<w:t>{{</w:t><w:t>part1</w:t><w:t>part2}}</w:t>`
- 复杂嵌套: `<w:t>prefix{{</w:t><w:t>content</w:t><w:t>}}suffix</w:t>`

### 文件验证

内置的 DOCX 文件验证确保输入文件的完整性:

1. **ZIP 格式验证**:检查文件签名和结构
2. **DOCX 结构验证**:确保包含必要的文件
   - `[Content_Types].xml`
   - `_rels/.rels` 
   - `word/document.xml`
3. **MIME 类型验证**:验证内容类型正确性

## 性能和兼容性

- **零拷贝**: Rust 和 WASM 之间高效的内存管理
- **流式处理**: 适合处理大型 DOCX 文件
- **跨平台**: 支持 Windows、macOS、Linux、Web
- **现代浏览器**: 支持所有支持 WASM 的现代浏览器

## 许可证

本项目采用 MIT 许可证 - 详见 [LICENSE-MIT](LICENSE-MIT) 文件。

## 贡献

欢迎贡献代码!请查看我们的贡献指南:

1. Fork 项目
2. 创建特性分支 (`git checkout -b feature/AmazingFeature`)
3. 提交更改 (`git commit -m 'Add some AmazingFeature'`)
4. 推送到分支 (`git push origin feature/AmazingFeature`)
5. 开启 Pull Request

## 更新日志

### v0.1.6

- **重大重构**: 采用函数式 API,更简洁易用
-**智能合并**: 完善的 Handlebars 语法分割合并算法  
-**文件验证**: 内置 DOCX 格式验证和错误处理
-**错误处理**: 使用 thiserror 提供详细的错误信息
-**Helper 函数**: 内置常用的 Handlebars helper
- 🐛 **修复**: 多种边界情况和兼容性问题
- 📚 **文档**: 全面更新文档和示例
- 🧪 **测试**: 完整的测试覆盖和验证脚本

## 支持

- 📚 [文档]https://docs.rs/docx-handlebars
- 🐛 [问题反馈]https://github.com/sail-sail/docx-handlebars/issues
- 💬 [讨论]https://github.com/sail-sail/docx-handlebars/discussions

---

<div align="center">
  <p>
    <strong>docx-handlebars</strong> - 让 DOCX 模板处理变得简单高效
  </p>
  <p>
    <a href="https://github.com/sail-sail/docx-handlebars">⭐ 给项目点个星</a>
    ·
    <a href="https://github.com/sail-sail/docx-handlebars/issues">🐛 报告问题</a>
    ·
    <a href="https://github.com/sail-sail/docx-handlebars/discussions">💬 参与讨论</a>
  </p>
</div>