liepress 0.1.0-beta.0

A Markdown to PDF/SVG/PNG converter with CSS styling support
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
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
pub mod error;
pub mod generator;
pub mod render;
pub mod ast;
pub mod text;
pub mod visual;

use std::fs;
use std::path::Path;
use std::path::PathBuf;

pub use render::{
    PixmapDocumentGenerator, PixmapRenderer,
    SvgDocumentGenerator, SvgRenderer,
    PdfDocumentGenerator, PdfRenderer, PageRenderer,
};

pub use ast::PageConfig;

use generator::{
    markdown_to_document, markdown_to_document_with_base_dir,
    markdown_to_document_with_css_and_page_config,
    Document,
};

/// Markdown 转换配置
///
/// 控制样式、字体、严格模式等选项。通过 builder 风格方法快速构造:
///
/// ```
/// use liepress::ConvertOptions;
///
/// let opts = ConvertOptions::new()
///     .with_font_family(&["Noto Sans SC", "sans-serif"])
///     .with_css("h1 { color: red; }")
///     .with_strict(true);
/// ```
#[derive(Debug, Clone)]
pub struct ConvertOptions {
    /// 全局默认字体家族列表(优先级从高到低)
    ///
    /// 设置后会自动生成 `body { font-family: ... }` 样式,
    /// 通过 CSS 继承机制应用到所有元素。如果同时提供了 `user_css`
    /// 或 `css_file`,其中的 `body { font-family }` 会覆盖此设置。
    pub font_family: Vec<String>,
    /// 用户提供的 CSS 样式字符串(叠加在默认样式之上)
    pub user_css: String,
    /// 用户提供的 CSS 样式文件路径(叠加在默认样式之上)
    /// 如果与 `user_css` 同时设置,两者会合并
    pub css_file: Option<PathBuf>,
    /// 严格模式:CSS 解析失败时返回错误(默认 false)
    pub strict: bool,
    /// 自动字体:根据文档内容自动选择合适的字体(默认 true)
    ///
    /// 启用后,如果没有显式设置 `font_family`,会根据文档中的字符分布
    /// 自动推荐字体列表(如中文优先 Noto Serif SC + SimSun,日文优先 Noto Serif CJK JP)。
    /// 用户提供的 CSS(包括 `<style>` 中的 `body { font-family }`)始终最高优先级。
    pub auto_font: bool,
    /// 页面配置(页面尺寸、边距等)
    ///
    /// 可通过 `@page` CSS 规则或此字段设置。此字段优先级高于 CSS 中的 `@page` 规则。
    /// 如果为 `None`(默认),则完全由 CSS `@page` 或内置默认值决定。
    pub page_config: Option<PageConfig>,
}

impl ConvertOptions {
    pub fn new() -> Self {
        Self::default()
    }

    /// 设置全局默认字体家族
    ///
    /// 列表按优先级从高到低排列,支持 CSS 通用家族关键字
    /// (`serif`、`sans-serif`、`monospace`)和具体字体名称。
    ///
    /// ```
    /// use liepress::ConvertOptions;
    ///
    /// // 单个字体 + 回退
    /// let opts = ConvertOptions::new().with_font_family(&["Noto Sans SC", "sans-serif"]);
    ///
    /// // 使用通用字体
    /// let opts = ConvertOptions::new().with_font_family(&["serif"]);
    /// ```
    pub fn with_font_family(mut self, families: &[&str]) -> Self {
        self.font_family = families.iter().map(|f| f.to_string()).collect();
        self
    }

    /// 设置用户 CSS 样式字符串
    pub fn with_css(mut self, css: &str) -> Self {
        self.user_css = css.to_string();
        self
    }

    /// 设置用户 CSS 样式文件路径
    pub fn with_css_file(mut self, path: PathBuf) -> Self {
        self.css_file = Some(path);
        self
    }

    /// 设置严格模式
    pub fn with_strict(mut self, strict: bool) -> Self {
        self.strict = strict;
        self
    }

