Split GreaterBlock into CenterBlock, QuoteBlock, and SpecialBlock.
Center and quote blocks do not have parameters nor do they store their name so I am separating them out.
This commit is contained in:
@@ -19,7 +19,6 @@ use nom::sequence::tuple;
|
||||
|
||||
use super::org_source::OrgSource;
|
||||
use super::util::in_section;
|
||||
use super::util::org_line_ending;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::ContextElement;
|
||||
use crate::context::ContextMatcher;
|
||||
@@ -34,17 +33,18 @@ use crate::parser::util::blank_line;
|
||||
use crate::parser::util::exit_matcher_parser;
|
||||
use crate::parser::util::get_consumed;
|
||||
use crate::parser::util::start_of_line;
|
||||
use crate::types::CenterBlock;
|
||||
use crate::types::Element;
|
||||
use crate::types::GreaterBlock;
|
||||
use crate::types::Paragraph;
|
||||
use crate::types::QuoteBlock;
|
||||
use crate::types::SetSource;
|
||||
use crate::types::SpecialBlock;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub(crate) fn greater_block<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, GreaterBlock<'s>> {
|
||||
// TODO: Do I need to differentiate between different greater block types.
|
||||
) -> Res<OrgSource<'s>, Element<'s>> {
|
||||
start_of_line(input)?;
|
||||
let (remaining, _leading_whitespace) = space0(input)?;
|
||||
let (remaining, (_begin, name)) = tuple((
|
||||
@@ -56,22 +56,98 @@ pub(crate) fn greater_block<'b, 'g, 'r, 's>(
|
||||
}
|
||||
}),
|
||||
))(remaining)?;
|
||||
let context_name = match Into::<&str>::into(name).to_lowercase().as_str() {
|
||||
"center" => "center block".to_owned(),
|
||||
"quote" => "quote block".to_owned(),
|
||||
name @ _ => format!("special block {}", name),
|
||||
let name = Into::<&str>::into(name);
|
||||
let (remaining, element) = match name.to_lowercase().as_str() {
|
||||
"center" => center_block(context, remaining, input)?,
|
||||
"quote" => quote_block(context, remaining, input)?,
|
||||
_ => special_block(name)(context, remaining, input)?,
|
||||
};
|
||||
if in_section(context, context_name.as_str()) {
|
||||
Ok((remaining, element))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn center_block<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
original_input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Element<'s>> {
|
||||
let (remaining, (source, children)) =
|
||||
greater_block_body(context, input, original_input, "center", "center block")?;
|
||||
Ok((
|
||||
remaining,
|
||||
Element::CenterBlock(CenterBlock { source, children }),
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn quote_block<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
original_input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Element<'s>> {
|
||||
let (remaining, (source, children)) =
|
||||
greater_block_body(context, input, original_input, "quote", "quote block")?;
|
||||
Ok((
|
||||
remaining,
|
||||
Element::QuoteBlock(QuoteBlock { source, children }),
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn special_block<'s>(
|
||||
name: &'s str,
|
||||
) -> impl for<'b, 'g, 'r> Fn(
|
||||
RefContext<'b, 'g, 'r, 's>,
|
||||
OrgSource<'s>,
|
||||
OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Element<'s>>
|
||||
+ 's {
|
||||
let context_name = format!("special block {}", name);
|
||||
move |context, input, original_input| {
|
||||
_special_block(context, input, original_input, name, context_name.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn _special_block<'c, 'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
original_input: OrgSource<'s>,
|
||||
name: &'s str,
|
||||
context_name: &'c str,
|
||||
) -> Res<OrgSource<'s>, Element<'s>> {
|
||||
let (remaining, parameters) = opt(tuple((space1, parameters)))(input)?;
|
||||
let (remaining, (source, children)) =
|
||||
greater_block_body(context, remaining, original_input, name, context_name)?;
|
||||
Ok((
|
||||
remaining,
|
||||
Element::SpecialBlock(SpecialBlock {
|
||||
source,
|
||||
children,
|
||||
name,
|
||||
parameters: parameters.map(|(_, parameters)| Into::<&str>::into(parameters)),
|
||||
}),
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn greater_block_body<'c, 'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
original_input: OrgSource<'s>,
|
||||
name: &'c str,
|
||||
context_name: &'c str,
|
||||
) -> Res<OrgSource<'s>, (&'s str, Vec<Element<'s>>)> {
|
||||
if in_section(context, context_name) {
|
||||
return Err(nom::Err::Error(CustomError::MyError(MyError(
|
||||
"Cannot nest objects of the same element".into(),
|
||||
))));
|
||||
}
|
||||
let exit_with_name = greater_block_end(name.into());
|
||||
let (remaining, parameters) = opt(tuple((space1, parameters)))(remaining)?;
|
||||
let (remaining, _nl) = tuple((space0, line_ending))(remaining)?;
|
||||
let exit_with_name = greater_block_end(name);
|
||||
let (remaining, _nl) = tuple((space0, line_ending))(input)?;
|
||||
let contexts = [
|
||||
ContextElement::ConsumeTrailingWhitespace(true),
|
||||
ContextElement::Context(context_name.as_str()),
|
||||
ContextElement::Context(context_name),
|
||||
ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Alpha,
|
||||
exit_matcher: &exit_with_name,
|
||||
@@ -80,11 +156,6 @@ pub(crate) fn greater_block<'b, 'g, 'r, 's>(
|
||||
let parser_context = context.with_additional_node(&contexts[0]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[1]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[2]);
|
||||
let parameters = match parameters {
|
||||
Some((_ws, parameters)) => Some(parameters),
|
||||
None => None,
|
||||
};
|
||||
|
||||
let element_matcher = parser_with_context!(element(true))(&parser_context);
|
||||
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
|
||||
not(exit_matcher)(remaining)?;
|
||||
@@ -108,16 +179,8 @@ pub(crate) fn greater_block<'b, 'g, 'r, 's>(
|
||||
|
||||
// Not checking if parent exit matcher is causing exit because the greater_block_end matcher asserts we matched a full greater block
|
||||
|
||||
let source = get_consumed(input, remaining);
|
||||
Ok((
|
||||
remaining,
|
||||
GreaterBlock {
|
||||
source: source.into(),
|
||||
name: name.into(),
|
||||
parameters: parameters.map(|val| Into::<&str>::into(val)),
|
||||
children,
|
||||
},
|
||||
))
|
||||
let source = get_consumed(original_input, remaining);
|
||||
Ok((remaining, (Into::<&str>::into(source), children)))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
|
||||
Reference in New Issue
Block a user