use super::Span;
use nom::{
bytes::complete::{tag, take_until},
IResult,
};
pub fn inline_math(input: Span) -> IResult<Span, Span> {
log::debug!("Parsing inline math at: {:?}", input.fragment());
let (input, _) = tag("$")(input)?;
let content_str = input.fragment();
if content_str.starts_with('$') {
return Err(nom::Err::Error(nom::error::Error::new(
input,
nom::error::ErrorKind::Tag,
)));
}
let (input, content) = take_until("$")(input)?;
let (input, _) = tag("$")(input)?;
log::debug!("Inline math content: {:?}", content.fragment());
Ok((input, content))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn smoke_test_inline_math_basic() {
let input = Span::new("$E = mc^2$ text");
let result = inline_math(input);
assert!(result.is_ok());
let (rest, content) = result.unwrap();
assert_eq!(*content.fragment(), "E = mc^2");
assert_eq!(*rest.fragment(), " text");
}
#[test]
fn smoke_test_inline_math_with_special_chars() {
let input = Span::new("$\\frac{a}{b}$");
let result = inline_math(input);
assert!(result.is_ok());
let (_, content) = result.unwrap();
assert_eq!(*content.fragment(), "\\frac{a}{b}");
}
#[test]
fn smoke_test_inline_math_not_display() {
let input = Span::new("$$x^2$$");
let result = inline_math(input);
assert!(result.is_err()); }
}