2023-04-23 01:45:18 +00:00
|
|
|
use nom::branch::alt;
|
|
|
|
use nom::combinator::map;
|
|
|
|
use nom::multi::many0;
|
|
|
|
|
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-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;
|
|
|
|
use super::element::Element;
|
2023-04-22 02:04:22 +00:00
|
|
|
use super::fixed_width_area::fixed_width_area;
|
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-04-22 04:09:14 +00:00
|
|
|
use super::source::SetSource;
|
2023-04-22 03:54:54 +00:00
|
|
|
use super::util::get_consumed;
|
|
|
|
use super::util::maybe_consume_trailing_whitespace_if_not_exiting;
|
2023-04-21 20:10:56 +00:00
|
|
|
use super::Context;
|
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-04-21 20:10:56 +00:00
|
|
|
use crate::parser::parser_with_context::parser_with_context;
|
|
|
|
use crate::parser::table::org_mode_table;
|
|
|
|
|
2023-08-25 04:48:34 +00:00
|
|
|
pub const fn element(
|
2023-04-22 04:55:31 +00:00
|
|
|
can_be_paragraph: bool,
|
2023-08-23 04:30:26 +00:00
|
|
|
) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, Element<'s>> {
|
|
|
|
move |context: Context, input: OrgSource<'_>| _element(context, input, can_be_paragraph)
|
2023-04-22 23:15:29 +00:00
|
|
|
}
|
|
|
|
|
2023-08-11 00:04:59 +00:00
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
2023-04-22 23:15:29 +00:00
|
|
|
fn _element<'r, 's>(
|
|
|
|
context: Context<'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-04-22 23:15:29 +00:00
|
|
|
let plain_list_matcher = parser_with_context!(plain_list)(context);
|
|
|
|
let greater_block_matcher = parser_with_context!(greater_block)(context);
|
|
|
|
let dynamic_block_matcher = parser_with_context!(dynamic_block)(context);
|
|
|
|
let footnote_definition_matcher = parser_with_context!(footnote_definition)(context);
|
|
|
|
let comment_matcher = parser_with_context!(comment)(context);
|
|
|
|
let drawer_matcher = parser_with_context!(drawer)(context);
|
|
|
|
let table_matcher = parser_with_context!(org_mode_table)(context);
|
|
|
|
let verse_block_matcher = parser_with_context!(verse_block)(context);
|
|
|
|
let comment_block_matcher = parser_with_context!(comment_block)(context);
|
|
|
|
let example_block_matcher = parser_with_context!(example_block)(context);
|
|
|
|
let export_block_matcher = parser_with_context!(export_block)(context);
|
|
|
|
let src_block_matcher = parser_with_context!(src_block)(context);
|
|
|
|
let clock_matcher = parser_with_context!(clock)(context);
|
|
|
|
let diary_sexp_matcher = parser_with_context!(diary_sexp)(context);
|
|
|
|
let fixed_width_area_matcher = parser_with_context!(fixed_width_area)(context);
|
|
|
|
let horizontal_rule_matcher = parser_with_context!(horizontal_rule)(context);
|
|
|
|
let keyword_matcher = parser_with_context!(keyword)(context);
|
2023-08-29 20:56:07 +00:00
|
|
|
let affiliated_keyword_matcher = parser_with_context!(affiliated_keyword)(context);
|
2023-04-22 23:15:29 +00:00
|
|
|
let paragraph_matcher = parser_with_context!(paragraph)(context);
|
|
|
|
let latex_environment_matcher = parser_with_context!(latex_environment)(context);
|
2023-04-22 20:56:36 +00:00
|
|
|
|
2023-08-29 20:56:07 +00:00
|
|
|
// TODO: Affiliated keywords cannot be on comments, clocks, headings, inlinetasks, items, node properties, planning, property drawers, sections, and table rows
|
|
|
|
let (remaining, mut affiliated_keywords) = many0(affiliated_keyword_matcher)(input)?;
|
2023-04-22 23:15:29 +00:00
|
|
|
let (remaining, mut element) = match alt((
|
|
|
|
map(plain_list_matcher, Element::PlainList),
|
|
|
|
map(greater_block_matcher, Element::GreaterBlock),
|
|
|
|
map(dynamic_block_matcher, Element::DynamicBlock),
|
|
|
|
map(footnote_definition_matcher, Element::FootnoteDefinition),
|
|
|
|
map(comment_matcher, Element::Comment),
|
|
|
|
map(drawer_matcher, Element::Drawer),
|
|
|
|
map(table_matcher, Element::Table),
|
|
|
|
map(verse_block_matcher, Element::VerseBlock),
|
|
|
|
map(comment_block_matcher, Element::CommentBlock),
|
|
|
|
map(example_block_matcher, Element::ExampleBlock),
|
|
|
|
map(export_block_matcher, Element::ExportBlock),
|
|
|
|
map(src_block_matcher, Element::SrcBlock),
|
|
|
|
map(clock_matcher, Element::Clock),
|
|
|
|
map(diary_sexp_matcher, Element::DiarySexp),
|
|
|
|
map(fixed_width_area_matcher, Element::FixedWidthArea),
|
|
|
|
map(horizontal_rule_matcher, Element::HorizontalRule),
|
|
|
|
map(latex_environment_matcher, Element::LatexEnvironment),
|
|
|
|
))(remaining)
|
|
|
|
{
|
|
|
|
the_ok @ Ok(_) => the_ok,
|
|
|
|
Err(_) => {
|
|
|
|
if can_be_paragraph {
|
|
|
|
match map(paragraph_matcher, Element::Paragraph)(remaining) {
|
|
|
|
the_ok @ Ok(_) => the_ok,
|
|
|
|
Err(_) => {
|
|
|
|
affiliated_keywords.clear();
|
|
|
|
map(keyword_matcher, Element::Keyword)(input)
|
2023-04-22 05:34:34 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-22 23:15:29 +00:00
|
|
|
} else {
|
|
|
|
affiliated_keywords.clear();
|
|
|
|
map(keyword_matcher, Element::Keyword)(input)
|
2023-04-22 04:55:31 +00:00
|
|
|
}
|
2023-04-22 23:15:29 +00:00
|
|
|
}
|
|
|
|
}?;
|
2023-04-21 20:10:56 +00:00
|
|
|
|
2023-04-22 23:15:29 +00:00
|
|
|
let (remaining, _trailing_ws) =
|
|
|
|
maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?;
|
2023-04-22 03:54:54 +00:00
|
|
|
|
2023-04-22 23:15:29 +00:00
|
|
|
let source = get_consumed(input, remaining);
|
2023-08-23 04:30:26 +00:00
|
|
|
element.set_source(source.into());
|
2023-04-22 03:54:54 +00:00
|
|
|
|
2023-04-22 23:15:29 +00:00
|
|
|
Ok((remaining, element))
|
2023-04-21 20:10:56 +00:00
|
|
|
}
|
2023-08-25 04:48:34 +00:00
|
|
|
|
|
|
|
pub const fn detect_element(
|
|
|
|
can_be_paragraph: bool,
|
|
|
|
) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, ()> {
|
|
|
|
move |context: Context, input: OrgSource<'_>| _detect_element(context, input, can_be_paragraph)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
|
|
fn _detect_element<'r, 's>(
|
|
|
|
context: Context<'r, 's>,
|
|
|
|
input: OrgSource<'s>,
|
|
|
|
can_be_paragraph: bool,
|
|
|
|
) -> Res<OrgSource<'s>, ()> {
|
|
|
|
if detect_plain_list(context, input).is_ok() {
|
|
|
|
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(),
|
|
|
|
))));
|
|
|
|
}
|