aboutsummaryrefslogtreecommitdiff
path: root/parse_wiki_text/src/redirect.rs
blob: 6e3f4ece9c2bae4a3081eaad0e0056623a95882e (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
// 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;
    }
}