37 lines
1.0 KiB
Rust
37 lines
1.0 KiB
Rust
|
use lazy_static::lazy_static;
|
||
|
use regex::Regex;
|
||
|
|
||
|
use super::ast::DiscordComponent;
|
||
|
|
||
|
pub fn parse_code_block(text: &'_ str) -> Option<(DiscordComponent<'_>, usize)> {
|
||
|
lazy_static! {
|
||
|
static ref CODE_BLOCK: Regex =
|
||
|
Regex::new(r"^```(?P<lang>.+)?\n(?P<source>.*?)\n```").unwrap();
|
||
|
}
|
||
|
|
||
|
if let Some(caps) = CODE_BLOCK.captures(text) {
|
||
|
let lang = caps.name("lang").map(|m| m.as_str());
|
||
|
let source = caps.name("source").unwrap().as_str();
|
||
|
let whole_len = caps.get(0).unwrap().as_str().len();
|
||
|
|
||
|
return Some((DiscordComponent::CodeBlock { lang, source }, whole_len));
|
||
|
}
|
||
|
|
||
|
None
|
||
|
}
|
||
|
|
||
|
pub fn parse_code(text: &'_ str) -> Option<(DiscordComponent<'_>, usize)> {
|
||
|
lazy_static! {
|
||
|
static ref CODE: Regex = Regex::new(r"^`(?P<inner>.+?)`").unwrap();
|
||
|
}
|
||
|
|
||
|
if let Some(caps) = CODE.captures(text) {
|
||
|
let inner = caps.name("inner").unwrap().as_str();
|
||
|
let whole_len = caps.get(0).unwrap().as_str().len();
|
||
|
|
||
|
return Some((DiscordComponent::Code(inner), whole_len));
|
||
|
}
|
||
|
|
||
|
None
|
||
|
}
|