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-08-23 00:30:26 -04:00
|
|
|
use super::org_source::OrgSource;
|
2023-08-31 19:17:46 -04:00
|
|
|
use super::util::in_section;
|
2023-09-03 12:07:51 -04:00
|
|
|
use crate::context::parser_with_context;
|
|
|
|
use crate::context::ContextElement;
|
|
|
|
use crate::context::ContextMatcher;
|
|
|
|
use crate::context::ExitClass;
|
|
|
|
use crate::context::ExitMatcherNode;
|
|
|
|
use crate::context::RefContext;
|
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 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;
|
|
|
|
use crate::parser::util::start_of_line;
|
2023-09-03 12:07:51 -04:00
|
|
|
use crate::types::Element;
|
|
|
|
use crate::types::GreaterBlock;
|
|
|
|
use crate::types::Paragraph;
|
|
|
|
use crate::types::SetSource;
|
2023-04-03 17:36:56 -04:00
|
|
|
|
2023-08-10 20:04:59 -04:00
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
2023-09-11 13:11:08 -04:00
|
|
|
fn greater_block<'b, 'g, 'r, 's>(
|
2023-09-03 15:44:18 -04:00
|
|
|
context: RefContext<'b, 'g, 'r, 's>,
|
2023-08-23 00:30:26 -04:00
|
|
|
input: OrgSource<'s>,
|
|
|
|
) -> Res<OrgSource<'s>, GreaterBlock<'s>> {
|
2023-04-18 22:10:44 -04:00
|
|
|
// TODO: Do I need to differentiate between different greater block types.
|
2023-08-24 19:29:00 -04:00
|
|
|
start_of_line(input)?;
|
2023-04-07 16:18:13 -04:00
|
|
|
let (remaining, _leading_whitespace) = space0(input)?;
|
2023-04-03 18:38:35 -04:00
|
|
|
let (remaining, (_begin, name)) = tuple((
|
|
|
|
tag_no_case("#+begin_"),
|
2023-08-23 00:30:26 -04:00
|
|
|
verify(name, |name: &OrgSource<'_>| {
|
|
|
|
match Into::<&str>::into(name).to_lowercase().as_str() {
|
|
|
|
"comment" | "example" | "export" | "src" | "verse" => false,
|
|
|
|
_ => true,
|
|
|
|
}
|
2023-04-03 18:38:35 -04:00
|
|
|
}),
|
2023-04-07 16:18:13 -04:00
|
|
|
))(remaining)?;
|
2023-08-23 00:30:26 -04:00
|
|
|
let context_name = match Into::<&str>::into(name).to_lowercase().as_str() {
|
2023-08-31 19:02:18 -04:00
|
|
|
"center" => "center block".to_owned(),
|
|
|
|
"quote" => "quote block".to_owned(),
|
|
|
|
name @ _ => format!("special block {}", name),
|
2023-04-19 15:00:02 -04:00
|
|
|
};
|
2023-08-31 19:17:46 -04:00
|
|
|
if in_section(context, context_name.as_str()) {
|
2023-04-19 15:00:02 -04:00
|
|
|
return Err(nom::Err::Error(CustomError::MyError(MyError(
|
2023-08-23 00:30:26 -04:00
|
|
|
"Cannot nest objects of the same element".into(),
|
2023-04-19 15:00:02 -04:00
|
|
|
))));
|
|
|
|
}
|
2023-08-27 22:35:37 -04:00
|
|
|
let exit_with_name = greater_block_end(name.into());
|
2023-04-03 18:33:07 -04:00
|
|
|
let (remaining, parameters) = opt(tuple((space1, parameters)))(remaining)?;
|
|
|
|
let (remaining, _nl) = line_ending(remaining)?;
|
2023-09-03 12:07:51 -04:00
|
|
|
let contexts = [
|
|
|
|
ContextElement::ConsumeTrailingWhitespace(true),
|
|
|
|
ContextElement::Context(context_name.as_str()),
|
|
|
|
ContextElement::ExitMatcherNode(ExitMatcherNode {
|
2023-04-18 20:33:01 -04:00
|
|
|
class: ExitClass::Alpha,
|
2023-08-27 22:35:37 -04:00
|
|
|
exit_matcher: &exit_with_name,
|
2023-09-03 12:07:51 -04:00
|
|
|
}),
|
|
|
|
];
|
2023-09-03 12:28:45 -04:00
|
|
|
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]);
|
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)))) => {
|
2023-08-23 00:30:26 -04:00
|
|
|
let mut element = Element::Paragraph(Paragraph::of_text(first_line.into()));
|
2023-04-22 21:45:18 -04:00
|
|
|
let source = get_consumed(remaining, remain);
|
2023-08-23 00:30:26 -04:00
|
|
|
element.set_source(source.into());
|
2023-04-22 21:45:18 -04:00
|
|
|
(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-08-27 22:35:37 -04:00
|
|
|
let (remaining, _end) = exit_with_name(&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 {
|
2023-08-23 00:30:26 -04:00
|
|
|
source: source.into(),
|
|
|
|
name: name.into(),
|
|
|
|
parameters: parameters.map(|val| Into::<&str>::into(val)),
|
2023-04-03 18:33:07 -04:00
|
|
|
children,
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2023-08-10 20:04:59 -04:00
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
2023-08-23 00:30:26 -04:00
|
|
|
fn name<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
2023-04-03 18:33:07 -04:00
|
|
|
is_not(" \t\r\n")(input)
|
|
|
|
}
|
|
|
|
|
2023-08-10 20:04:59 -04:00
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
2023-08-23 00:30:26 -04:00
|
|
|
fn parameters<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
2023-04-03 18:33:07 -04:00
|
|
|
is_not("\r\n")(input)
|
|
|
|
}
|
|
|
|
|
2023-09-03 12:07:51 -04:00
|
|
|
fn greater_block_end<'c>(name: &'c str) -> impl ContextMatcher + 'c {
|
2023-08-27 22:35:37 -04:00
|
|
|
// TODO: Can this be done without making an owned copy?
|
2023-09-03 12:07:51 -04:00
|
|
|
move |context, input: OrgSource<'_>| _greater_block_end(context, input, name)
|
2023-08-27 22:35:37 -04:00
|
|
|
}
|
|
|
|
|
2023-08-10 20:04:59 -04:00
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
2023-09-03 15:44:18 -04:00
|
|
|
fn _greater_block_end<'b, 'g, 'r, 's, 'c>(
|
|
|
|
_context: RefContext<'b, 'g, 'r, 's>,
|
2023-08-23 00:30:26 -04:00
|
|
|
input: OrgSource<'s>,
|
2023-09-03 12:07:51 -04:00
|
|
|
name: &'c str,
|
2023-08-23 00:30:26 -04:00
|
|
|
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
2023-08-24 19:29:00 -04:00
|
|
|
start_of_line(input)?;
|
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_"),
|
2023-08-27 22:35:37 -04:00
|
|
|
tag_no_case(name),
|
2023-04-03 18:33:07 -04:00
|
|
|
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))
|
|
|
|
}
|