aboutsummaryrefslogtreecommitdiff
path: root/parse_wiki_text/src/magic_word.rs
blob: 40cab17c6e73229e7bba8db9baef6ad23212a908 (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
// 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_magic_word(state: &mut crate::State, configuration: &crate::Configuration) {
    if let Ok((match_length, _)) = configuration
        .magic_words
        .find(&state.wiki_text[state.scan_position + 2..])
    {
        let end_position = match_length + state.scan_position + 2;
        if state.get_byte(end_position) == Some(b'_')
            && state.get_byte(end_position + 1) == Some(b'_')
        {
            let scan_position = state.scan_position;
            state.flush(scan_position);
            state.flushed_position = end_position + 2;
            state.nodes.push(crate::Node::MagicWord {
                end: state.flushed_position,
                start: state.scan_position,
            });
            state.scan_position = state.flushed_position;
            return;
        }
    }
    state.scan_position += 1;
}