aboutsummaryrefslogtreecommitdiff
path: root/parse_wiki_text/src/state.rs
blob: 34d7a9c4429797c07eccf4b355c2ac6102e9b6b2 (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
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
// Copyright 2019 Fredrik Portström <https://portstrom.com>
// This is free software distributed under the terms specified in
// the file LICENSE at the top-level directory of this distribution.

pub struct OpenNode<'a> {
    pub nodes: Vec<crate::Node<'a>>,
    pub start: usize,
    pub type_: OpenNodeType<'a>,
}

pub enum OpenNodeType<'a> {
    DefinitionList {
        items: Vec<crate::DefinitionListItem<'a>>,
    },
    ExternalLink,
    Heading {
        level: u8,
    },
    Link {
        namespace: Option<crate::Namespace>,
        target: &'a str,
    },
    OrderedList {
        items: Vec<crate::ListItem<'a>>,
    },
    Parameter {
        default: Option<Vec<crate::Node<'a>>>,
        name: Option<Vec<crate::Node<'a>>>,
    },
    Preformatted,
    Table(Table<'a>),
    Tag {
        name: crate::Cow<'a, str>,
    },
    Template {
        name: Option<Vec<crate::Node<'a>>>,
        parameters: Vec<crate::Parameter<'a>>,
    },
    UnorderedList {
        items: Vec<crate::ListItem<'a>>,
    },
}

pub struct State<'a> {
    pub flushed_position: usize,
    pub nodes: Vec<crate::Node<'a>>,
    pub scan_position: usize,
    pub stack: Vec<OpenNode<'a>>,
    pub warnings: Vec<crate::Warning>,
    pub wiki_text: &'a str,
}

pub struct Table<'a> {
    pub attributes: Vec<crate::Node<'a>>,
    pub before: Vec<crate::Node<'a>>,
    pub captions: Vec<crate::TableCaption<'a>>,
    pub child_element_attributes: Option<Vec<crate::Node<'a>>>,
    pub rows: Vec<crate::TableRow<'a>>,
    pub start: usize,
    pub state: TableState,
}

pub enum TableState {
    Before,
    CaptionFirstLine,
    CaptionRemainder,
    CellFirstLine,
    CellRemainder,
    HeadingFirstLine,
    HeadingRemainder,
    Row,
    TableAttributes,
}

impl<'a> State<'a> {
    pub fn flush(&mut self, end_position: usize) {
        flush(
            &mut self.nodes,
            self.flushed_position,
            end_position,
            self.wiki_text,
        );
    }

    pub fn get_byte(&self, position: usize) -> Option<u8> {
        self.wiki_text.as_bytes().get(position).cloned()
    }

    pub fn push_open_node(&mut self, type_: OpenNodeType<'a>, inner_start_position: usize) {
        let scan_position = self.scan_position;
        self.flush(scan_position);
        self.stack.push(OpenNode {
            nodes: std::mem::replace(&mut self.nodes, vec![]),
            start: scan_position,
            type_,
        });
        self.scan_position = inner_start_position;
        self.flushed_position = inner_start_position;
    }

    pub fn rewind(&mut self, nodes: Vec<crate::Node<'a>>, position: usize) {
        self.scan_position = position + 1;
        self.nodes = nodes;
        if let Some(position_before_text) = match self.nodes.last() {
            Some(crate::Node::Text { start, .. }) => Some(*start),
            _ => None,
        } {
            self.nodes.pop();
            self.flushed_position = position_before_text;
        } else {
            self.flushed_position = position;
        }
    }

    pub fn skip_empty_lines(&mut self) {
        match self.stack.last() {
            Some(OpenNode {
                type_: OpenNodeType::Table { .. },
                ..
            }) => {
                self.scan_position -= 1;
                crate::table::parse_table_end_of_line(self, false);
            }
            _ => {
                crate::line::parse_beginning_of_line(self, None);
            }
        }
    }

    pub fn skip_whitespace_backwards(&self, position: usize) -> usize {
        skip_whitespace_backwards(self.wiki_text, position)
    }

    pub fn skip_whitespace_forwards(&self, position: usize) -> usize {
        skip_whitespace_forwards(self.wiki_text, position)
    }
}

pub fn flush<'a>(
    nodes: &mut Vec<crate::Node<'a>>,
    flushed_position: usize,
    end_position: usize,
    wiki_text: &'a str,
) {
    if end_position > flushed_position {
        nodes.push(crate::Node::Text {
            end: end_position,
            start: flushed_position,
            value: &wiki_text[flushed_position..end_position],
        });
    }
}

pub fn skip_whitespace_backwards(wiki_text: &str, mut position: usize) -> usize {
    while position > 0
        && match wiki_text.as_bytes()[position - 1] {
            b'\t' | b'\n' | b' ' => true,
            _ => false,
        }
    {
        position -= 1;
    }
    position
}

pub fn skip_whitespace_forwards(wiki_text: &str, mut position: usize) -> usize {
    while match wiki_text.as_bytes().get(position).cloned() {
        Some(b'\t') | Some(b'\n') | Some(b' ') => true,
        _ => false,
    } {
        position += 1;
    }
    position
}