Update the section parser to handle the new whitespace-included element parsers.

This commit is contained in:
Tom Alexander 2023-04-10 11:16:32 -04:00
parent 54b0ff5036
commit cbe7ef4030
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

View File

@ -107,36 +107,23 @@ 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 trailing_matcher = parser_with_context!(element_trailing_whitespace)(&parser_context);
let (remaining, (children, _exit_contents)) = verify(
many_till(
tuple((
element_matcher,
opt(map(trailing_matcher, Element::TrailingWhitespace)),
)),
exit_matcher,
),
many_till(element_matcher, exit_matcher),
|(children, _exit_contents)| !children.is_empty(),
)(input)?;
let flattened_children: Vec<Element> = children
.into_iter()
.flat_map(|tpl| {
let mut flattened_children = Vec::with_capacity(2);
flattened_children.push(tpl.0);
if let Some(bar) = tpl.1 {
flattened_children.push(bar);
}
flattened_children.into_iter()
})
.collect();
// Check if a parent exit matcher is causing the exit
exit_matcher_parser(context, remaining)?;
let (remaining, _trailing_ws) = if context.should_consume_trailing_whitespace() {
opt(parser_with_context!(element_trailing_whitespace)(&parser_context))(remaining)?
} else {
(remaining, None)
};
let source = get_consumed(input, remaining);
Ok((
remaining,
Section {
source,
children: flattened_children,
},
))
Ok((remaining, Section { source, children }))
}
#[tracing::instrument(ret, level = "debug")]