discord-message-format/src/parse_quotes.rs

50 lines
1.5 KiB
Rust

use lazy_static::lazy_static;
use regex::Regex;
use super::ast::DiscordComponent;
use super::parse_basic::parse;
pub fn parse_quotes(text: &'_ str) -> Option<(DiscordComponent<'_>, usize)> {
lazy_static! {
static ref QUOTE: Regex = Regex::new(r"^> (?P<body>.*?)(?P<endl>\n|$)").unwrap();
}
static MULTIQUOTE_PREFIX: &str = ">>> ";
if text.starts_with(MULTIQUOTE_PREFIX) {
return Some((
DiscordComponent::Quote(parse(text.strip_prefix(MULTIQUOTE_PREFIX).unwrap())),
text.len(),
));
}
if let Some(caps) = QUOTE.captures(text) {
let body = caps.name("body").unwrap().as_str();
let mut endl = caps.name("endl");
let mut whole_len = caps.get(0).unwrap().as_str().len();
let mut body_components = parse(body);
while let Some(next_line_caps) = QUOTE.captures(&text[whole_len..]) {
dbg!(&next_line_caps);
let next_line_body = next_line_caps.name("body").unwrap().as_str();
endl = next_line_caps.name("endl");
let mut next_body_components = parse(next_line_body);
body_components.push(DiscordComponent::LineBreak);
body_components.append(&mut next_body_components);
whole_len += next_line_caps.get(0).unwrap().as_str().len();
}
let endl_len = endl.map(|m| m.as_str().len()).unwrap_or(0);
return Some((
DiscordComponent::Quote(body_components),
whole_len - endl_len,
));
}
None
}