From a2e04ff18ad27be4dc1c66079941baaec79e003f Mon Sep 17 00:00:00 2001 From: JJ Date: Wed, 4 Jan 2023 15:57:41 -0800 Subject: Copy the last version of the parse_wiki_text crate in for development --- parse_wiki_text/src/redirect.rs | 86 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 parse_wiki_text/src/redirect.rs (limited to 'parse_wiki_text/src/redirect.rs') 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 +// 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; + } +} -- cgit v1.2.3-70-g09d2