From 169bf69f5e419703757e758778f58741d8cde602 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Wed, 4 Oct 2023 13:23:57 -0400 Subject: [PATCH] Preserve the leading whitespace before an escape. --- src/compare/diff.rs | 5 ++--- src/parser/lesser_block.rs | 43 +++++++++++++++++++++++++++---------- src/types/lesser_element.rs | 15 +------------ 3 files changed, 35 insertions(+), 28 deletions(-) diff --git a/src/compare/diff.rs b/src/compare/diff.rs index 3e2e44d..938622d 100644 --- a/src/compare/diff.rs +++ b/src/compare/diff.rs @@ -1549,12 +1549,11 @@ fn compare_example_block<'b, 's>( // Compare value let contents = get_property_quoted_string(emacs, ":value")?.unwrap_or(String::new()); - let rust_contents = rust.get_contents(); - if contents != rust_contents { + if contents != rust.contents { this_status = DiffStatus::Bad; message = Some(format!( "Value mismatch (emacs != rust) {:?} != {:?}", - contents, rust_contents + contents, rust.contents )); } diff --git a/src/parser/lesser_block.rs b/src/parser/lesser_block.rs index 39a44fa..4886965 100644 --- a/src/parser/lesser_block.rs +++ b/src/parser/lesser_block.rs @@ -9,15 +9,12 @@ use nom::character::complete::space1; use nom::combinator::consumed; use nom::combinator::eof; use nom::combinator::map; -use nom::combinator::not; use nom::combinator::opt; use nom::combinator::peek; use nom::combinator::recognize; use nom::combinator::verify; -use nom::multi::many0; use nom::multi::many_till; use nom::multi::separated_list1; -use nom::sequence::preceded; use nom::sequence::tuple; use super::org_source::OrgSource; @@ -156,10 +153,7 @@ pub(crate) fn example_block<'b, 'g, 'r, 's>( let parser_context = parser_context.with_additional_node(&contexts[2]); let parameters = parameters.map(|(_, parameters)| parameters); - let (remaining, contents) = many0(preceded( - not(parser_with_context!(exit_matcher_parser)(&parser_context)), - map(content_line, Into::<&str>::into), - ))(remaining)?; + let (remaining, contents) = content(&parser_context, remaining)?; let (remaining, _end) = lesser_block_end_specialized(&parser_context, remaining)?; let source = get_consumed(input, remaining); @@ -441,8 +435,35 @@ fn switch_word<'s>(input: OrgSource<'s>) -> Res, OrgSource<'s>> { } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn content_line<'s>(input: OrgSource<'s>) -> Res, OrgSource<'s>> { - let (remaining, _) = opt(tuple((space0, tag(","), peek(alt((tag("#+"), tag("*")))))))(input)?; - let (remaining, line_post_escape) = recognize(many_till(anychar, line_ending))(remaining)?; - Ok((remaining, line_post_escape)) +pub(crate) fn content<'b, 'g, 'r, 's>( + context: RefContext<'b, 'g, 'r, 's>, + input: OrgSource<'s>, +) -> Res, String> { + let mut ret = String::new(); + let mut remaining = input; + let exit_matcher_parser = parser_with_context!(exit_matcher_parser)(context); + loop { + if exit_matcher_parser(remaining).is_ok() { + break; + } + + let (remain, (pre_escape_whitespace, line)) = content_line(remaining)?; + pre_escape_whitespace.map(|val| ret.push_str(Into::<&str>::into(val))); + ret.push_str(line.into()); + remaining = remain; + } + + Ok((remaining, ret)) +} + +#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] +fn content_line<'s>( + input: OrgSource<'s>, +) -> Res, (Option>, OrgSource<'s>)> { + let (remaining, pre_escape_whitespace) = opt(map( + tuple((space0, tag(","), peek(alt((tag("#+"), tag("*")))))), + |(pre_comma, _, _)| pre_comma, + ))(input)?; + let (remaining, line_post_escape) = recognize(many_till(anychar, line_ending))(remaining)?; + Ok((remaining, (pre_escape_whitespace, line_post_escape))) } diff --git a/src/types/lesser_element.rs b/src/types/lesser_element.rs index a90c826..5f1f29c 100644 --- a/src/types/lesser_element.rs +++ b/src/types/lesser_element.rs @@ -45,7 +45,7 @@ pub struct ExampleBlock<'s> { pub retain_labels: bool, pub use_labels: bool, pub label_format: Option<&'s str>, - pub contents: Vec<&'s str>, + pub contents: String, } #[derive(Debug)] @@ -237,16 +237,3 @@ impl<'s> Comment<'s> { ret } } - -impl<'s> ExampleBlock<'s> { - /// Get the inner contents of the ExampleBlock with the escaping commas removed. - pub fn get_contents(&self) -> String { - let final_size = self.contents.iter().map(|line| line.len()).sum(); - let mut ret = String::with_capacity(final_size); - for line in &self.contents { - ret.push_str(line); - } - - ret - } -}