use nom::bytes::complete::tag; use nom::bytes::complete::tag_no_case; use nom::character::complete::anychar; use nom::character::complete::line_ending; use nom::character::complete::one_of; use nom::character::complete::space0; use nom::combinator::opt; use nom::combinator::recognize; use nom::combinator::verify; use nom::multi::many_till; #[cfg(feature = "tracing")] use tracing::span; use super::org_source::OrgSource; use super::Context; use crate::error::CustomError; use crate::error::Res; use crate::parser::exiting::ExitClass; use crate::parser::parser_context::ContextElement; use crate::parser::parser_context::ExitMatcherNode; use crate::parser::parser_context::InlineSourceBlockBracket; use crate::parser::parser_with_context::parser_with_context; use crate::parser::util::exit_matcher_parser; use crate::parser::util::get_consumed; use crate::parser::InlineSourceBlock; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn inline_source_block<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, InlineSourceBlock<'s>> { let (remaining, _) = tag_no_case("src_")(input)?; let (remaining, _) = lang(context, remaining)?; let (remaining, _header1) = opt(parser_with_context!(header)(context))(remaining)?; let (remaining, _body) = body(context, remaining)?; let (remaining, _) = space0(remaining)?; let source = get_consumed(input, remaining); Ok(( remaining, InlineSourceBlock { source: source.into(), }, )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn lang<'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: &lang_end, })); let (remaining, lang) = recognize(many_till( verify(anychar, |c| !(c.is_whitespace() || "[{".contains(*c))), parser_with_context!(exit_matcher_parser)(&parser_context), ))(input)?; Ok((remaining, lang)) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn lang_end<'r, 's>( _context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, OrgSource<'s>> { recognize(one_of("[{"))(input) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn header<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, OrgSource<'s>> { let (remaining, _) = tag("[")(input)?; let parser_context = context .with_additional_node(ContextElement::InlineSourceBlockBracket( InlineSourceBlockBracket { position: remaining, depth: 0, }, )) .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Beta, exit_matcher: &header_end, })); let (remaining, header_contents) = recognize(many_till( anychar, parser_with_context!(exit_matcher_parser)(&parser_context), ))(remaining)?; let (remaining, _) = tag("]")(remaining)?; Ok((remaining, header_contents)) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn header_end<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, OrgSource<'s>> { let context_depth = get_bracket_depth(context) .expect("This function should only be called from inside an inline source block header."); let text_since_context_entry = get_consumed(context_depth.position, input); let mut current_depth = context_depth.depth; for c in Into::<&str>::into(text_since_context_entry).chars() { match c { '[' => { current_depth += 1; } ']' if current_depth == 0 => { panic!("Exceeded inline source block header bracket depth.") } ']' if current_depth > 0 => { current_depth -= 1; } _ => {} } } if current_depth == 0 { let close_bracket = tag::<&str, OrgSource<'_>, CustomError>>("]")(input); if close_bracket.is_ok() { return close_bracket; } } line_ending(input) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn body<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, OrgSource<'s>> { let (remaining, _) = tag("{")(input)?; let parser_context = context .with_additional_node(ContextElement::InlineSourceBlockBracket( InlineSourceBlockBracket { position: remaining, depth: 0, }, )) .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Beta, exit_matcher: &body_end, })); let (remaining, body_contents) = recognize(many_till( anychar, parser_with_context!(exit_matcher_parser)(&parser_context), ))(remaining)?; let (remaining, _) = { #[cfg(feature = "tracing")] let span = span!( tracing::Level::DEBUG, "outside end body", remaining = Into::<&str>::into(remaining) ); #[cfg(feature = "tracing")] let _enter = span.enter(); tag("}")(remaining)? }; Ok((remaining, body_contents)) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn body_end<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, OrgSource<'s>> { let context_depth = get_bracket_depth(context) .expect("This function should only be called from inside an inline source block body."); let text_since_context_entry = get_consumed(context_depth.position, input); let mut current_depth = context_depth.depth; for c in Into::<&str>::into(text_since_context_entry).chars() { match c { '{' => { current_depth += 1; } '}' if current_depth == 0 => { panic!("Exceeded inline source block body bracket depth.") } '}' if current_depth > 0 => { current_depth -= 1; } _ => {} } } { #[cfg(feature = "tracing")] let span = span!( tracing::Level::DEBUG, "inside end body", remaining = Into::<&str>::into(input), current_depth = current_depth ); #[cfg(feature = "tracing")] let _enter = span.enter(); if current_depth == 0 { let close_bracket = tag::<&str, OrgSource<'_>, CustomError>>("}")(input); if close_bracket.is_ok() { return close_bracket; } } } line_ending(input) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn get_bracket_depth<'r, 's>( context: Context<'r, 's>, ) -> Option<&'r InlineSourceBlockBracket<'s>> { for node in context.iter() { match node.get_data() { ContextElement::InlineSourceBlockBracket(depth) => return Some(depth), _ => {} } } None }