markx/
html.rs

1#![allow(unused)]
2
3use crate::block::*;
4use crate::inline::{parse_inline, Span};
5
6pub fn mark2html(input: &String) -> String {
7    let mut html = String::from("");
8    let mut ul = String::from("");
9    let blocks = parse_doc(input);
10
11    if blocks.is_empty() {
12        html = format!("");
13    } else {
14        for block in blocks {
15            match block {
16                Block::LeafBlock(leaf) => match leaf {
17                    LeafBlock::ListItem(list) => {
18                        ul = format!("{}{}", ul, list.tohtml());
19                    }
20                    LeafBlock::Paragraph(leaf) => {
21                        if ul.is_empty() {
22                            html = format!("{}{}", html, leaf.tohtml());
23                        } else {
24                            html = format!("{}<ul>{}</ul>{}", html, ul, leaf.tohtml());
25                            ul.clear();
26                        }
27                    }
28                    LeafBlock::DisplayMath(leaf) => {
29                        if ul.is_empty() {
30                            html = format!("{}{}", html, leaf.tohtml());
31                        } else {
32                            html = format!("{}<ul>{}</ul>{}", html, ul, leaf.tohtml());
33                            ul.clear();
34                        }
35                    }
36                    LeafBlock::FencedCode(leaf) => {
37                        if ul.is_empty() {
38                            html = format!("{}{}", html, leaf.tohtml());
39                        } else {
40                            html = format!("{}<ul>{}</ul>{}", html, ul, leaf.tohtml());
41                            ul.clear();
42                        }
43                    }
44                    LeafBlock::Heading(leaf) => {
45                        if ul.is_empty() {
46                            html = format!("{}{}", html, leaf.tohtml());
47                        } else {
48                            html = format!("{}<ul>{}</ul>{}", html, ul, leaf.tohtml());
49                            ul.clear();
50                        }
51                    }
52                },
53                Block::ContainerBlock(c) => {
54                    html = format!("{}{}", html, c.tohtml());
55                }
56            }
57        }
58
59        // gather last ul
60        if !ul.is_empty() {
61            html = format!("{}<ul>{}</ul>", html, ul);
62        }
63    }
64
65    html
66}
67
68#[cfg(test)]
69mod tests {
70    use super::*;
71    use std::fs;
72
73    #[test]
74    fn test_html() {
75        // run with cargo test -- --nocapture
76        let input = fs::read_to_string("tests/test4.md").unwrap();
77
78        let html = mark2html(&input);
79        println!("{:?}", html);
80    }
81}