aboutsummaryrefslogtreecommitdiff
path: root/tools/src/markdown.rs
blob: af3798e6b1492d0d7a97c38058b81db723f7cb25 (plain)
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
pub struct Markdown(Vec<MdElement>);

pub enum MdElement {
    Heading(u8, Vec<RichText>),
    Paragraph(Vec<RichText>),
    Code { syntax: String, content: String },
}

pub enum RichText {
    Text(String),
    Bold(Box<RichText>),
    Strike(Box<RichText>),
    Italic(Box<RichText>),
    Code(Box<RichText>),
    Link(String, Vec<RichText>),
}

impl Markdown {
    pub fn parse(s: &str) -> anyhow::Result<Self> {
        let mut c = vec![];
        let mut lines = s.lines();
        while let Some(line) = lines.next() {
            if line.starts_with("#") {
                let (hashes, h) = line.split_once(' ').unwrap();
                c.push(MdElement::Heading(hashes.len() as u8, RichText::parse(h)?));
            } else if line.starts_with("```") {
                todo!()
            } else {
                let mut block = line.to_string();
                while let Some(line) = lines.next() {
                    if line == "" {
                        break;
                    }
                    block += line;
                }
                c.push(MdElement::Paragraph(RichText::parse(&block)?))
            }
        }
        Ok(Markdown(c))
    }
}
impl RichText {
    pub fn parse(s: &str) -> anyhow::Result<Vec<Self>> {
        let mut before = String::new();
        let mut after = String::new();
        let mut after_el = false;
        for c in s.chars() {
            match c {
                _ => {}
            }
        }
        Ok(segs)
    }
}