liepress 0.1.4

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
//! HTML 解析器
//!
//! 使用 html5ever 将 HTML 字符串解析为 HtmlDocument。
//! 通过自定义 TreeSink 直接生成我们的 HtmlElement 树,
//! 不再依赖已被移除的 RcDom。

use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::rc::{Rc, Weak};

use html5ever::driver::ParseOpts;
use html5ever::tendril::{StrTendril, TendrilSink};
use html5ever::tree_builder::TreeBuilderOpts;
use html5ever::tree_builder::{ElementFlags, NodeOrText, QuirksMode, TreeSink};
use html5ever::{Attribute, ExpandedName, LocalName, QualName, parse_document, parse_fragment};

use super::ast::*;

// ─── Custom DOM TreeSink ───────────────────────────────────

/// Simple DOM node for building our HtmlElement tree.
///
/// Design note: element_name is stored outside RefCell so that
/// `TreeSink::elem_name()` can return references without lifetime conflicts.
struct DomNode {
    parent: Cell<Option<Weak<DomNode>>>,
    children: RefCell<Vec<Rc<DomNode>>>,
    /// Element name; None for non-element nodes (document, text, comment)
    element_name: Option<QualName>,
    /// Element attributes (mutated by add_attrs_if_missing)
    element_attrs: RefCell<Vec<Attribute>>,
    /// Text content for text nodes
    text_content: RefCell<Option<StrTendril>>,
    /// Comment content for comment nodes
    _comment_content: RefCell<Option<StrTendril>>,
    /// Whether this is the document node
    is_document: bool,
}

type Handle = Rc<DomNode>;

/// Our custom TreeSink that builds a DOM tree
struct DomSink {
    document: Handle,
    _errors: Vec<String>,
    _quirks_mode: QuirksMode,
}

impl DomSink {
    fn new() -> Self {
        DomSink {
            document: Rc::new(DomNode {
                parent: Cell::new(None),
                children: RefCell::new(Vec::new()),
                element_name: None,
                element_attrs: RefCell::new(Vec::new()),
                text_content: RefCell::new(None),
                _comment_content: RefCell::new(None),
                is_document: true,
            }),
            _errors: Vec::new(),
            _quirks_mode: QuirksMode::NoQuirks,
        }
    }

    /// Convert the internal DOM tree to our HtmlDocument
    #[allow(clippy::wrong_self_convention)]
    fn into_html_document(&self) -> HtmlDocument {
        let root = self.collect_children(&self.document);
        let html_elem = root
            .into_iter()
            .find_map(|node| match node {
                HtmlNode::Element(e) if e.tag == HtmlTag::Html || e.tag == HtmlTag::Body => Some(e),
                _ => None,
            })
            .unwrap_or_else(|| HtmlElement {
                tag: HtmlTag::Unknown,
                attrs: HashMap::new(),
                children: Vec::new(),
            });

        let style_sheets = extract_style_sheets(&html_elem);
        HtmlDocument {
            root: html_elem,
            style_sheets,
        }
    }

    fn collect_children(&self, handle: &Handle) -> Vec<HtmlNode> {
        self.collect_children_with_opts(handle, false)
    }

    /// 收集子节点,`in_pre` 为 true 时不 trim 文本节点(保留 <pre> 内的原始空白)
    fn collect_children_with_opts(&self, handle: &Handle, in_pre: bool) -> Vec<HtmlNode> {
        let mut result = Vec::new();
        let mut pending_text = String::new();

        for child in handle.children.borrow().iter() {
            if let Some(ref name) = child.element_name {
                // Element node - flush pending text first
                let text = if in_pre {
                    pending_text.clone()
                } else {
                    pending_text.trim().to_string()
                };
                if !text.is_empty() {
                    result.push(HtmlNode::Text(text));
                }
                pending_text.clear();

                let tag_name = name.local.as_ref().to_string();
                let attr_map = {
                    let attrs = child.element_attrs.borrow();
                    let mut map = HashMap::new();
                    for attr in attrs.iter() {
                        map.insert(attr.name.local.as_ref().to_string(), attr.value.to_string());
                    }
                    map
                };

                let child_in_pre = tag_name == "pre" || in_pre;
                let children = self.collect_children_with_opts(child, child_in_pre);

                let children = if tag_name == "head" {
                    children
                        .into_iter()
                        .filter(|c| {
                            if let HtmlNode::Element(e) = c {
                                e.tag == HtmlTag::Style
                            } else {
                                false
                            }
                        })
                        .collect()
                } else {
                    children
                };

                result.push(HtmlNode::Element(HtmlElement {
                    tag: HtmlTag::from_str(&tag_name),
                    attrs: attr_map,
                    children,
                }));
            } else if child.is_document {
                // Document node - recurse into children, preserving in_pre context
                let doc_children = self.collect_children_with_opts(child, in_pre);
                result.extend(doc_children);
            } else {
                // Text or comment node
                if let Some(ref text) = *child.text_content.borrow() {
                    pending_text.push_str(text.as_ref());
                }
            }
        }

        // Flush remaining text
        let text = if in_pre {
            pending_text.clone()
        } else {
            pending_text.trim().to_string()
        };
        if !text.is_empty() {
            result.push(HtmlNode::Text(text));
        }

        result
    }
}

