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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (C) 2026 ALGFORCE INC.
use pulldown_cmark::{html, Options, Parser};
use scraper::{node::Node, ElementRef, Html};
use crate::get_font_for_locale;
// 引入 MarkdownStyle
#[derive(Debug, Clone, Default)]
pub struct MarkdownStyle {
pub font: Option<String>,
pub size: Option<u32>,
pub indent: Option<u32>,
pub locale: String,
}
impl MarkdownStyle {
pub fn new() -> Self {
MarkdownStyle {
font: None,
size: Some(10),
indent: Some(420),
locale: "en".to_string(),
}
}
/// 根据 locale 设置默认字体
pub fn with_locale(mut self, locale: String) -> Self {
self.locale = locale.clone();
if self.font.is_none() {
self.font = Some(get_font_for_locale(&locale));
}
self
}
/// 设置默认字体
pub fn with_font(mut self, font: String) -> Self {
self.font = Some(font);
self
}
}
/// 表示Markdown内容
#[derive(Debug)]
pub struct Markdown {
content: String,
style: Option<MarkdownStyle>,
}
impl Markdown {
/// 创建新的Markdown内容
pub fn new(content: String) -> Self {
Self {
content,
style: Some(MarkdownStyle::new()),
}
}
/// 根据 locale 设置默认字体
pub fn with_locale(mut self, locale: String) -> Self {
self.style = Some(self.style.unwrap_or_default().with_locale(locale));
self
}
pub fn with_font(mut self, font: String) -> Self {
self.style.get_or_insert_with(MarkdownStyle::default).font = Some(font);
self
}
pub fn with_size(mut self, size: u32) -> Self {
self.style.get_or_insert_with(MarkdownStyle::default).size = Some(size);
self
}
pub fn with_indent(mut self, indent: u32) -> Self {
self.style.get_or_insert_with(MarkdownStyle::default).indent = Some(indent);
self
}
/// 获取原始内容
pub fn content(&self) -> &str {
&self.content
}
/// 将Markdown转换为HTML
pub fn to_html(&self) -> String {
// 设置Markdown解析选项,启用GFM扩展特性
let mut options = Options::empty();
options.insert(Options::ENABLE_TABLES);
options.insert(Options::ENABLE_FOOTNOTES);
options.insert(Options::ENABLE_STRIKETHROUGH);
options.insert(Options::ENABLE_TASKLISTS);
// 解析Markdown并转换为HTML
let parser = Parser::new_ext(&self.content, options);
let mut html_output = String::new();
html::push_html(&mut html_output, parser);
// 去除开头的空白字符
html_output.trim_start().to_string()
}
/// 生成OpenXML格式
pub fn generate_openxml(&self, escape_fn: fn(&str) -> String) -> String {
// 先转换为HTML
let html = self.to_html();
// 使用html_to_openxml函数将HTML转换为OpenXML
html_to_openxml(&html, escape_fn, self.style.as_ref())
}
}
/// 解析HTML并转换为OpenXML格式
pub fn html_to_openxml(
html: &str,
escape_fn: fn(&str) -> String,
style: Option<&MarkdownStyle>,
) -> String {
let document = Html::parse_document(html);
let mut result = String::new();
let fragment = document.root_element();
// 处理HTML文档的内容
for child in fragment.children() {
match child.value() {
Node::Element(element) => {
if let Some(element_ref) = ElementRef::wrap(child) {
result.push_str(&element_to_openxml(
&element_ref,
&document,
escape_fn,
style,
));
}
}
Node::Text(text) => {
if !text.trim().is_empty() {
// 处理纯文本节点
result.push_str(&format!(
"<w:p><w:r>{}<w:t>{}</w:t></w:r></w:p>",
get_style_rpr(style),
escape_fn(text.trim())
));
}
}
_ => {}
}
}
// 去除开头的空白字符
result.trim_start().to_string()
}
/// 生成样式的rPr标签
fn get_style_rpr(style: Option<&MarkdownStyle>) -> String {
let mut rpr = String::new();
if let Some(style) = style {
rpr.push_str("<w:rPr>");
// 添加字体
if let Some(font) = &style.font {
rpr.push_str(&format!(
"<w:rFonts w:ascii=\"{}\" w:eastAsia=\"{}\" w:hAnsi=\"{}\" w:cs=\"{}\"/>",
font, font, font, font
));
}
// 添加字体大小
if let Some(size) = style.size {
rpr.push_str(&format!(
"<w:sz w:val=\"{}\"/><w:szCs w:val=\"{}\"/>",
size * 2,
size * 2
)); // Word中的大小单位是半点,所以乘以2
}
rpr.push_str("</w:rPr>");
}
rpr
}
/// 将HTML元素转换为OpenXML
fn element_to_openxml(
element: &ElementRef,
document: &Html,
escape_fn: fn(&str) -> String,
style: Option<&MarkdownStyle>,
) -> String {
let tag_name = element.value().name();
match tag_name {
"p" => paragraph_to_openxml(element, document, escape_fn, style),
"h1" | "h2" | "h3" | "h4" | "h5" | "h6" => {
heading_to_openxml(element, document, escape_fn, tag_name, style)
}
"ul" | "ol" => list_to_openxml(element, document, escape_fn, tag_name, style),
"li" => list_item_to_openxml(element, document, escape_fn, style),
"table" => table_to_openxml(element, document, escape_fn, style),
"div" | "span" | "section" | "article" => {
// 处理容器元素,递归处理其子元素
let mut result = String::new();
for child in element.children() {
match child.value() {
Node::Element(_) => {
if let Some(child_ref) = ElementRef::wrap(child) {
result.push_str(&element_to_openxml(
&child_ref, document, escape_fn, style,
));
}
}
Node::Text(text) => {
if !text.trim().is_empty() {
result.push_str(&format!(
"<w:p><w:r>{}<w:t>{}</w:t></w:r></w:p>",
get_style_rpr(style),
escape_fn(text.trim())
));
}
}
_ => {}
}
}
result
}
_ => {
// 处理其它未知元素,默认作为段落处理
let mut result = String::new();
if element.children().count() > 0 {
result.push_str("<w:p>");
if let Some(style) = style {
if let Some(indent) = style.indent {
result.push_str(&format!(
"<w:pPr><w:ind w:firstLine=\"{}\"/></w:pPr>",
indent
));
}
}
// 递归处理子元素
for child in element.children() {
match child.value() {
Node::Element(_) => {
if let Some(child_ref) = ElementRef::wrap(child) {
result.push_str(&element_to_openxml(
&child_ref, document, escape_fn, style,
));
}
}
Node::Text(text) => {
if !text.trim().is_empty() {
result.push_str(&format!(
"<w:r>{}<w:t>{}</w:t></w:r>",
get_style_rpr(style),
escape_fn(text.trim())
));
}
}
_ => {}
}
}
result.push_str("</w:p>");
}
result
}
}
}
/// 将段落元素转换为OpenXML
fn paragraph_to_openxml(
element: &ElementRef,
document: &Html,
escape_fn: fn(&str) -> String,
style: Option<&MarkdownStyle>,
) -> String {
let mut result = String::new();
// 处理段落内的内容
for child in element.children() {
match child.value() {
Node::Element(el) => {
if let Some(child_ref) = ElementRef::wrap(child) {
let tag_name = child_ref.value().name();
match tag_name {
"strong" | "b" => {
// 处理粗体
let mut rpr = get_style_rpr(style);
// 如果rPr标签已经存在,在其中添加粗体标签
if rpr.contains("<w:rPr>") {
rpr = rpr.replace("</w:rPr>", "<w:b/></w:rPr>");
} else {
rpr = "<w:rPr><w:b/></w:rPr>".to_string();
}
result.push_str("<w:r>");
result.push_str(&rpr);
result.push_str("<w:t>");
let text = get_element_text(&child_ref);
result.push_str(&escape_fn(&text));
result.push_str("</w:t></w:r>");
}
"em" | "i" => {
// 处理斜体
let mut rpr = get_style_rpr(style);
// 如果rPr标签已经存在,在其中添加斜体标签
if rpr.contains("<w:rPr>") {
rpr = rpr.replace("</w:rPr>", "<w:i/></w:rPr>");
} else {
rpr = "<w:rPr><w:i/></w:rPr>".to_string();
}
result.push_str("<w:r>");
result.push_str(&rpr);
result.push_str("<w:t>");
let text = get_element_text(&child_ref);
result.push_str(&escape_fn(&text));
result.push_str("</w:t></w:r>");
}
"a" => {
// 处理链接
let href = child_ref.value().attr("href").unwrap_or("#");
result.push_str("<w:hyperlink r:id=\"");
result.push_str(href); // 需要更完善的链接处理
result.push_str("\"><w:r>");
let mut rpr = get_style_rpr(style);
// 如果rPr标签已经存在,在其中添加链接样式
if rpr.contains("<w:rPr>") {
rpr = rpr.replace(
"</w:rPr>",
"<w:color w:val=\"0000FF\"/><w:u w:val=\"single\"/></w:rPr>",
);
} else {
rpr = "<w:rPr><w:color w:val=\"0000FF\"/><w:u w:val=\"single\"/></w:rPr>".to_string();
}
result.push_str(&rpr);
result.push_str("<w:t>");
let text = get_element_text(&child_ref);
result.push_str(&escape_fn(&text));
result.push_str("</w:t></w:r></w:hyperlink>");
}
_ => {
// 处理其他内联元素
result.push_str("<w:r>");
result.push_str(&get_style_rpr(style));
result.push_str("<w:t>");
let text = get_element_text(&child_ref);
result.push_str(&escape_fn(&text));
result.push_str("</w:t></w:r>");
}
}
}
}
Node::Text(text) => {
if !text.trim().is_empty() {
result.push_str("<w:r>");
result.push_str(&get_style_rpr(style));
result.push_str("<w:t>");
result.push_str(&escape_fn(text.trim()));
result.push_str("</w:t></w:r>");
}
}
_ => {}
}
}
result
}
/// 将标题元素转换为OpenXML
fn heading_to_openxml(
element: &ElementRef,
document: &Html,
escape_fn: fn(&str) -> String,
tag_name: &str,
style: Option<&MarkdownStyle>,
) -> String {
let level = match tag_name {
"h1" => "1",
"h2" => "2",
"h3" => "3",
"h4" => "4",
"h5" => "5",
"h6" => "6",
_ => "1",
};
let mut result = String::new();
result.push_str("<w:p><w:pPr><w:pStyle w:val=\"Heading");
result.push_str(level);
result.push_str("\"/></w:pPr>");
// 处理标题内容
for child in element.children() {
match child.value() {
Node::Element(_) => {
if let Some(child_ref) = ElementRef::wrap(child) {
// 递归处理子元素(如果有的话)
// 此处简化处理
let text = get_element_text(&child_ref);
result.push_str("<w:r>");
result.push_str(&get_style_rpr(style));
result.push_str("<w:t>");
result.push_str(&escape_fn(&text));
result.push_str("</w:t></w:r>");
}
}
Node::Text(text) => {
if !text.trim().is_empty() {
result.push_str("<w:r>");
result.push_str(&get_style_rpr(style));
result.push_str("<w:t>");
result.push_str(&escape_fn(text.trim()));
result.push_str("</w:t></w:r>");
}
}
_ => {}
}
}
result.push_str("</w:p>");
result
}
/// 将列表元素转换为OpenXML
fn list_to_openxml(
element: &ElementRef,
document: &Html,
escape_fn: fn(&str) -> String,
tag_name: &str,
style: Option<&MarkdownStyle>,
) -> String {
let mut result = String::new();
// 处理每个列表项
for (i, child) in element.children().enumerate() {
match child.value() {
Node::Element(_) => {
if let Some(child_ref) = ElementRef::wrap(child) {
result.push_str(&element_to_openxml(&child_ref, document, escape_fn, style));
}
}
Node::Text(text) => {
if !text.trim().is_empty() {
result.push_str(&format!(
"<w:p><w:r>{}<w:t>{}</w:t></w:r></w:p>",
get_style_rpr(style),
escape_fn(text.trim())
));
}
}
_ => {}
}
}
result
}
/// 将列表项元素转换为OpenXML(当单独处理时)
fn list_item_to_openxml(
element: &ElementRef,
document: &Html,
escape_fn: fn(&str) -> String,
style: Option<&MarkdownStyle>,
) -> String {
// 默认作为普通段落处理
paragraph_to_openxml(element, document, escape_fn, style)
}
/// 将表格元素转换为OpenXML
fn table_to_openxml(
element: &ElementRef,
document: &Html,
escape_fn: fn(&str) -> String,
style: Option<&MarkdownStyle>,
) -> String {
let mut result = String::new();
result.push_str("<w:tbl><w:tblPr><w:tblStyle w:val=\"TableGrid\"/><w:tblW w:w=\"0\" w:type=\"auto\"/><w:tblLook w:val=\"04A0\" w:firstRow=\"1\" w:lastRow=\"0\" w:firstColumn=\"1\" w:lastColumn=\"0\" w:noHBand=\"0\" w:noVBand=\"1\"/></w:tblPr>");
// 此处简化处理表格,实际情况可能需要更复杂的表格处理逻辑
// 处理表格行
for child in element.children() {
match child.value() {
Node::Element(_) => {
if let Some(child_ref) = ElementRef::wrap(child) {
if child_ref.value().name() == "tr" {
result.push_str("<w:tr>");
// 处理单元格
for cell in child_ref.children() {
match cell.value() {
Node::Element(_) => {
if let Some(cell_ref) = ElementRef::wrap(cell) {
if cell_ref.value().name() == "td"
|| cell_ref.value().name() == "th"
{
result.push_str(
"<w:tc><w:tcPr><w:tcW w:w=\"0\" w:type=\"auto\"/>",
);
// 如果是表头,添加加粗样式
if cell_ref.value().name() == "th" {
result.push_str("<w:shd w:val=\"clear\" w:color=\"auto\" w:fill=\"D9D9D9\"/>");
}
result.push_str("</w:tcPr>");
// 处理单元格内容
let cell_content = get_element_text(&cell_ref);
result.push_str("<w:p><w:r>");
result.push_str(&get_style_rpr(style));
result.push_str("<w:t>");
result.push_str(&escape_fn(&cell_content));
result.push_str("</w:t></w:r></w:p>");
result.push_str("</w:tc>");
}
}
}
_ => {}
}
}
result.push_str("</w:tr>");
}
}
}
_ => {}
}
}
result.push_str("</w:tbl>");
result
}
/// 获取元素及其子元素的所有文本内容
fn get_element_text(element: &ElementRef) -> String {
let mut text = String::new();
for child in element.children() {
match child.value() {
Node::Element(_) => {
if let Some(child_ref) = ElementRef::wrap(child) {
text.push_str(&get_element_text(&child_ref));
}
}
Node::Text(text_content) => {
text.push_str(text_content);
}
_ => {}
}
}
text
}