aboutsummaryrefslogtreecommitdiff
path: root/parse_wiki_text/src/redirect.rs
diff options
context:
space:
mode:
authorJJ2023-01-04 23:57:41 +0000
committerJJ2023-01-04 23:57:48 +0000
commita2e04ff18ad27be4dc1c66079941baaec79e003f (patch)
tree256201497d3c3ef3dba9031ee985d407b80b95a6 /parse_wiki_text/src/redirect.rs
parentbaf2f93b3002c2a0769bbd53f37d845c7717d95b (diff)
Copy the last version of the parse_wiki_text crate in for development
Diffstat (limited to 'parse_wiki_text/src/redirect.rs')
-rw-r--r--parse_wiki_text/src/redirect.rs86
1 files changed, 86 insertions, 0 deletions
diff --git a/parse_wiki_text/src/redirect.rs b/parse_wiki_text/src/redirect.rs
new file mode 100644
index 0000000..6e3f4ec
--- /dev/null
+++ b/parse_wiki_text/src/redirect.rs
@@ -0,0 +1,86 @@
+// 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_redirect(
+ state: &mut crate::State,
+ configuration: &crate::Configuration,
+ start_position: usize,
+) {
+ let mut position = match configuration
+ .redirect_magic_words
+ .find(&state.wiki_text[start_position + 1..])
+ {
+ Err(_) => return,
+ Ok((match_length, _)) => match_length + start_position + 1,
+ };
+ loop {
+ match state.get_byte(position) {
+ Some(b'\t') | Some(b'\n') | Some(b' ') => position += 1,
+ Some(b':') => {
+ position += 1;
+ loop {
+ match state.get_byte(position) {
+ Some(b'\t') | Some(b'\n') | Some(b' ') => position += 1,
+ Some(b'[') => break,
+ _ => return,
+ }
+ }
+ break;
+ }
+ Some(b'[') => break,
+ _ => return,
+ }
+ }
+ if state.get_byte(position + 1) != Some(b'[') {
+ return;
+ }
+ position += 2;
+ let target_end_position;
+ let target_start_position = position;
+ loop {
+ match state.get_byte(position) {
+ None | Some(b'\n') | Some(b'[') | Some(b'{') | Some(b'}') => return,
+ Some(b']') => {
+ target_end_position = position;
+ break;
+ }
+ Some(b'|') => {
+ state.warnings.push(crate::Warning {
+ end: position + 1,
+ message: crate::WarningMessage::UselessTextInRedirect,
+ start: position,
+ });
+ target_end_position = position;
+ position += 1;
+ loop {
+ match state.get_byte(position) {
+ None | Some(b'\n') => return,
+ Some(b']') => break,
+ Some(_) => position += 1,
+ }
+ }
+ break;
+ }
+ Some(_) => position += 1,
+ }
+ }
+ if state.get_byte(position + 1) == Some(b']') {
+ position += 2;
+ state.nodes.push(crate::Node::Redirect {
+ end: position,
+ start: start_position,
+ target: &state.wiki_text[target_start_position..target_end_position],
+ });
+ state.flushed_position = state.skip_whitespace_forwards(position);
+ state.scan_position = state.flushed_position;
+ if state.wiki_text.len() > position {
+ state.warnings.push(crate::Warning {
+ end: state.wiki_text.len(),
+ message: crate::WarningMessage::TextAfterRedirect,
+ start: start_position,
+ });
+ }
+ return;
+ }
+}