diff --git a/magnetar_mmm_parser/src/lib.rs b/magnetar_mmm_parser/src/lib.rs index 9c859fa..919ebd7 100644 --- a/magnetar_mmm_parser/src/lib.rs +++ b/magnetar_mmm_parser/src/lib.rs @@ -1022,6 +1022,19 @@ impl Context { let FlankingDelim(closing_tag, closing_rule, ..) = closing_tag.into(); move |input| { + if let FlankingRule::Strict = opening_rule { + let (input, pre) = opt(recognize(tuple(( + alphanumeric1_unicode, + opt(tag("\\")), + &opening_tag, + peek(not(alt((space1_unicode, eof)))), + ))))(input)?; + + if let Some(pre_text) = pre { + return Ok((input, Token::PlainText(pre_text.into_fragment().into()))); + } + } + if escape { if let Ok((input_escaped, (_, mark))) = tuple((tag("\\"), &opening_tag))(input) { return Ok(( @@ -1031,14 +1044,6 @@ impl Context { } } - if let FlankingRule::Strict = opening_rule { - let (input, pre) = - opt(recognize(tuple((alphanumeric1_unicode, &opening_tag))))(input)?; - if let Some(pre_text) = pre { - return Ok((input, Token::PlainText(pre_text.into_fragment().into()))); - } - } - let begin = input; let (post_open, _) = opening_tag(input)?; @@ -1047,11 +1052,7 @@ impl Context { &closing_tag, ))(post_open); - if let Err(nom::Err::Error(nom::error::Error { - input: input_past_err, - .. - })) = res - { + if let Err(nom::Err::Error(nom::error::Error { .. })) = res { let res_fallback = tuple(( many1(tuple((not(&closing_tag), &fallback.matcher_inner))), &closing_tag, @@ -1059,8 +1060,8 @@ impl Context { if res_fallback.is_err() { return Ok(( - input_past_err, - Token::PlainText(begin.fragment_between(&input_past_err).into()), + post_open, + Token::PlainText(begin.fragment_between(&post_open).into()), )); } @@ -1727,8 +1728,6 @@ mod test { #[test] fn parse_empty() { - let ctx = Context::default(); - assert_eq!(parse_full(""), Token::Sequence(vec![])); } @@ -1833,6 +1832,14 @@ mod test { Token::PlainText("_ italic *".into()) ); + assert_eq!( + parse_full(r#"long text with a *footnote text"#), + Token::Sequence(vec![ + Token::PlainText("long text with a *footnote ".into()), + Token::Bold(Box::new(Token::PlainText("text".into()))) + ]) + ); + assert_eq!( parse_full(r#"*"italic"*"#), Token::Italic(Box::new(Token::PlainText("\"italic\"".into()))) @@ -1879,10 +1886,66 @@ mod test { assert_eq!( parse_full("~~*hello\nworld*"), - Token::PlainText("~~*hello\nworld*".into()) + Token::Sequence(vec![ + Token::PlainText("~~".into()), + Token::Italic(Box::new(Token::PlainText("hello\nworld".into()))) + ]) ) } + #[test] + fn parse_flanking() { + assert_eq!( + parse_full(r#"aaa*iii*bbb"#), + Token::Sequence(vec![ + Token::PlainText("aaa".into()), + Token::Italic(Box::new(Token::PlainText("iii".into()))), + Token::PlainText("bbb".into()) + ]) + ); + + assert_eq!( + parse_full(r#"aaa_nnn_bbb"#), + Token::PlainText("aaa_nnn_bbb".into()) + ); + + assert_eq!( + parse_full(r#"*iii*"#), + Token::Italic(Box::new(Token::PlainText("iii".into()))) + ); + + assert_eq!( + parse_full(r#"_iii_"#), + Token::Italic(Box::new(Token::PlainText("iii".into()))) + ); + + assert_eq!( + parse_full(r#"aaa*iii*"#), + Token::Sequence(vec![ + Token::PlainText("aaa".into()), + Token::Italic(Box::new(Token::PlainText("iii".into()))), + ]) + ); + + assert_eq!( + parse_full(r#"*iii*bbb"#), + Token::Sequence(vec![ + Token::Italic(Box::new(Token::PlainText("iii".into()))), + Token::PlainText("bbb".into()), + ]) + ); + + assert_eq!( + parse_full(r#"aaa_nnn_"#), + Token::PlainText("aaa_nnn_".into()) + ); + + assert_eq!( + parse_full(r#"_nnn_bbb"#), + Token::PlainText("_nnn_bbb".into()) + ); + } + #[test] fn parse_complex() { assert_eq!(