    /// 设置自动字体模式
    pub fn with_auto_font(mut self, auto_font: bool) -> Self {
        self.auto_font = auto_font;
        self
    }

    /// 设置页面配置(页面尺寸、边距等)
    ///
    /// 优先级高于 CSS 中的 `@page` 规则。
    /// 可通过 `PageConfig::default()` 创建后按需设置字段。
    ///
    /// ```
    /// use liepress::{ConvertOptions, ast::PageConfig};
    ///
    /// let page_cfg = PageConfig {
    ///     width: Some(841.890),  // A4 landscape
    ///     height: Some(595.276),
    ///     ..PageConfig::default()
    /// };
    /// let opts = ConvertOptions::new().with_page_config(page_cfg);
    /// ```
    pub fn with_page_config(mut self, config: PageConfig) -> Self {
        self.page_config = Some(config);
        self
    }

    /// 设置页眉文本(支持 {page} 和 {total} 模板变量)
    ///
    /// 页眉会显示在每页的顶部边距区域,居中对齐。
    /// 使用 `{page}` 表示当前页码,`{total}` 表示总页数。
    ///
    /// ```
    /// use liepress::ConvertOptions;
    ///
    /// let opts = ConvertOptions::new()
    ///     .with_header("我的文档");
    ///
    /// let opts = ConvertOptions::new()
    ///     .with_header("第 {page} 页 / 共 {total} 页");
    /// ```
    pub fn with_header(mut self, header: &str) -> Self {
        let config = self.page_config.get_or_insert_with(PageConfig::default);
        config.header = Some(header.to_string());
        self
    }

    /// 设置页脚文本(支持 {page} 和 {total} 模板变量)
    ///
    /// 页脚会显示在每页的底部边距区域,居中对齐。
    /// 使用 `{page}` 表示当前页码,`{total}` 表示总页数。
    ///
    /// ```
    /// use liepress::ConvertOptions;
    ///
    /// let opts = ConvertOptions::new()
    ///     .with_footer("- {page} -");
    ///
    /// let opts = ConvertOptions::new()
    ///     .with_footer("第 {page} 页 / 共 {total} 页");
    /// ```
    pub fn with_footer(mut self, footer: &str) -> Self {
        let config = self.page_config.get_or_insert_with(PageConfig::default);
        config.footer = Some(footer.to_string());
        self
    }

    /// 设置页眉字体大小(pt)
    ///
    /// 默认 9pt。仅在设置了页眉时生效。
    pub fn with_header_font_size(mut self, size: f32) -> Self {
        let config = self.page_config.get_or_insert_with(PageConfig::default);
        config.header_font_size = Some(size);
        self
    }

    /// 设置页脚字体大小(pt)
    ///
    /// 默认 9pt。仅在设置了页脚时生效。
    pub fn with_footer_font_size(mut self, size: f32) -> Self {
        let config = self.page_config.get_or_insert_with(PageConfig::default);
        config.footer_font_size = Some(size);
        self
    }
}

impl Default for ConvertOptions {
    fn default() -> Self {
        Self {
            font_family: Vec::new(),
            user_css: String::new(),
            css_file: None,
            strict: false,
            auto_font: true,
            page_config: None,
        }
    }
}

// ─── 内部渲染辅助函数 ─────────────────────────────────────

fn render_pdf(document: &Document) -> crate::error::Result<Vec<u8>> {
    let mut pdf_gen = PdfDocumentGenerator::new("output".to_string());
    for page in &document.pages {
        pdf_gen.render_page(page)?;
    }
    pdf_gen.finalize()
}

fn render_svg(document: &Document) -> Vec<String> {
    let mut svgs = Vec::new();
    for page in &document.pages {
        let mut renderer = SvgRenderer::new(page.width, page.height);
        renderer.render_elements(&page.elements);
        svgs.push(renderer.finalize());
    }
    svgs
}

