2023-04-22 17:12:49 -04:00
|
|
|
use nom::branch::alt;
|
|
|
|
use nom::bytes::complete::tag;
|
|
|
|
use nom::bytes::complete::tag_no_case;
|
|
|
|
use nom::bytes::complete::take_while1;
|
2023-07-18 23:27:48 -04:00
|
|
|
use nom::character::complete::anychar;
|
2023-04-22 17:12:49 -04:00
|
|
|
use nom::character::complete::line_ending;
|
|
|
|
use nom::character::complete::space0;
|
|
|
|
use nom::combinator::eof;
|
2023-07-18 23:27:48 -04:00
|
|
|
use nom::combinator::peek;
|
|
|
|
use nom::combinator::recognize;
|
2023-10-04 20:01:09 -04:00
|
|
|
use nom::multi::many0;
|
2023-07-18 23:27:48 -04:00
|
|
|
use nom::multi::many_till;
|
2023-04-22 17:12:49 -04:00
|
|
|
use nom::sequence::tuple;
|
|
|
|
|
2023-10-11 14:44:25 -04:00
|
|
|
use super::affiliated_keyword::parse_affiliated_keywords;
|
2023-10-04 20:01:09 -04:00
|
|
|
use super::keyword::affiliated_keyword;
|
2023-08-23 00:30:26 -04:00
|
|
|
use super::org_source::OrgSource;
|
2023-04-22 17:12:49 -04:00
|
|
|
use super::util::get_consumed;
|
2023-10-06 11:45:15 -04:00
|
|
|
use super::util::maybe_consume_trailing_whitespace_if_not_exiting;
|
2023-09-03 11:05:34 -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-22 16:56:36 -04:00
|
|
|
use crate::error::Res;
|
2023-07-18 23:27:48 -04:00
|
|
|
use crate::parser::util::exit_matcher_parser;
|
2023-04-22 17:12:49 -04:00
|
|
|
use crate::parser::util::start_of_line;
|
2023-09-03 11:05:34 -04:00
|
|
|
use crate::types::LatexEnvironment;
|
2023-04-22 16:56:36 -04:00
|
|
|
|
2023-10-09 18:00:48 -04:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "tracing",
|
|
|
|
tracing::instrument(ret, level = "debug", skip(context))
|
|
|
|
)]
|
2023-09-11 13:13:28 -04:00
|
|
|
pub(crate) fn latex_environment<'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>, LatexEnvironment<'s>> {
|
2023-10-06 11:45:15 -04:00
|
|
|
let (remaining, affiliated_keywords) = many0(affiliated_keyword)(input)?;
|
|
|
|
let value_start = remaining;
|
|
|
|
start_of_line(remaining)?;
|
|
|
|
let (remaining, _leading_whitespace) = space0(remaining)?;
|
2023-04-22 17:12:49 -04:00
|
|
|
let (remaining, (_opening, name, _open_close_brace, _ws, _line_ending)) = tuple((
|
|
|
|
tag_no_case(r#"\begin{"#),
|
|
|
|
name,
|
|
|
|
tag("}"),
|
|
|
|
space0,
|
|
|
|
line_ending,
|
|
|
|
))(remaining)?;
|
|
|
|
|
2023-08-23 00:30:26 -04:00
|
|
|
let latex_environment_end_specialized = latex_environment_end(name.into());
|
2023-09-03 11:05:34 -04:00
|
|
|
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
|
|
|
|
class: ExitClass::Beta,
|
|
|
|
exit_matcher: &latex_environment_end_specialized,
|
|
|
|
});
|
|
|
|
let parser_context = context.with_additional_node(&parser_context);
|
2023-04-22 17:12:49 -04:00
|
|
|
|
2023-09-03 12:07:51 -04:00
|
|
|
let (remaining, _contents) = contents(&latex_environment_end_specialized)(context, remaining)?;
|
2023-04-22 17:12:49 -04:00
|
|
|
let (remaining, _end) = latex_environment_end_specialized(&parser_context, remaining)?;
|
2023-10-06 11:45:15 -04:00
|
|
|
let value_end = remaining;
|
2023-04-22 17:12:49 -04:00
|
|
|
|
2023-10-06 11:45:15 -04:00
|
|
|
let (remaining, _trailing_ws) =
|
|
|
|
maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?;
|
2023-04-22 17:12:49 -04:00
|
|
|
let source = get_consumed(input, remaining);
|
2023-10-06 11:45:15 -04:00
|
|
|
let value = get_consumed(value_start, value_end);
|
2023-08-23 00:30:26 -04:00
|
|
|
Ok((
|
|
|
|
remaining,
|
|
|
|
LatexEnvironment {
|
|
|
|
source: source.into(),
|
2023-10-11 14:44:25 -04:00
|
|
|
affiliated_keywords: parse_affiliated_keywords(
|
|
|
|
context.get_global_settings(),
|
|
|
|
affiliated_keywords,
|
|
|
|
),
|
2023-10-06 11:45:15 -04:00
|
|
|
value: value.into(),
|
2023-08-23 00:30:26 -04:00
|
|
|
},
|
|
|
|
))
|
2023-04-22 17:12:49 -04:00
|
|
|
}
|
|
|
|
|
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-22 17:12:49 -04:00
|
|
|
take_while1(|c: char| c.is_alphanumeric() || c == '*')(input)
|
|
|
|
}
|
|
|
|
|
2023-09-03 12:07:51 -04:00
|
|
|
fn contents<F: ContextMatcher>(end_matcher: F) -> impl ContextMatcher {
|
|
|
|
move |context, input| _contents(&end_matcher, context, input)
|
|
|
|
}
|
|
|
|
|
2023-08-10 20:04:59 -04:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "tracing",
|
2023-10-09 18:00:48 -04:00
|
|
|
tracing::instrument(ret, level = "debug", skip(context, end_matcher))
|
2023-08-10 20:04:59 -04:00
|
|
|
)]
|
2023-09-03 15:44:18 -04:00
|
|
|
fn _contents<'b, 'g, 'r, 's, F: ContextMatcher>(
|
2023-07-18 23:27:48 -04:00
|
|
|
end_matcher: F,
|
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>, OrgSource<'s>> {
|
2023-07-18 23:27:48 -04:00
|
|
|
let (remaining, source) = recognize(many_till(
|
|
|
|
anychar,
|
|
|
|
peek(alt((
|
|
|
|
parser_with_context!(exit_matcher_parser)(context),
|
2023-09-03 12:07:51 -04:00
|
|
|
parser_with_context!(&end_matcher)(context),
|
2023-07-18 23:27:48 -04:00
|
|
|
))),
|
|
|
|
))(input)?;
|
|
|
|
|
|
|
|
Ok((remaining, source))
|
|
|
|
}
|
|
|
|
|
2023-09-03 11:05:34 -04:00
|
|
|
fn latex_environment_end(current_name: &str) -> impl ContextMatcher {
|
2023-04-22 17:12:49 -04:00
|
|
|
let current_name_lower = current_name.to_lowercase();
|
2023-09-03 11:05:34 -04:00
|
|
|
move |context, input: OrgSource<'_>| {
|
2023-04-22 19:12:48 -04:00
|
|
|
_latex_environment_end(context, input, current_name_lower.as_str())
|
2023-04-22 17:12:49 -04:00
|
|
|
}
|
2023-04-22 16:56:36 -04:00
|
|
|
}
|
2023-04-22 19:12:48 -04:00
|
|
|
|
2023-10-09 18:00:48 -04:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "tracing",
|
|
|
|
tracing::instrument(ret, level = "debug", skip(_context))
|
|
|
|
)]
|
2023-09-03 15:44:18 -04:00
|
|
|
fn _latex_environment_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 15:44:18 -04:00
|
|
|
current_name_lower: &'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-22 19:12:48 -04:00
|
|
|
let (remaining, _leading_whitespace) = space0(input)?;
|
|
|
|
let (remaining, (_begin, _name, _close_brace, _ws, _line_ending)) = tuple((
|
|
|
|
tag_no_case(r#"\end{"#),
|
|
|
|
tag_no_case(current_name_lower),
|
|
|
|
tag("}"),
|
|
|
|
space0,
|
|
|
|
alt((eof, line_ending)),
|
|
|
|
))(remaining)?;
|
|
|
|
let source = get_consumed(input, remaining);
|
|
|
|
Ok((remaining, source))
|
|
|
|
}
|