use nom::branch::alt; use nom::bytes::complete::is_not; use nom::bytes::complete::tag; 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::map; use nom::combinator::opt; use nom::combinator::recognize; use nom::multi::many_till; use nom::sequence::tuple; use super::org_source::OrgSource; use super::Context; use crate::error::CustomError; use crate::error::MyError; use crate::error::Res; use crate::parser::exiting::ExitClass; use crate::parser::greater_element::NodeProperty; use crate::parser::greater_element::PropertyDrawer; use crate::parser::parser_context::ContextElement; use crate::parser::parser_context::ExitMatcherNode; use crate::parser::parser_with_context::parser_with_context; use crate::parser::plain_text::plain_text; use crate::parser::util::exit_matcher_parser; use crate::parser::util::get_consumed; use crate::parser::util::immediate_in_section; use crate::parser::util::maybe_consume_trailing_whitespace_if_not_exiting; use crate::parser::util::start_of_line; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn property_drawer<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, PropertyDrawer<'s>> { if immediate_in_section(context, "property-drawer") { return Err(nom::Err::Error(CustomError::MyError(MyError( "Cannot nest objects of the same element".into(), )))); } let ( remaining, (_start_of_line, _leading_whitespace, _open_tag, _trailing_whitespace, _line_ending), ) = tuple(( start_of_line, space0, tag_no_case(":PROPERTIES:"), space0, line_ending, ))(input)?; let parser_context = context .with_additional_node(ContextElement::ConsumeTrailingWhitespace(true)) .with_additional_node(ContextElement::Context("property-drawer")) .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Alpha, exit_matcher: &property_drawer_end, })); let node_property_matcher = parser_with_context!(node_property)(&parser_context); let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context); let (remaining, (children, _exit_contents)) = many_till(node_property_matcher, exit_matcher)(remaining)?; let (remaining, _end) = property_drawer_end(&parser_context, remaining)?; let (remaining, _trailing_ws) = maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?; let source = get_consumed(input, remaining); Ok(( remaining, PropertyDrawer { source: source.into(), children, }, )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn property_drawer_end<'r, 's>( _context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, OrgSource<'s>> { recognize(tuple(( start_of_line, space0, tag_no_case(":end:"), space0, alt((line_ending, eof)), )))(input) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn node_property<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, NodeProperty<'s>> { let (remaining, (_start_of_line, _leading_whitespace, _open_colon, _name, _close_colon)) = tuple(( start_of_line, space0, tag(":"), parser_with_context!(node_property_name)(context), tag(":"), ))(input)?; match tuple(( space0::, nom::error::Error>>, line_ending, ))(remaining) { Ok((remaining, _ws)) => { let source = get_consumed(input, remaining); Ok(( remaining, NodeProperty { source: source.into(), value: None, }, )) } Err(_) => { let (remaining, (_ws, value, _line_ending)) = tuple((space1, is_not("\r\n"), line_ending))(remaining)?; let source = get_consumed(input, remaining); Ok(( remaining, NodeProperty { source: source.into(), value: Some(value.into()), }, )) } } } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn node_property_name<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, OrgSource<'s>> { let parser_context = context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Beta, exit_matcher: &node_property_name_end, })); let (remaining, name) = recognize(tuple(( map(parser_with_context!(plain_text)(&parser_context), |pt| { pt.source }), opt(tag("+")), )))(input)?; Ok((remaining, name)) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn node_property_name_end<'r, 's>( _context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, OrgSource<'s>> { alt((tag("+:"), tag(":")))(input) }