fn render_png(document: &Document) -> crate::error::Result<Vec<Vec<u8>>> {
    let mut pngs = Vec::new();
    for page in &document.pages {
        let mut renderer = PixmapRenderer::new_default_dpi(page.width, page.height);
        renderer.render_elements(&page.elements);
        pngs.push(renderer.render_to_png()?);
    }
    Ok(pngs)
}

// ─── 内部文件读取辅助函数 ─────────────────────────────────

fn read_markdown_file(path: &Path) -> crate::error::Result<(String, Option<PathBuf>)> {
    let markdown = fs::read_to_string(path)?;
    let base_dir = path.parent().map(|p| p.to_path_buf());
    Ok((markdown, base_dir))
}

// ─── 自动字体推断 ────────────────────────────────────────

/// 运行脚本范围(避免误判 URL、代码、标签中的字符)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum ScriptRange {
    Han,
    Japanese,
    Korean,
    Latin,
    Other,
}

impl ScriptRange {
    fn from_char(c: char) -> Self {
        let code = c as u32;
        match code {
            // 日文:平假名、片假名
            0x3040..=0x309F | 0x30A0..=0x30FF | 0x31F0..=0x31FF => ScriptRange::Japanese,
            // 中文/汉字:
            //   CJK 统一表意文字 (4E00-9FFF)
            //   CJK 扩展 A (3400-4DBF)
            //   CJK 扩展 B (20000-2A6DF)
            //   CJK 扩展 C (2A700-2B73F)
            //   CJK 扩展 D (2B740-2B81F)
            //   CJK 扩展 E (2B820-2CEAF)
            //   CJK 扩展 F (2CEB0-2EBE0)
            //   CJK 兼容表意文字 (F900-FAFF)
            //   CJK 兼容表意文字补充 (2F800-2FA1F)
            0x3400..=0x4DBF | 0x4E00..=0x9FFF
            | 0xF900..=0xFAFF
            | 0x20000..=0x2A6DF
            | 0x2A700..=0x2B73F
            | 0x2B740..=0x2B81F
            | 0x2B820..=0x2CEAF
            | 0x2CEB0..=0x2EBE0
            | 0x2F800..=0x2FA1F => ScriptRange::Han,
            // 韩文
            0xAC00..=0xD7AF => ScriptRange::Korean,
            // 拉丁文基础、补充标点
            0x0000..=0x00FF | 0x2000..=0x206F => ScriptRange::Latin,
            // 其他有字母属性的字符归为 Latin
            _ if c.is_alphabetic() => ScriptRange::Latin,
            _ => ScriptRange::Other,
        }
    }
}

/// 从 Markdown 文本中推断主要语言,返回推荐字体列表
fn infer_font_family(markdown: &str) -> Vec<String> {
    let mut counts = std::collections::HashMap::new();
    let mut in_code = false;
    let mut in_link = false;

    for line in markdown.lines() {
        // 简单状态机:代码块用 ``` 包围
        if line.trim().starts_with("```") {
            in_code = !in_code;
            continue;
        }
        if in_code {
            continue;
        }

        // 跳过标题标记和列表标记
        let content = line.trim_start().trim_start_matches('#').trim_start();

        for c in content.chars() {
            // 跳过链接内容 [text](url)
            if c == '[' {
                in_link = true;
                continue;
            }
            if in_link && c == ']' {
                in_link = false;
                continue;
            }
            if in_link {
                continue;
            }
            if c == '`' {
                continue;
            }

            let range = ScriptRange::from_char(c);
            if range != ScriptRange::Other {
                *counts.entry(range).or_insert(0) += 1;
            }
        }
    }

    let total: usize = counts.values().sum();
    if total == 0 {
        return vec!["serif".to_string()];
    }

    // 找出占比最高的脚本
    let dominant = counts.iter().max_by_key(|&(_, count)| *count).map(|(k, _)| *k).unwrap_or(ScriptRange::Other);

    match dominant {
        ScriptRange::Han => vec![
            "Noto Serif SC".to_string(),
            "Source Han Serif SC".to_string(),
            "SimSun".to_string(),
            "SimSun-ExtB".to_string(),
            "serif".to_string(),
        ],
        ScriptRange::Japanese => vec![
            "Noto Serif CJK JP".to_string(),
            "Noto Serif JP".to_string(),
            "serif".to_string(),
        ],
        ScriptRange::Korean => vec![
            "Noto Serif CJK KR".to_string(),
            "Noto Serif KR".to_string(),
            "serif".to_string(),
        ],
        ScriptRange::Latin => vec![
            "Noto Serif".to_string(),
            "Georgia".to_string(),
            "Times New Roman".to_string(),
            "serif".to_string(),
        ],
        ScriptRange::Other => vec!["serif".to_string()],
    }
}

