2023-04-22 21:45:18 -04:00
|
|
|
use nom::branch::alt;
|
|
|
|
use nom::bytes::complete::is_not;
|
|
|
|
use nom::bytes::complete::tag_no_case;
|
|
|
|
use nom::character::complete::line_ending;
|
|
|
|
use nom::character::complete::space0;
|
|
|
|
use nom::character::complete::space1;
|
|
|
|
use nom::combinator::eof;
|
|
|
|
use nom::combinator::not;
|
|
|
|
use nom::combinator::opt;
|
|
|
|
use nom::combinator::verify;
|
|
|
|
use nom::multi::many_till;
|
|
|
|
use nom::sequence::tuple;
|
|
|
|
|
2023-04-21 23:54:54 -04:00
|
|
|
use super::Context;
|
2023-04-21 18:36:01 -04:00
|
|
|
use crate::error::CustomError;
|
|
|
|
use crate::error::MyError;
|
|
|
|
use crate::error::Res;
|
2023-04-21 16:10:56 -04:00
|
|
|
use crate::parser::element_parser::element;
|
2023-04-18 20:33:01 -04:00
|
|
|
use crate::parser::exiting::ExitClass;
|
2023-04-03 18:33:07 -04:00
|
|
|
use crate::parser::greater_element::GreaterBlock;
|
|
|
|
use crate::parser::parser_context::ContextElement;
|
|
|
|
use crate::parser::parser_context::ExitMatcherNode;
|
|
|
|
use crate::parser::parser_with_context::parser_with_context;
|
2023-04-22 21:45:18 -04:00
|
|
|
use crate::parser::source::SetSource;
|
2023-04-18 21:28:22 -04:00
|
|
|
use crate::parser::util::blank_line;
|
2023-04-03 18:33:07 -04:00
|
|
|
use crate::parser::util::exit_matcher_parser;
|
|
|
|
use crate::parser::util::get_consumed;
|
2023-04-18 22:10:44 -04:00
|
|
|
use crate::parser::util::immediate_in_section;
|
2023-04-03 18:33:07 -04:00
|
|
|
use crate::parser::util::start_of_line;
|
2023-04-18 21:28:22 -04:00
|
|
|
use crate::parser::Element;
|
|
|
|
use crate::parser::Paragraph;
|
2023-04-03 17:36:56 -04:00
|
|
|
|
2023-04-03 18:38:35 -04:00
|
|
|
#[tracing::instrument(ret, level = "debug")]
|
2023-04-03 18:33:07 -04:00
|
|
|
pub fn greater_block<'r, 's>(
|
|
|
|
context: Context<'r, 's>,
|
|
|
|
input: &'s str,
|
|
|
|
) -> Res<&'s str, GreaterBlock<'s>> {
|
2023-04-18 22:10:44 -04:00
|
|
|
// TODO: Do I need to differentiate between different greater block types.
|
2023-04-07 16:18:13 -04:00
|
|
|
start_of_line(context, input)?;
|
|
|
|
let (remaining, _leading_whitespace) = space0(input)?;
|
2023-04-03 18:38:35 -04:00
|
|
|
let (remaining, (_begin, name)) = tuple((
|
|
|
|
tag_no_case("#+begin_"),
|
|
|
|
verify(name, |name: &str| match name.to_lowercase().as_str() {
|
|
|
|
"comment" | "example" | "export" | "src" | "verse" => false,
|
|
|
|
_ => true,
|
|
|
|
}),
|
2023-04-07 16:18:13 -04:00
|
|
|
))(remaining)?;
|
2023-04-19 15:00:02 -04:00
|
|
|
let context_name = match name.to_lowercase().as_str() {
|
|
|
|
"center" => "center block",
|
|
|
|
"quote" => "quote block",
|
|
|
|
_ => "greater block",
|
|
|
|
};
|
|
|
|
if immediate_in_section(context, context_name) {
|
|
|
|
return Err(nom::Err::Error(CustomError::MyError(MyError(
|
|
|
|
"Cannot nest objects of the same element",
|
|
|
|
))));
|
|
|
|
}
|
2023-04-03 18:33:07 -04:00
|
|
|
let (remaining, parameters) = opt(tuple((space1, parameters)))(remaining)?;
|
|
|
|
let (remaining, _nl) = line_ending(remaining)?;
|
|
|
|
let parser_context = context
|
2023-04-10 10:36:16 -04:00
|
|
|
.with_additional_node(ContextElement::ConsumeTrailingWhitespace(true))
|
2023-04-19 15:00:02 -04:00
|
|
|
.with_additional_node(ContextElement::Context(context_name))
|
2023-04-10 10:36:16 -04:00
|
|
|
.with_additional_node(ContextElement::GreaterBlock(name))
|
2023-04-03 18:33:07 -04:00
|
|
|
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
2023-04-18 20:33:01 -04:00
|
|
|
class: ExitClass::Alpha,
|
|
|
|
exit_matcher: &greater_block_end,
|
2023-04-10 10:36:16 -04:00
|
|
|
}));
|
2023-04-18 21:28:22 -04:00
|
|
|
let parameters = match parameters {
|
|
|
|
Some((_ws, parameters)) => Some(parameters),
|
|
|
|
None => None,
|
|
|
|
};
|
2023-04-03 18:33:07 -04:00
|
|
|
|
2023-04-22 01:45:38 -04:00
|
|
|
let element_matcher = parser_with_context!(element(true))(&parser_context);
|
2023-04-03 18:33:07 -04:00
|
|
|
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
|
2023-04-18 21:28:22 -04:00
|
|
|
// Check for a completely empty block
|
2023-04-22 21:45:18 -04:00
|
|
|
let (remaining, children) = match tuple((
|
|
|
|
not(exit_matcher),
|
|
|
|
blank_line,
|
|
|
|
many_till(blank_line, exit_matcher),
|
|
|
|
))(remaining)
|
|
|
|
{
|
|
|
|
Ok((remain, (_not_immediate_exit, first_line, (_trailing_whitespace, _exit_contents)))) => {
|
|
|
|
let mut element = Element::Paragraph(Paragraph::of_text(first_line));
|
|
|
|
let source = get_consumed(remaining, remain);
|
|
|
|
element.set_source(source);
|
|
|
|
(remain, vec![element])
|
|
|
|
}
|
2023-04-18 21:28:22 -04:00
|
|
|
Err(_) => {
|
|
|
|
let (remaining, (children, _exit_contents)) =
|
|
|
|
many_till(element_matcher, exit_matcher)(remaining)?;
|
|
|
|
(remaining, children)
|
|
|
|
}
|
|
|
|
};
|
2023-04-03 18:33:07 -04:00
|
|
|
let (remaining, _end) = greater_block_end(&parser_context, remaining)?;
|
2023-04-10 11:50:43 -04:00
|
|
|
|
|
|
|
// Not checking if parent exit matcher is causing exit because the greater_block_end matcher asserts we matched a full greater block
|
|
|
|
|
2023-04-03 18:33:07 -04:00
|
|
|
let source = get_consumed(input, remaining);
|
|
|
|
Ok((
|
|
|
|
remaining,
|
|
|
|
GreaterBlock {
|
|
|
|
source,
|
|
|
|
name,
|
|
|
|
parameters,
|
|
|
|
children,
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2023-04-03 18:38:35 -04:00
|
|
|
#[tracing::instrument(ret, level = "debug")]
|
2023-04-03 18:33:07 -04:00
|
|
|
fn name<'s>(input: &'s str) -> Res<&'s str, &'s str> {
|
|
|
|
is_not(" \t\r\n")(input)
|
|
|
|
}
|
|
|
|
|
2023-04-03 18:38:35 -04:00
|
|
|
#[tracing::instrument(ret, level = "debug")]
|
2023-04-03 18:33:07 -04:00
|
|
|
fn parameters<'s>(input: &'s str) -> Res<&'s str, &'s str> {
|
|
|
|
is_not("\r\n")(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(ret, level = "debug")]
|
|
|
|
fn greater_block_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
|
|
|
start_of_line(context, input)?;
|
|
|
|
let current_name: &str = get_context_greater_block_name(context).ok_or(nom::Err::Error(
|
|
|
|
CustomError::MyError(MyError("Not inside a greater block")),
|
|
|
|
))?;
|
2023-04-07 16:18:13 -04:00
|
|
|
let (remaining, _leading_whitespace) = space0(input)?;
|
2023-04-03 18:33:07 -04:00
|
|
|
let (remaining, (_begin, _name, _ws)) = tuple((
|
|
|
|
tag_no_case("#+end_"),
|
|
|
|
tag_no_case(current_name),
|
|
|
|
alt((eof, line_ending)),
|
2023-04-07 16:18:13 -04:00
|
|
|
))(remaining)?;
|
2023-04-03 18:33:07 -04:00
|
|
|
let source = get_consumed(input, remaining);
|
|
|
|
Ok((remaining, source))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_context_greater_block_name<'r, 's>(context: Context<'r, 's>) -> Option<&'s str> {
|
|
|
|
for thing in context.iter() {
|
|
|
|
match thing.get_data() {
|
|
|
|
ContextElement::GreaterBlock(name) => return Some(name),
|
|
|
|
_ => {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|