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