// ─── CSS 解析 ───────────────────────────────────────────────

fn resolve_user_css(
    options: &ConvertOptions,
    markdown: Option<&str>,
) -> crate::error::Result<String> {
    let file_css = match &options.css_file {
        Some(path) => fs::read_to_string(path)?,
        None => String::new(),
    };

    // 判断用户是否已经显式设置了 font-family(通过 CSS 字符串)
    // 注意:这里做简单启发式判断。更严谨的做法是在 CSS 解析阶段
    // 检查是否有 body { font-family: ... } 规则。当前先以显式 font_family 为首要考虑。
    let user_has_font_css = file_css.contains("font-family")
        || options.user_css.contains("font-family");

    // 优先级:用户 CSS > auto-font > font_family
    let font_css = if user_has_font_css || !options.font_family.is_empty() {
        if !options.font_family.is_empty() {
            let families: Vec<String> = options
                .font_family
                .iter()
                .map(|f| {
                    if f.contains(' ') {
                        format!("\"{}\"", f)
                    } else {
                        f.clone()
                    }
                })
                .collect();
            format!("body {{ font-family: {}; }}\n", families.join(", "))
        } else {
            String::new()
        }
    } else if options.auto_font {
        if let Some(md) = markdown {
            let families = infer_font_family(md);
            format!(
                "body {{ font-family: {}; }}\n",
                families
                    .iter()
                    .map(|f| {
                        if f.contains(' ') {
                            format!("\"{}\"", f)
                        } else {
                            f.clone()
                        }
                    })
                    .collect::<Vec<_>>()
                    .join(", ")
            )
        } else {
            String::new()
        }
    } else {
        String::new()
    };

    let parts: Vec<&str> = [font_css.as_str(), options.user_css.as_str(), file_css.as_str()]
        .into_iter()
        .filter(|s| !s.is_empty())
        .collect();

    if parts.is_empty() {
        Ok(String::new())
    } else {
        Ok(parts.join("\n"))
    }
}

// ─── 快速入口(无额外配置) ───────────────────────────────

pub fn markdown_to_pdf(markdown: &str) -> crate::error::Result<Vec<u8>> {
    render_pdf(&markdown_to_document(markdown))
}

pub fn markdown_to_svg(markdown: &str) -> crate::error::Result<Vec<String>> {
    Ok(render_svg(&markdown_to_document(markdown)))
}

pub fn markdown_to_png(markdown: &str) -> crate::error::Result<Vec<Vec<u8>>> {
    render_png(&markdown_to_document(markdown))
}

pub fn markdown_file_to_pdf(path: &Path) -> crate::error::Result<Vec<u8>> {
    let (markdown, base_dir) = read_markdown_file(path)?;
    render_pdf(&markdown_to_document_with_base_dir(&markdown, base_dir))
}

pub fn markdown_file_to_svg(path: &Path) -> crate::error::Result<Vec<String>> {
    let (markdown, base_dir) = read_markdown_file(path)?;
    Ok(render_svg(&markdown_to_document_with_base_dir(&markdown, base_dir)))
}

pub fn markdown_file_to_png(path: &Path) -> crate::error::Result<Vec<Vec<u8>>> {
    let (markdown, base_dir) = read_markdown_file(path)?;
    render_png(&markdown_to_document_with_base_dir(&markdown, base_dir))
}

