From 87bdedf2e6034078c45611046afbff169b803160 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Wed, 19 Apr 2023 19:39:56 -0400 Subject: [PATCH] Only drop trailing whitespace when there isn't a property drawer. --- src/parser/document.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/parser/document.rs b/src/parser/document.rs index e57710e..6d2e2da 100644 --- a/src/parser/document.rs +++ b/src/parser/document.rs @@ -157,7 +157,7 @@ fn zeroth_section<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s s } #[tracing::instrument(ret, level = "debug")] -fn section<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Section<'s>> { +fn section<'r, 's>(context: Context<'r, 's>, mut input: &'s str) -> Res<&'s str, Section<'s>> { // TODO: The zeroth section is specialized so it probably needs its own parser let parser_context = context .with_additional_node(ContextElement::ConsumeTrailingWhitespace(true)) @@ -168,8 +168,13 @@ fn section<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Sec })); let element_matcher = parser_with_context!(element)(&parser_context); let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context); - let (remaining, property_drawer_element) = + let (mut remaining, property_drawer_element) = opt(parser_with_context!(property_drawer)(&parser_context))(input)?; + if property_drawer_element.is_none() { + let (remain, _ws) = many0(blank_line)(remaining)?; + remaining = remain; + input = remain; + } let (remaining, (mut children, _exit_contents)) = verify( many_till(element_matcher, exit_matcher), |(children, _exit_contents)| !children.is_empty() || property_drawer_element.is_some(), @@ -194,7 +199,7 @@ fn section_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, #[tracing::instrument(ret, level = "debug")] fn heading<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Heading<'s>> { not(|i| context.check_exit_matcher(i))(input)?; - let (remaining, (star_count, _ws, title, _ws2)) = headline(context, input)?; + let (remaining, (star_count, _ws, title)) = headline(context, input)?; let section_matcher = parser_with_context!(section)(context); let heading_matcher = parser_with_context!(heading)(context); let (remaining, children) = many0(alt(( @@ -220,7 +225,7 @@ fn heading<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Hea fn headline<'r, 's>( context: Context<'r, 's>, input: &'s str, -) -> Res<&'s str, (usize, &'s str, Vec>, &'s str)> { +) -> Res<&'s str, (usize, &'s str, Vec>)> { let parser_context = context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Document, @@ -229,14 +234,13 @@ fn headline<'r, 's>( let standard_set_object_matcher = parser_with_context!(standard_set_object)(&parser_context); let start_of_line_matcher = parser_with_context!(start_of_line)(&parser_context); - let (remaining, (_sol, star_count, ws, title, ws2)) = tuple(( + let (remaining, (_sol, star_count, ws, title)) = tuple(( start_of_line_matcher, many1_count(tag("*")), space1, many1(standard_set_object_matcher), - trailing_whitespace, ))(input)?; - Ok((remaining, (star_count, ws, title, ws2))) + Ok((remaining, (star_count, ws, title))) } #[tracing::instrument(ret, level = "debug")]