summaryrefslogtreecommitdiff
path: root/src/parse_heirarchical.rs
blob: 75c2bec85bc0733ee4d9d44b3a3192e9f284fda9 (plain) (blame)
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
use crate::*;

macro_rules! get_subsection {
    ($t:ident) => {
        pub fn get_subsection(&self, name: &str) -> Option<&$t> {
            for section in &self.sections {
                if line_to_string(&section.title) == name {
                    return Some(section);
                }
            }
            return None;
        }
    };
}

#[derive(Default)]
pub struct Document {
    pub preamble: Vec<Block>,
    pub sections: Vec<TopLevelSection>,
}
impl Document {
    get_subsection! {TopLevelSection}
}

#[derive(Default)]
pub struct TopLevelSection {
    pub title: Line,
    pub content: Vec<Block>,
    pub sections: Vec<MidLevelSection>,
}
impl TopLevelSection {
    get_subsection! {MidLevelSection}
}

#[derive(Default)]
pub struct MidLevelSection {
    pub title: Line,
    pub content: Vec<Block>,
    pub sections: Vec<LowLevelSection>,
}
impl MidLevelSection {
    get_subsection! {LowLevelSection}
}

#[derive(Default)]
pub struct LowLevelSection {
    pub title: Line,
    pub content: Vec<Block>,
}

pub fn parse_heirarchical(markdown: &str) -> Result<Document, ()> {
    macro_rules! push_section {
        ($from:ident => $to:ident) => {
            $to.sections.push(std::mem::take(&mut $from))
        };
    }
    let mut document = Document::default();
    let mut h1_buffer = TopLevelSection::default();
    let mut h2_buffer = MidLevelSection::default();
    let mut h3_buffer = LowLevelSection::default();
    let mut level = 0;

    let blocks = parse(markdown);
    for block in blocks {
        match (level, block) {
            (0, Block::Heading1(title)) => {
                h1_buffer.title = title;
                level = 1;
            }
            (0, Block::Heading2(_)) => return Err(()),
            (0, Block::Heading3(_)) => return Err(()),
            (0, block) => document.preamble.push(block),
            (1, Block::Heading1(title)) => {
                push_section!(h1_buffer => document);
                h1_buffer.title = title;
            }
            (1, Block::Heading2(title)) => {
                h2_buffer.title = title;
                level = 2;
            }
            (1, Block::Heading3(_)) => return Err(()),
            (1, block) => h1_buffer.content.push(block),
            (2, Block::Heading1(title)) => {
                push_section!(h2_buffer => h1_buffer);
                push_section!(h1_buffer => document);
                h1_buffer.title = title;
                level = 1;
            }
            (2, Block::Heading2(title)) => {
                push_section!(h2_buffer => h1_buffer);
                h2_buffer.title = title;
            }
            (2, Block::Heading3(title)) => {
                h3_buffer.title = title;
                level = 3;
            }
            (2, block) => h2_buffer.content.push(block),
            (3, Block::Heading1(title)) => {
                push_section!(h3_buffer => h2_buffer);
                push_section!(h2_buffer => h1_buffer);
                push_section!(h1_buffer => document);
                h1_buffer.title = title;
                level = 1;
            }
            (3, Block::Heading2(title)) => {
                push_section!(h3_buffer => h2_buffer);
                push_section!(h2_buffer => h1_buffer);
                h2_buffer.title = title;
                level = 2;
            }
            (3, Block::Heading3(title)) => {
                push_section!(h3_buffer => h2_buffer);
                h3_buffer.title = title;
            }
            (3, block) => h3_buffer.content.push(block),
            _ => unreachable!(),
        }
    }

    // Push all in-progress sections
    match level {
        3 => {
            push_section!(h3_buffer => h2_buffer);
            push_section!(h2_buffer => h1_buffer);
            push_section!(h1_buffer => document);
        }
        2 => {
            push_section!(h2_buffer => h1_buffer);
            push_section!(h1_buffer => document);
        }
        1 => {
            push_section!(h1_buffer => document);
        }
        _ => (),
    }
    Ok(document)
}