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
|
// 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_link_end<'a>(
state: &mut crate::State<'a>,
configuration: &crate::Configuration,
start_position: usize,
nodes: Vec<crate::Node<'a>>,
namespace: Option<crate::Namespace>,
target: &'a str,
) {
let inner_end_position = state.skip_whitespace_backwards(state.scan_position);
state.flush(inner_end_position);
state.scan_position += 2;
state.flushed_position = state.scan_position;
let mut text = std::mem::replace(&mut state.nodes, nodes);
let end = state.scan_position;
let start = start_position;
state.nodes.push(match namespace {
None => {
let mut trail_end_position = end;
for character in state.wiki_text[end..].chars() {
if !configuration.link_trail_character_set.contains(&character) {
break;
}
trail_end_position += character.len_utf8();
}
if trail_end_position > end {
text.push(crate::Node::Text {
end: trail_end_position,
start: end,
value: &state.wiki_text[end..trail_end_position],
});
}
crate::Node::Link {
end: trail_end_position,
start,
target,
text,
}
}
Some(crate::Namespace::Category) => crate::Node::Category {
end,
ordinal: text,
start,
target,
},
Some(crate::Namespace::File) => crate::Node::Image {
end,
start,
target,
text,
},
});
}
pub fn parse_link_start(state: &mut crate::State, configuration: &crate::Configuration) {
if match state.stack.last() {
Some(crate::OpenNode {
type_: crate::OpenNodeType::Link { namespace, .. },
..
}) => *namespace != Some(crate::Namespace::File),
_ => false,
} {
let open_node = state.stack.pop().unwrap();
state.warnings.push(crate::Warning {
end: state.scan_position,
message: crate::WarningMessage::InvalidLinkSyntax,
start: open_node.start,
});
state.rewind(open_node.nodes, open_node.start);
return;
}
let mut target_end_position;
let target_start_position = state.skip_whitespace_forwards(state.scan_position + 2);
let namespace = match configuration
.namespaces
.find(&state.wiki_text[target_start_position..])
{
Err(match_length) => {
target_end_position = match_length + target_start_position;
None
}
Ok((match_length, namespace)) => {
target_end_position = match_length + target_start_position;
Some(namespace)
}
};
loop {
match state.get_byte(target_end_position) {
None | Some(b'\n') | Some(b'[') | Some(b'{') | Some(b'}') => {
parse_unexpected_end(state, target_end_position);
break;
}
Some(b']') => {
parse_end(
state,
configuration,
target_start_position,
target_end_position,
namespace,
);
break;
}
Some(b'|') => {
state.push_open_node(
crate::OpenNodeType::Link {
namespace,
target: &state.wiki_text[target_start_position..target_end_position],
},
target_end_position + 1,
);
break;
}
_ => target_end_position += 1,
}
}
}
fn parse_end(
state: &mut crate::State,
configuration: &crate::Configuration,
target_start_position: usize,
target_end_position: usize,
namespace: Option<crate::Namespace>,
) {
if state.get_byte(target_end_position + 1) != Some(b']') {
parse_unexpected_end(state, target_end_position);
return;
}
let start_position = state.scan_position;
state.flush(start_position);
let trail_start_position = target_end_position + 2;
let mut trail_end_position = trail_start_position;
match namespace {
Some(crate::Namespace::Category) => {
state.nodes.push(crate::Node::Category {
end: trail_end_position,
ordinal: vec![],
start: state.scan_position,
target: state.wiki_text[target_start_position..target_end_position].trim_end(),
});
}
Some(crate::Namespace::File) => {
state.nodes.push(crate::Node::Image {
end: trail_end_position,
start: state.scan_position,
target: state.wiki_text[target_start_position..target_end_position].trim_end(),
text: vec![],
});
}
None => {
for character in state.wiki_text[trail_start_position..].chars() {
if !configuration.link_trail_character_set.contains(&character) {
break;
}
trail_end_position += character.len_utf8();
}
let target_text = crate::Node::Text {
end: target_end_position,
start: target_start_position,
value: &state.wiki_text[target_start_position..target_end_position],
};
let text = if trail_end_position > trail_start_position {
vec![
target_text,
crate::Node::Text {
end: trail_end_position,
start: trail_start_position,
value: &state.wiki_text[trail_start_position..trail_end_position],
},
]
} else {
vec![target_text]
};
state.nodes.push(crate::Node::Link {
end: trail_end_position,
start: state.scan_position,
target: &state.wiki_text[target_start_position..target_end_position].trim_end(),
text,
});
}
}
state.flushed_position = trail_end_position;
state.scan_position = trail_end_position;
}
fn parse_unexpected_end(state: &mut crate::State, target_end_position: usize) {
state.warnings.push(crate::Warning {
end: target_end_position,
message: crate::WarningMessage::InvalidLinkSyntax,
start: state.scan_position,
});
state.scan_position += 1;
}
|