// ─── 带配置的入口 ────────────────────────────────────────

pub fn markdown_to_pdf_with_options(
    markdown: &str,
    options: &ConvertOptions,
) -> crate::error::Result<Vec<u8>> {
    let user_css = resolve_user_css(options, Some(markdown))?;
    let doc = (if options.strict {
        markdown_to_document_with_css_and_page_config(
            markdown, &user_css, options.page_config.clone(), None, true,
        )
    } else {
        markdown_to_document_with_css_and_page_config(
            markdown, &user_css, options.page_config.clone(), None, false,
        )
    })
    .map_err(crate::error::Error::CssParseError)?;
    render_pdf(&doc)
}

pub fn markdown_to_svg_with_options(
    markdown: &str,
    options: &ConvertOptions,
) -> crate::error::Result<Vec<String>> {
    let user_css = resolve_user_css(options, Some(markdown))?;
    let doc = (if options.strict {
        markdown_to_document_with_css_and_page_config(
            markdown, &user_css, options.page_config.clone(), None, true,
        )
    } else {
        markdown_to_document_with_css_and_page_config(
            markdown, &user_css, options.page_config.clone(), None, false,
        )
    })
    .map_err(crate::error::Error::CssParseError)?;
    Ok(render_svg(&doc))
}

pub fn markdown_to_png_with_options(
    markdown: &str,
    options: &ConvertOptions,
) -> crate::error::Result<Vec<Vec<u8>>> {
    let user_css = resolve_user_css(options, Some(markdown))?;
    let doc = (if options.strict {
        markdown_to_document_with_css_and_page_config(
            markdown, &user_css, options.page_config.clone(), None, true,
        )
    } else {
        markdown_to_document_with_css_and_page_config(
            markdown, &user_css, options.page_config.clone(), None, false,
        )
    })
    .map_err(crate::error::Error::CssParseError)?;
    render_png(&doc)
}

pub fn markdown_file_to_pdf_with_options(
    path: &Path,
    options: &ConvertOptions,
) -> crate::error::Result<Vec<u8>> {
    let (markdown, base_dir) = read_markdown_file(path)?;
    let user_css = resolve_user_css(options, Some(&markdown))?;
    let doc = (if options.strict {
        markdown_to_document_with_css_and_page_config(
            &markdown, &user_css, options.page_config.clone(), base_dir, true,
        )
    } else {
        markdown_to_document_with_css_and_page_config(
            &markdown, &user_css, options.page_config.clone(), base_dir, false,
        )
    })
    .map_err(crate::error::Error::CssParseError)?;
    render_pdf(&doc)
}

pub fn markdown_file_to_svg_with_options(
    path: &Path,
    options: &ConvertOptions,
) -> crate::error::Result<Vec<String>> {
    let (markdown, base_dir) = read_markdown_file(path)?;
    let user_css = resolve_user_css(options, Some(&markdown))?;
    let doc = (if options.strict {
        markdown_to_document_with_css_and_page_config(
            &markdown, &user_css, options.page_config.clone(), base_dir, true,
        )
    } else {
        markdown_to_document_with_css_and_page_config(
            &markdown, &user_css, options.page_config.clone(), base_dir, false,
        )
    })
    .map_err(crate::error::Error::CssParseError)?;
    Ok(render_svg(&doc))
}

pub fn markdown_file_to_png_with_options(
    path: &Path,
    options: &ConvertOptions,
) -> crate::error::Result<Vec<Vec<u8>>> {
    let (markdown, base_dir) = read_markdown_file(path)?;
    let user_css = resolve_user_css(options, Some(&markdown))?;
    let doc = (if options.strict {
        markdown_to_document_with_css_and_page_config(
            &markdown, &user_css, options.page_config.clone(), base_dir, true,
        )
    } else {
        markdown_to_document_with_css_and_page_config(
            &markdown, &user_css, options.page_config.clone(), base_dir, false,
        )
    })
    .map_err(crate::error::Error::CssParseError)?;
    render_png(&doc)
}