impl TreeSink for DomSink {
    type Handle = Handle;
    type Output = HtmlDocument;
    type ElemName<'a> = ExpandedName<'a>;

    fn finish(self) -> HtmlDocument {
        self.into_html_document()
    }

    fn parse_error(&self, _msg: std::borrow::Cow<'static, str>) {}

    fn get_document(&self) -> Handle {
        self.document.clone()
    }

    fn elem_name<'a>(&'a self, target: &'a Handle) -> ExpandedName<'a> {
        match target.element_name {
            Some(ref name) => name.expanded(),
            None => panic!("elem_name called on non-element node"),
        }
    }

    fn create_element(
        &self,
        name: QualName,
        attrs: Vec<Attribute>,
        _flags: ElementFlags,
    ) -> Handle {
        Rc::new(DomNode {
            parent: Cell::new(None),
            children: RefCell::new(Vec::new()),
            element_name: Some(name),
            element_attrs: RefCell::new(attrs),
            text_content: RefCell::new(None),
            _comment_content: RefCell::new(None),
            is_document: false,
        })
    }

    fn create_comment(&self, text: StrTendril) -> Handle {
        Rc::new(DomNode {
            parent: Cell::new(None),
            children: RefCell::new(Vec::new()),
            element_name: None,
            element_attrs: RefCell::new(Vec::new()),
            text_content: RefCell::new(None),
            _comment_content: RefCell::new(Some(text)),
            is_document: false,
        })
    }

    fn create_pi(&self, _target: StrTendril, _data: StrTendril) -> Handle {
        Rc::new(DomNode {
            parent: Cell::new(None),
            children: RefCell::new(Vec::new()),
            element_name: None,
            element_attrs: RefCell::new(Vec::new()),
            text_content: RefCell::new(None),
            _comment_content: RefCell::new(Some(_data)),
            is_document: false,
        })
    }

    fn append(&self, parent: &Handle, child: NodeOrText<Handle>) {
        let child = match child {
            NodeOrText::AppendNode(node) => node,
            NodeOrText::AppendText(text) => {
                // Clone the last child handle if it's a text node we can merge with
                let last_handle = {
                    let children = parent.children.borrow();
                    children.last().and_then(|last| {
                        if last.element_name.is_none() && !last.is_document {
                            Some(Rc::clone(last))
                        } else {
                            None
                        }
                    })
                };
                if let Some(last) = last_handle {
                    let mut last_text = last.text_content.borrow_mut();
                    if let Some(ref mut existing) = *last_text {
                        existing.push_slice(&text);
                        return;
                    }
                }
                Rc::new(DomNode {
                    parent: Cell::new(None),
                    children: RefCell::new(Vec::new()),
                    element_name: None,
                    element_attrs: RefCell::new(Vec::new()),
                    text_content: RefCell::new(Some(text)),
                    _comment_content: RefCell::new(None),
                    is_document: false,
                })
            }
        };
        child.parent.set(Some(Rc::downgrade(parent)));
        parent.children.borrow_mut().push(child);
    }

    fn append_based_on_parent_node(
        &self,
        _element: &Handle,
        prev_element: &Handle,
        child: NodeOrText<Handle>,
    ) {
        self.append(prev_element, child);
    }

    fn append_doctype_to_document(
        &self,
        _name: StrTendril,
        _public_id: StrTendril,
        _system_id: StrTendril,
    ) {
        // Ignore doctype
    }

    fn get_template_contents(&self, target: &Handle) -> Handle {
        target.clone()
    }

    fn same_node(&self, x: &Handle, y: &Handle) -> bool {
        Rc::ptr_eq(x, y)
    }

    fn set_quirks_mode(&self, _mode: QuirksMode) {}

    fn append_before_sibling(&self, sibling: &Handle, new_node: NodeOrText<Handle>) {
        // Find the parent and insert before the sibling
        if let Some(parent_weak) = sibling.parent.take() {
            if let Some(parent) = parent_weak.upgrade() {
                sibling.parent.set(Some(parent_weak));
                let child = match new_node {
                    NodeOrText::AppendNode(node) => node,
                    NodeOrText::AppendText(text) => Rc::new(DomNode {
                        parent: Cell::new(None),
                        children: RefCell::new(Vec::new()),
                        element_name: None,
                        element_attrs: RefCell::new(Vec::new()),
                        text_content: RefCell::new(Some(text)),
                        _comment_content: RefCell::new(None),
                        is_document: false,
                    }),
                };
                child.parent.set(Some(Rc::downgrade(&parent)));
                let pos = parent
                    .children
                    .borrow()
                    .iter()
                    .position(|c| Rc::ptr_eq(c, sibling));
                if let Some(pos) = pos {
                    parent.children.borrow_mut().insert(pos, child);
                } else {
                    parent.children.borrow_mut().push(child);
                }
            } else {
                sibling.parent.set(Some(parent_weak));
                // Parent is gone, just append to orphan sibling as fallback
                let child = match new_node {
                    NodeOrText::AppendNode(node) => node,
                    NodeOrText::AppendText(text) => Rc::new(DomNode {
                        parent: Cell::new(None),
                        children: RefCell::new(Vec::new()),
                        element_name: None,
                        element_attrs: RefCell::new(Vec::new()),
                        text_content: RefCell::new(Some(text)),
                        _comment_content: RefCell::new(None),
                        is_document: false,
                    }),
                };
                child.parent.set(None);
                sibling.children.borrow_mut().push(child);
            }
        }
    }

    fn add_attrs_if_missing(&self, target: &Handle, attrs: Vec<Attribute>) {
        let mut existing = target.element_attrs.borrow_mut();
        for attr in attrs {
            if !existing.iter().any(|a| a.name == attr.name) {
                existing.push(attr);
            }
        }
    }

    fn remove_from_parent(&self, target: &Handle) {
        if let Some(parent_weak) = target.parent.take()
            && let Some(parent) = parent_weak.upgrade()
        {
            parent
                .children
                .borrow_mut()
                .retain(|c| !Rc::ptr_eq(c, target));
        }
    }

    fn reparent_children(&self, node: &Handle, new_parent: &Handle) {
        let children = std::mem::take(&mut *node.children.borrow_mut());
        for child in children {
            child.parent.set(Some(Rc::downgrade(new_parent)));
            new_parent.children.borrow_mut().push(child);
        }
    }
}

// ─── Public API ────────────────────────────────────────────

/// 将 HTML 字符串解析为 HtmlDocument(完整文档模式)
pub fn parse_html(html: &str) -> HtmlDocument {
    let opts = ParseOpts {
        tree_builder: TreeBuilderOpts {
            drop_doctype: true,
            ..Default::default()
        },
        ..Default::default()
    };

    parse_document(DomSink::new(), opts)
        .from_utf8()
        .read_from(&mut html.as_bytes())
        .expect("html5ever parse failed")
}

/// 将 HTML 字符串解析为 HtmlDocument(明确文档语义的别名)
pub fn parse_html_document(html: &str) -> HtmlDocument {
    parse_html(html)
}

/// 解析 HTML 片段,返回顶级节点列表
pub fn parse_html_fragment(html: &str) -> Vec<HtmlNode> {
    let opts = ParseOpts {
        tree_builder: TreeBuilderOpts {
            ..Default::default()
        },
        ..Default::default()
    };

    let doc: HtmlDocument = parse_fragment(
        DomSink::new(),
        opts,
        QualName::new(None, html5ever::ns!(html), LocalName::from("body")),
        vec![],
        false,
    )
    .from_utf8()
    .read_from(&mut html.as_bytes())
    .expect("html5ever parse_fragment failed");

    // Extract children from the parsed document
    if let Some(body) = doc.root.find(HtmlTag::Body) {
        body.children.clone()
    } else {
        doc.root.children.clone()
    }
}

// ─── Helpers ───────────────────────────────────────────────

/// 从 HTML 树中提取所有 <style> 标签内容
fn extract_style_sheets(root: &HtmlElement) -> Vec<String> {
    let mut sheets = Vec::new();
    collect_styles(root, &mut sheets);
    sheets
}

fn collect_styles(element: &HtmlElement, sheets: &mut Vec<String>) {
    if element.tag == HtmlTag::Style {
        for child in &element.children {
            sheets.push(child.text_content());
        }
        return;
    }
    for child in &element.children {
        if let HtmlNode::Element(elem) = child {
            collect_styles(elem, sheets);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_simple_html() {
        let html = "<p>Hello <strong>world</strong></p>";
        let doc = parse_html(html);
        assert!(!doc.root.children.is_empty());
    }

    #[test]
    fn test_parse_style_extraction() {
        let html = "<style>h1 { color: red; }</style><h1>Hello</h1>";
        let doc = parse_html(html);
        assert_eq!(doc.style_sheets.len(), 1);
        assert!(doc.style_sheets[0].contains("color: red"));
    }

    #[test]
    fn test_element_classes() {
        let html = r#"<div class="foo bar">content</div>"#;
        let doc = parse_html(html);
        if let Some(elem) = doc.root.find(HtmlTag::Div) {
            let classes = elem.classes();
            assert_eq!(classes.len(), 2);
            assert!(classes.contains(&"foo".to_string()));
            assert!(classes.contains(&"bar".to_string()));
        } else {
            panic!("Div not found");
        }
    }

    #[test]
    fn test_parse_fragment_basic() {
        let nodes = parse_html_fragment("<p>Hello</p><span>World</span>");
        assert_eq!(nodes.len(), 2);
    }

    #[test]
    fn test_parse_empty() {
        let doc = parse_html("");
        // html5ever 会为空白输入生成 <html><head></head><body></body></html>
        assert_eq!(doc.root.tag, HtmlTag::Html, "Expected Html element root");
        assert!(!doc.root.children.is_empty(), "Should have head/body");
    }
}