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
|
// 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 fn parse_beginning_of_line(state: &mut crate::State, line_start_position: Option<usize>) {
let mut has_line_break = false;
'a: loop {
match state.get_byte(state.scan_position) {
None => {
if line_start_position.is_none() {
state.flushed_position = state.scan_position;
}
return;
}
Some(b'\t') => {
state.scan_position += 1;
loop {
match state.get_byte(state.scan_position) {
None | Some(b'\n') => continue 'a,
Some(b'\t') | Some(b' ') => state.scan_position += 1,
Some(_) => break 'a,
}
}
}
Some(b'\n') => {
if has_line_break {
state.warnings.push(crate::Warning {
end: state.scan_position + 1,
message: crate::WarningMessage::RepeatedEmptyLine,
start: state.scan_position,
});
}
has_line_break = true;
state.scan_position += 1;
}
Some(b' ') => {
state.scan_position += 1;
let start_position = state.scan_position;
loop {
match state.get_byte(state.scan_position) {
None => return,
Some(b'\n') => break,
Some(b'\t') | Some(b' ') => state.scan_position += 1,
Some(b'{') if state.get_byte(state.scan_position + 1) == Some(b'|') => {
crate::table::start_table(state, line_start_position);
return;
}
Some(_) => {
if let Some(position) = line_start_position {
let position = state.skip_whitespace_backwards(position);
state.flush(position);
}
state.flushed_position = state.scan_position;
state.push_open_node(crate::OpenNodeType::Preformatted, start_position);
return;
}
}
}
}
Some(b'#') | Some(b'*') | Some(b':') | Some(b';') => {
if let Some(position) = line_start_position {
let position = state.skip_whitespace_backwards(position);
state.flush(position);
}
state.flushed_position = state.scan_position;
while crate::list::parse_list_item_start(state) {}
crate::list::skip_spaces(state);
return;
}
Some(b'-') => {
if state.get_byte(state.scan_position + 1) == Some(b'-')
&& state.get_byte(state.scan_position + 2) == Some(b'-')
&& state.get_byte(state.scan_position + 3) == Some(b'-')
{
if let Some(position) = line_start_position {
let position = state.skip_whitespace_backwards(position);
state.flush(position);
}
let start = state.scan_position;
state.scan_position += 4;
while state.get_byte(state.scan_position) == Some(b'-') {
state.scan_position += 1;
}
state.nodes.push(crate::Node::HorizontalDivider {
end: state.scan_position,
start,
});
while let Some(character) = state.get_byte(state.scan_position) {
match character {
b'\t' | b' ' => state.scan_position += 1,
b'\n' => {
state.scan_position += 1;
state.skip_empty_lines();
break;
}
_ => break,
}
}
state.flushed_position = state.scan_position;
return;
}
break;
}
Some(b'=') => {
if let Some(position) = line_start_position {
let position = state.skip_whitespace_backwards(position);
state.flush(position);
}
crate::heading::parse_heading_start(state);
return;
}
Some(b'{') => {
if state.get_byte(state.scan_position + 1) == Some(b'|') {
crate::table::start_table(state, line_start_position);
return;
}
break;
}
Some(_) => break,
}
}
match line_start_position {
None => state.flushed_position = state.scan_position,
Some(position) => {
if has_line_break {
let flush_position = state.skip_whitespace_backwards(position);
state.flush(flush_position);
state.nodes.push(crate::Node::ParagraphBreak {
end: state.scan_position,
start: position,
});
state.flushed_position = state.scan_position;
}
}
}
}
pub fn parse_end_of_line(state: &mut crate::State) {
match state.stack.last() {
None => {
let position = state.scan_position;
state.scan_position += 1;
parse_beginning_of_line(state, Some(position));
}
Some(crate::OpenNode {
type_: crate::OpenNodeType::DefinitionList { .. },
..
})
| Some(crate::OpenNode {
type_: crate::OpenNodeType::OrderedList { .. },
..
})
| Some(crate::OpenNode {
type_: crate::OpenNodeType::UnorderedList { .. },
..
}) => {
crate::list::parse_list_end_of_line(state);
}
Some(crate::OpenNode {
type_: crate::OpenNodeType::ExternalLink { .. },
..
}) => {
crate::external_link::parse_external_link_end_of_line(state);
}
Some(crate::OpenNode {
type_: crate::OpenNodeType::Heading { .. },
..
}) => {
crate::heading::parse_heading_end(state);
}
Some(crate::OpenNode {
type_: crate::OpenNodeType::Link { .. },
..
})
| Some(crate::OpenNode {
type_: crate::OpenNodeType::Parameter { .. },
..
})
| Some(crate::OpenNode {
type_: crate::OpenNodeType::Tag { .. },
..
})
| Some(crate::OpenNode {
type_: crate::OpenNodeType::Template { .. },
..
}) => {
state.scan_position += 1;
}
Some(crate::OpenNode {
type_: crate::OpenNodeType::Preformatted,
..
}) => {
parse_preformatted_end_of_line(state);
}
Some(crate::OpenNode {
type_: crate::OpenNodeType::Table { .. },
..
}) => {
crate::table::parse_table_end_of_line(state, true);
}
}
}
fn parse_preformatted_end_of_line(state: &mut crate::State) {
if state.get_byte(state.scan_position + 1) == Some(b' ') {
let mut position = state.scan_position + 2;
loop {
match state.get_byte(position) {
None => break,
Some(b'\t') | Some(b' ') => position += 1,
Some(b'{') if state.get_byte(position + 1) == Some(b'|') => {
break;
}
Some(b'|')
if state.get_byte(position + 1) == Some(b'}')
&& state.stack.len() > 1
&& match state.stack.get(state.stack.len() - 2) {
Some(crate::OpenNode {
type_: crate::OpenNodeType::Table { .. },
..
}) => true,
_ => false,
} =>
{
break;
}
Some(_) => {
let position = state.scan_position + 1;
state.flush(position);
state.scan_position += 2;
state.flushed_position = state.scan_position;
return;
}
}
}
}
let open_node = state.stack.pop().unwrap();
let position = state.skip_whitespace_backwards(state.scan_position);
state.flush(position);
state.scan_position += 1;
let nodes = std::mem::replace(&mut state.nodes, open_node.nodes);
state.nodes.push(crate::Node::Preformatted {
end: state.scan_position,
nodes,
start: open_node.start,
});
state.skip_empty_lines();
}
|