fn parse_markup_text()

in resctl-demo/src/doc/markup_rd.rs [165:225]


fn parse_markup_text(input: &str) -> Option<StyledString> {
    let mut parsed = StyledString::new();
    let mut chars = input.chars().peekable();
    let mut nr_stars = 0;
    let mut underline = false;
    loop {
        let (tok, next_is_space) = markup_text_next_tok(&mut chars);
        if tok.len() == 0 {
            break;
        }
        let first = tok.chars().next().unwrap();
        let last = tok.chars().last().unwrap();
        let len = tok.chars().count();
        match first {
            '*' => {
                if nr_stars == 0 && len <= 3 && !next_is_space {
                    nr_stars = len;
                    continue;
                }
                if nr_stars > 0 && nr_stars == len {
                    nr_stars = 0;
                    continue;
                }
            }
            '_' => {
                if !underline && len == 3 && !next_is_space {
                    underline = true;
                    continue;
                } else if underline && len == 3 {
                    underline = false;
                    continue;
                }
            }
            '%' => {
                if len > 2 && last == '%' {
                    match format_markup_tags(&tok[1..len - 1]) {
                        Some(text) => {
                            parsed.append(text);
                            continue;
                        }
                        None => return None,
                    }
                }
            }
            _ => {}
        }

        let mut style: Style = match nr_stars {
            1 => Effect::Bold.into(),
            2 => (*COLOR_ALERT).into(),
            3 => *STYLE_ALERT,
            _ => (*COLOR_DFL).into(),
        };
        if underline {
            style = style.combine(Effect::Underline);
        }

        parsed.append_styled(tok, style);
    }
    Some(parsed)
}