Port dynamic block.

This commit is contained in:
Tom Alexander 2023-10-12 16:46:15 -04:00
parent d1223dcdb7
commit 9ccdcaac24
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 38 additions and 29 deletions

View File

@ -18,7 +18,6 @@ use nom::sequence::preceded;
use nom::sequence::tuple; use nom::sequence::tuple;
use super::affiliated_keyword::parse_affiliated_keywords; use super::affiliated_keyword::parse_affiliated_keywords;
use super::keyword::affiliated_keyword;
use super::org_source::OrgSource; use super::org_source::OrgSource;
use super::util::maybe_consume_trailing_whitespace_if_not_exiting; use super::util::maybe_consume_trailing_whitespace_if_not_exiting;
use crate::context::parser_with_context; use crate::context::parser_with_context;
@ -37,6 +36,7 @@ use crate::parser::util::immediate_in_section;
use crate::parser::util::start_of_line; use crate::parser::util::start_of_line;
use crate::types::DynamicBlock; use crate::types::DynamicBlock;
use crate::types::Element; use crate::types::Element;
use crate::types::Keyword;
use crate::types::Paragraph; use crate::types::Paragraph;
use crate::types::SetSource; use crate::types::SetSource;
@ -44,16 +44,20 @@ use crate::types::SetSource;
feature = "tracing", feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context)) tracing::instrument(ret, level = "debug", skip(context))
)] )]
pub(crate) fn dynamic_block<'b, 'g, 'r, 's>( pub(crate) fn dynamic_block<'b, 'g, 'r, 's, AK>(
affiliated_keywords: AK,
remaining: OrgSource<'s>,
context: RefContext<'b, 'g, 'r, 's>, context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>, input: OrgSource<'s>,
) -> Res<OrgSource<'s>, DynamicBlock<'s>> { ) -> Res<OrgSource<'s>, DynamicBlock<'s>>
where
AK: IntoIterator<Item = Keyword<'s>>,
{
if immediate_in_section(context, "dynamic block") { if immediate_in_section(context, "dynamic block") {
return Err(nom::Err::Error(CustomError::MyError(MyError( return Err(nom::Err::Error(CustomError::MyError(MyError(
"Cannot nest objects of the same element".into(), "Cannot nest objects of the same element".into(),
)))); ))));
} }
let (remaining, affiliated_keywords) = many0(affiliated_keyword)(input)?;
start_of_line(remaining)?; start_of_line(remaining)?;
let (remaining, _leading_whitespace) = space0(remaining)?; let (remaining, _leading_whitespace) = space0(remaining)?;

View File

@ -1,9 +1,7 @@
use nom::branch::alt; use nom::branch::alt;
use nom::combinator::map; use nom::combinator::map;
use nom::combinator::opt; use nom::combinator::opt;
use nom::combinator::peek;
use nom::multi::many0; use nom::multi::many0;
use nom::sequence::tuple;
#[cfg(feature = "tracing")] #[cfg(feature = "tracing")]
use tracing::span; use tracing::span;
@ -82,7 +80,15 @@ fn _element<'b, 'g, 'r, 's>(
input input
); );
let dynamic_block_matcher = parser_with_context!(dynamic_block)(context); ak_element!(
dynamic_block,
&mut affiliated_keywords,
post_affiliated_keywords_input,
context,
input,
Element::DynamicBlock
);
let footnote_definition_matcher = parser_with_context!(footnote_definition)(context); let footnote_definition_matcher = parser_with_context!(footnote_definition)(context);
let comment_matcher = parser_with_context!(comment)(context); let comment_matcher = parser_with_context!(comment)(context);
let drawer_matcher = parser_with_context!(drawer)(context); let drawer_matcher = parser_with_context!(drawer)(context);
@ -100,14 +106,13 @@ fn _element<'b, 'g, 'r, 's>(
let babel_keyword_matcher = parser_with_context!(babel_call)(context); let babel_keyword_matcher = parser_with_context!(babel_call)(context);
let latex_environment_matcher = parser_with_context!(latex_environment)(context); let latex_environment_matcher = parser_with_context!(latex_environment)(context);
let (mut remaining, mut maybe_element) = { let (remaining, maybe_element) = {
#[cfg(feature = "tracing")] #[cfg(feature = "tracing")]
let span = span!(tracing::Level::DEBUG, "Main element block"); let span = span!(tracing::Level::DEBUG, "Main element block");
#[cfg(feature = "tracing")] #[cfg(feature = "tracing")]
let _enter = span.enter(); let _enter = span.enter();
opt(alt(( opt(alt((
map(dynamic_block_matcher, Element::DynamicBlock),
map(footnote_definition_matcher, Element::FootnoteDefinition), map(footnote_definition_matcher, Element::FootnoteDefinition),
map(comment_matcher, Element::Comment), map(comment_matcher, Element::Comment),
map(drawer_matcher, Element::Drawer), map(drawer_matcher, Element::Drawer),
@ -127,6 +132,7 @@ fn _element<'b, 'g, 'r, 's>(
)))(input)? )))(input)?
}; };
if can_be_paragraph {
// Paragraph with affiliated keyword // Paragraph with affiliated keyword
ak_element!( ak_element!(
paragraph, paragraph,
@ -146,6 +152,7 @@ fn _element<'b, 'g, 'r, 's>(
input, input,
Element::Paragraph Element::Paragraph
); );
}
if maybe_element.is_none() { if maybe_element.is_none() {
return Err(nom::Err::Error(CustomError::MyError(MyError( return Err(nom::Err::Error(CustomError::MyError(MyError(

View File

@ -2,14 +2,12 @@ use nom::branch::alt;
use nom::combinator::eof; use nom::combinator::eof;
use nom::combinator::recognize; use nom::combinator::recognize;
use nom::combinator::verify; use nom::combinator::verify;
use nom::multi::many0;
use nom::multi::many1; use nom::multi::many1;
use nom::multi::many_till; use nom::multi::many_till;
use nom::sequence::tuple; use nom::sequence::tuple;
use super::affiliated_keyword::parse_affiliated_keywords; use super::affiliated_keyword::parse_affiliated_keywords;
use super::element_parser::detect_element; use super::element_parser::detect_element;
use super::keyword::affiliated_keyword;
use super::org_source::OrgSource; use super::org_source::OrgSource;
use super::util::blank_line; use super::util::blank_line;
use super::util::get_consumed; use super::util::get_consumed;