Parse soft newlines and escaped closing brackets in propvals

This commit is contained in:
Savanni D'Gerinel 2023-06-28 19:16:18 -04:00
parent fd4c6ff935
commit 12f4f9dde6
1 changed files with 30 additions and 13 deletions

View File

@ -69,8 +69,8 @@
use nom::{ use nom::{
branch::alt, branch::alt,
bytes::complete::{escaped_transform, is_not, tag}, bytes::complete::{escaped, escaped_transform, is_not, tag},
character::complete::{alpha1, digit1, multispace0, multispace1}, character::complete::{alpha1, digit1, multispace0, multispace1, none_of, one_of},
combinator::{opt, value}, combinator::{opt, value},
multi::{many0, many1, separated_list1}, multi::{many0, many1, separated_list1},
sequence::delimited, sequence::delimited,
@ -395,9 +395,13 @@ fn parse_propval_text<'a, E: nom::error::ParseError<&'a str>>(
input: &'a str, input: &'a str,
) -> IResult<&'a str, Option<String>, E> { ) -> IResult<&'a str, Option<String>, E> {
let (input, value) = opt(escaped_transform( let (input, value) = opt(escaped_transform(
is_not(r"\]"), none_of("\\]"),
'\\', '\\',
value("]", tag(r"\]")), // alt((value("]", tag(r"\]")), value("", tag("\\\n")))), alt((
value("]", tag("]")),
value("\\", tag("\\")),
value("", tag("\n")),
)),
))(input)?; ))(input)?;
Ok((input, value.map(|v| v.to_owned()))) Ok((input, value.map(|v| v.to_owned())))
} }
@ -635,29 +639,42 @@ mod tests {
#[test] #[test]
fn it_parses_propvals_with_hard_linebreaks() { fn it_parses_propvals_with_hard_linebreaks() {
let (_, propval) = parse_propval::<nom::error::VerboseError<&str>>( let (_, propval) = parse_propval_text::<nom::error::VerboseError<&str>>(
"[There are hard linebreaks & soft linebreaks. "There are hard linebreaks & soft linebreaks.
Soft linebreaks...]", Soft linebreaks...",
) )
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
propval, propval,
"There are hard linebreaks & soft linebreaks. Some(
"There are hard linebreaks & soft linebreaks.
Soft linebreaks..." Soft linebreaks..."
.to_owned() .to_owned()
)
);
}
#[test]
fn it_parses_propvals_with_escaped_closing_brackets() {
let (_, propval) =
parse_propval_text::<nom::error::VerboseError<&str>>(r"escaped closing \] bracket")
.unwrap();
assert_eq!(
propval,
Some(r"escaped closing ] bracket".to_owned()).to_owned()
); );
} }
#[test] #[test]
fn it_parses_propvals_with_soft_linebreaks() { fn it_parses_propvals_with_soft_linebreaks() {
let (_, propval) = parse_propval::<nom::error::VerboseError<&str>>( let (_, propval) = parse_propval_text::<nom::error::VerboseError<&str>>(
r"[Soft linebreaks are linebreaks preceeded by '\\' like this one >o\ r"Soft linebreaks are linebreaks preceeded by '\\' like this one >o\
k<. Hard line breaks are all other linebreaks.]", k<. Hard line breaks are all other linebreaks.",
) )
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
propval, propval,
r"Soft linebreaks are linebreaks preceeded by '\\' like this one >ok<. Hard line breaks are all other linebreaks." Some("Soft linebreaks are linebreaks preceeded by '\\' like this one >ok<. Hard line breaks are all other linebreaks.".to_owned())
.to_owned() .to_owned()
); );
} }