From ce8cb5aad063b77b83b8a785393278453882994f Mon Sep 17 00:00:00 2001 From: Natty Date: Sat, 30 Dec 2023 20:02:25 +0100 Subject: [PATCH] MMM: More forgiving math parsing --- magnetar_mmm_parser/src/lib.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/magnetar_mmm_parser/src/lib.rs b/magnetar_mmm_parser/src/lib.rs index 215c439..a862acc 100644 --- a/magnetar_mmm_parser/src/lib.rs +++ b/magnetar_mmm_parser/src/lib.rs @@ -986,14 +986,13 @@ impl Context { let (input, _) = start(input)?; let (input, _) = opt(line_ending)(input)?; + let (input, _) = opt(space1_unicode)(input)?; - let (input, math_span) = recognize(many1_count(tuple(( - not(tuple((opt(line_ending), end))), - not_line_ending, - ))))(input)?; + let (input, math_span) = map( + many_till(anychar, tuple((opt(space1_unicode), opt(line_ending), end))), + |v| v.0, + )(input)?; - let (input, _) = opt(line_ending)(input)?; - let (input, _) = end(input)?; // Trailing whitespace after the closing delim let (input, _) = opt(space1_unicode)(input)?; // If we got this far, the next character should be a line ending @@ -1002,7 +1001,7 @@ impl Context { Ok(( input, - Token::BlockMath(math_span.into_fragment().to_string()), + Token::BlockMath(math_span.into_iter().collect::()), )) } @@ -1926,6 +1925,11 @@ a^2 + b^2 = c^2 Token::BlockMath("a^2 + b^2 = c^2".to_string()) ); + assert_eq!( + parse_full(r"\[ x^2 + y^2 = z^2 \]"), + Token::BlockMath("x^2 + y^2 = z^2".to_string()) + ); + assert_eq!( parse_full( r#"
centered