2023-04-23 01:45:18 +00:00
|
|
|
use nom::branch::alt;
|
2023-10-12 19:47:06 +00:00
|
|
|
use nom::multi::many0;
|
2023-04-23 01:45:18 +00:00
|
|
|
|
2023-10-05 20:27:36 +00:00
|
|
|
use super::babel_call::babel_call;
|
2023-04-21 23:02:16 +00:00
|
|
|
use super::clock::clock;
|
2023-04-21 20:10:56 +00:00
|
|
|
use super::comment::comment;
|
2023-09-11 16:28:15 +00:00
|
|
|
use super::comment::detect_comment;
|
|
|
|
use super::diary_sexp::detect_diary_sexp;
|
2023-04-22 00:22:31 +00:00
|
|
|
use super::diary_sexp::diary_sexp;
|
2023-04-21 20:10:56 +00:00
|
|
|
use super::drawer::drawer;
|
|
|
|
use super::dynamic_block::dynamic_block;
|
2023-09-11 16:28:15 +00:00
|
|
|
use super::fixed_width_area::detect_fixed_width_area;
|
2023-04-22 02:04:22 +00:00
|
|
|
use super::fixed_width_area::fixed_width_area;
|
2023-09-11 16:28:15 +00:00
|
|
|
use super::footnote_definition::detect_footnote_definition;
|
2023-04-21 20:10:56 +00:00
|
|
|
use super::footnote_definition::footnote_definition;
|
|
|
|
use super::greater_block::greater_block;
|
2023-04-22 02:23:59 +00:00
|
|
|
use super::horizontal_rule::horizontal_rule;
|
2023-08-29 20:56:07 +00:00
|
|
|
use super::keyword::affiliated_keyword;
|
2023-04-22 02:33:10 +00:00
|
|
|
use super::keyword::keyword;
|
2023-04-22 20:56:36 +00:00
|
|
|
use super::latex_environment::latex_environment;
|
2023-04-21 21:40:49 +00:00
|
|
|
use super::lesser_block::comment_block;
|
|
|
|
use super::lesser_block::example_block;
|
|
|
|
use super::lesser_block::export_block;
|
|
|
|
use super::lesser_block::src_block;
|
|
|
|
use super::lesser_block::verse_block;
|
2023-08-23 04:30:26 +00:00
|
|
|
use super::org_source::OrgSource;
|
2023-04-21 20:10:56 +00:00
|
|
|
use super::paragraph::paragraph;
|
2023-08-25 04:48:34 +00:00
|
|
|
use super::plain_list::detect_plain_list;
|
2023-04-21 20:10:56 +00:00
|
|
|
use super::plain_list::plain_list;
|
2023-09-11 16:28:15 +00:00
|
|
|
use super::table::detect_table;
|
2023-09-03 16:23:18 +00:00
|
|
|
use crate::context::parser_with_context;
|
|
|
|
use crate::context::RefContext;
|
2023-08-25 04:48:34 +00:00
|
|
|
use crate::error::CustomError;
|
|
|
|
use crate::error::MyError;
|
2023-04-21 22:36:01 +00:00
|
|
|
use crate::error::Res;
|
2023-10-12 20:06:59 +00:00
|
|
|
use crate::parser::macros::ak_element;
|
2023-10-12 20:52:49 +00:00
|
|
|
use crate::parser::macros::element;
|
2023-04-21 20:10:56 +00:00
|
|
|
use crate::parser::table::org_mode_table;
|
2023-09-03 16:23:18 +00:00
|
|
|
use crate::types::Element;
|
2023-04-21 20:10:56 +00:00
|
|
|
|
2023-09-11 17:13:28 +00:00
|
|
|
pub(crate) const fn element(
|
2023-04-22 04:55:31 +00:00
|
|
|
can_be_paragraph: bool,
|
2023-09-03 19:44:18 +00:00
|
|
|
) -> impl for<'b, 'g, 'r, 's> Fn(
|
|
|
|
RefContext<'b, 'g, 'r, 's>,
|
|
|
|
OrgSource<'s>,
|
|
|
|
) -> Res<OrgSource<'s>, Element<'s>> {
|
2023-09-03 16:23:18 +00:00
|
|
|
move |context, input: OrgSource<'_>| _element(context, input, can_be_paragraph)
|
2023-04-22 23:15:29 +00:00
|
|
|
}
|
|
|
|
|
2023-10-09 23:52:32 +00:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "tracing",
|
|
|
|
tracing::instrument(ret, level = "debug", skip(context))
|
|
|
|
)]
|
2023-09-03 19:44:18 +00:00
|
|
|
fn _element<'b, 'g, 'r, 's>(
|
|
|
|
context: RefContext<'b, 'g, 'r, 's>,
|
2023-08-23 04:30:26 +00:00
|
|
|
input: OrgSource<'s>,
|
2023-04-22 23:15:29 +00:00
|
|
|
can_be_paragraph: bool,
|
2023-08-23 04:30:26 +00:00
|
|
|
) -> Res<OrgSource<'s>, Element<'s>> {
|
2023-10-12 19:47:06 +00:00
|
|
|
let (post_affiliated_keywords_input, affiliated_keywords) = many0(affiliated_keyword)(input)?;
|
|
|
|
|
2023-10-12 20:06:59 +00:00
|
|
|
let mut affiliated_keywords = affiliated_keywords.into_iter();
|
|
|
|
|
|
|
|
ak_element!(
|
2023-10-12 20:09:35 +00:00
|
|
|
plain_list,
|
2023-10-12 20:06:59 +00:00
|
|
|
&mut affiliated_keywords,
|
|
|
|
post_affiliated_keywords_input,
|
|
|
|
context,
|
|
|
|
input,
|
|
|
|
Element::PlainList
|
|
|
|
);
|
|
|
|
|
2023-10-12 20:27:57 +00:00
|
|
|
ak_element!(
|
|
|
|
greater_block,
|
|
|
|
&mut affiliated_keywords,
|
|
|
|
post_affiliated_keywords_input,
|
|
|
|
context,
|
|
|
|
input
|
|
|
|
);
|
|
|
|
|
2023-10-12 20:46:15 +00:00
|
|
|
ak_element!(
|
|
|
|
dynamic_block,
|
|
|
|
&mut affiliated_keywords,
|
|
|
|
post_affiliated_keywords_input,
|
|
|
|
context,
|
|
|
|
input,
|
|
|
|
Element::DynamicBlock
|
|
|
|
);
|
|
|
|
|
2023-10-12 20:48:50 +00:00
|
|
|
ak_element!(
|
|
|
|
footnote_definition,
|
|
|
|
&mut affiliated_keywords,
|
|
|
|
post_affiliated_keywords_input,
|
|
|
|
context,
|
|
|
|
input,
|
|
|
|
Element::FootnoteDefinition
|
|
|
|
);
|
|
|
|
|
2023-10-12 20:52:49 +00:00
|
|
|
element!(comment, context, input, Element::Comment);
|
|
|
|
|
2023-10-12 21:12:55 +00:00
|
|
|
ak_element!(
|
|
|
|
drawer,
|
|
|
|
&mut affiliated_keywords,
|
|
|
|
post_affiliated_keywords_input,
|
|
|
|
context,
|
|
|
|
input,
|
|
|
|
Element::Drawer
|
|
|
|
);
|
|
|
|
|
|
|
|
ak_element!(
|
|
|
|
org_mode_table,
|
|
|
|
&mut affiliated_keywords,
|
|
|
|
post_affiliated_keywords_input,
|
|
|
|
context,
|
|
|
|
input,
|
|
|
|
Element::Table
|
|
|
|
);
|
|
|
|
|
|
|
|
ak_element!(
|
|
|
|
verse_block,
|
|
|
|
&mut affiliated_keywords,
|
|
|
|
post_affiliated_keywords_input,
|
|
|
|
context,
|
|
|
|
input,
|
|
|
|
Element::VerseBlock
|
|
|
|
);
|
|
|
|
|
|
|
|
ak_element!(
|
|
|
|
comment_block,
|
|
|
|
&mut affiliated_keywords,
|
|
|
|
post_affiliated_keywords_input,
|
|
|
|
context,
|
|
|
|
input,
|
|
|
|
Element::CommentBlock
|
|
|
|
);
|
|
|
|
|
|
|
|
ak_element!(
|
|
|
|
example_block,
|
|
|
|
&mut affiliated_keywords,
|
|
|
|
post_affiliated_keywords_input,
|
|
|
|
context,
|
|
|
|
input,
|
|
|
|
Element::ExampleBlock
|
|
|
|
);
|
|
|
|
|
|
|
|
ak_element!(
|
|
|
|
export_block,
|
|
|
|
&mut affiliated_keywords,
|
|
|
|
post_affiliated_keywords_input,
|
|
|
|
context,
|
|
|
|
input,
|
|
|
|
Element::ExportBlock
|
|
|
|
);
|
|
|
|
|
|
|
|
ak_element!(
|
|
|
|
src_block,
|
|
|
|
&mut affiliated_keywords,
|
|
|
|
post_affiliated_keywords_input,
|
|
|
|
context,
|
|
|
|
input,
|
|
|
|
Element::SrcBlock
|
|
|
|
);
|
|
|
|
|
|
|
|
element!(clock, context, input, Element::Clock);
|
|
|
|
|
|
|
|
ak_element!(
|
|
|
|
diary_sexp,
|
|
|
|
&mut affiliated_keywords,
|
|
|
|
post_affiliated_keywords_input,
|
|
|
|
context,
|
|
|
|
input,
|
|
|
|
Element::DiarySexp
|
|
|
|
);
|
|
|
|
|
|
|
|
ak_element!(
|
|
|
|
fixed_width_area,
|
|
|
|
&mut affiliated_keywords,
|
|
|
|
post_affiliated_keywords_input,
|
|
|
|
context,
|
|
|
|
input,
|
|
|
|
Element::FixedWidthArea
|
|
|
|
);
|
|
|
|
|
|
|
|
ak_element!(
|
|
|
|
horizontal_rule,
|
|
|
|
&mut affiliated_keywords,
|
|
|
|
post_affiliated_keywords_input,
|
|
|
|
context,
|
|
|
|
input,
|
|
|
|
Element::HorizontalRule
|
|
|
|
);
|
|
|
|
|
|
|
|
ak_element!(
|
|
|
|
latex_environment,
|
|
|
|
&mut affiliated_keywords,
|
|
|
|
post_affiliated_keywords_input,
|
|
|
|
context,
|
|
|
|
input,
|
|
|
|
Element::LatexEnvironment
|
|
|
|
);
|
|
|
|
|
|
|
|
ak_element!(
|
|
|
|
babel_call,
|
|
|
|
&mut affiliated_keywords,
|
|
|
|
post_affiliated_keywords_input,
|
|
|
|
context,
|
|
|
|
input,
|
|
|
|
Element::BabelCall
|
|
|
|
);
|
|
|
|
|
2023-10-12 21:23:54 +00:00
|
|
|
// Keyword with affiliated keywords
|
2023-10-12 21:12:55 +00:00
|
|
|
ak_element!(
|
|
|
|
keyword,
|
|
|
|
&mut affiliated_keywords,
|
|
|
|
post_affiliated_keywords_input,
|
|
|
|
context,
|
|
|
|
input,
|
|
|
|
Element::Keyword
|
|
|
|
);
|
2023-10-05 02:47:13 +00:00
|
|
|
|
2023-10-12 20:46:15 +00:00
|
|
|
if can_be_paragraph {
|
|
|
|
// Paragraph with affiliated keyword
|
|
|
|
ak_element!(
|
|
|
|
paragraph,
|
|
|
|
&mut affiliated_keywords,
|
|
|
|
post_affiliated_keywords_input,
|
|
|
|
context,
|
|
|
|
input,
|
|
|
|
Element::Paragraph
|
|
|
|
);
|
2023-10-12 21:32:29 +00:00
|
|
|
}
|
2023-10-05 02:47:13 +00:00
|
|
|
|
2023-10-12 21:32:29 +00:00
|
|
|
// Keyword without affiliated keywords
|
|
|
|
ak_element!(
|
|
|
|
keyword,
|
|
|
|
std::iter::empty(),
|
|
|
|
input,
|
|
|
|
context,
|
|
|
|
input,
|
|
|
|
Element::Keyword
|
|
|
|
);
|
|
|
|
|
|
|
|
if can_be_paragraph {
|
2023-10-12 20:46:15 +00:00
|
|
|
// Paragraph without affiliated keyword
|
|
|
|
ak_element!(
|
|
|
|
paragraph,
|
|
|
|
std::iter::empty(),
|
|
|
|
input,
|
|
|
|
context,
|
|
|
|
input,
|
|
|
|
Element::Paragraph
|
|
|
|
);
|
|
|
|
}
|
2023-10-05 02:47:13 +00:00
|
|
|
|
2023-10-12 21:12:55 +00:00
|
|
|
Err(nom::Err::Error(CustomError::MyError(MyError(
|
|
|
|
"No element.",
|
|
|
|
))))
|
2023-04-21 20:10:56 +00:00
|
|
|
}
|
2023-08-25 04:48:34 +00:00
|
|
|
|
2023-09-11 17:13:28 +00:00
|
|
|
pub(crate) const fn detect_element(
|
2023-08-25 04:48:34 +00:00
|
|
|
can_be_paragraph: bool,
|
2023-09-03 19:44:18 +00:00
|
|
|
) -> impl for<'b, 'g, 'r, 's> Fn(RefContext<'b, 'g, 'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, ()>
|
|
|
|
{
|
2023-09-03 16:23:18 +00:00
|
|
|
move |context, input: OrgSource<'_>| _detect_element(context, input, can_be_paragraph)
|
2023-08-25 04:48:34 +00:00
|
|
|
}
|
|
|
|
|
2023-10-09 23:52:32 +00:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "tracing",
|
|
|
|
tracing::instrument(ret, level = "debug", skip(context))
|
|
|
|
)]
|
2023-09-03 19:44:18 +00:00
|
|
|
fn _detect_element<'b, 'g, 'r, 's>(
|
|
|
|
context: RefContext<'b, 'g, 'r, 's>,
|
2023-08-25 04:48:34 +00:00
|
|
|
input: OrgSource<'s>,
|
|
|
|
can_be_paragraph: bool,
|
|
|
|
) -> Res<OrgSource<'s>, ()> {
|
2023-09-11 16:28:15 +00:00
|
|
|
if alt((
|
2023-09-14 04:27:54 +00:00
|
|
|
parser_with_context!(detect_plain_list)(context),
|
2023-09-11 16:28:15 +00:00
|
|
|
detect_footnote_definition,
|
|
|
|
detect_diary_sexp,
|
|
|
|
detect_comment,
|
|
|
|
detect_fixed_width_area,
|
|
|
|
detect_table,
|
|
|
|
))(input)
|
|
|
|
.is_ok()
|
|
|
|
{
|
2023-08-25 04:48:34 +00:00
|
|
|
return Ok((input, ()));
|
|
|
|
}
|
|
|
|
if _element(context, input, can_be_paragraph).is_ok() {
|
|
|
|
return Ok((input, ()));
|
|
|
|
}
|
|
|
|
return Err(nom::Err::Error(CustomError::MyError(MyError(
|
|
|
|
"No element detected.".into(),
|
|
|
|
))));
|
|
|
|
}
|