Switch the keyword parsers over to using the settings from GlobalSettings.

This commit is contained in:
Tom Alexander 2023-10-15 14:22:01 -04:00
parent 7603b0a1cc
commit e7c3c7aab6
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
8 changed files with 72 additions and 35 deletions

View File

@ -3,4 +3,14 @@
#+begin_example #+begin_example
baz baz
#+end_example #+end_example
#+caption[lorem]: ipsum #+caption[lorem]: ipsum
#+caption[foo]: bar
#+begin_example
baz
#+end_example
#+header[foo]: bar
#+begin_example
baz
#+end_example

View File

@ -9,6 +9,7 @@ use super::keyword::affiliated_keyword;
use super::org_source::OrgSource; use super::org_source::OrgSource;
use super::util::maybe_consume_trailing_whitespace_if_not_exiting; use super::util::maybe_consume_trailing_whitespace_if_not_exiting;
use super::util::org_line_ending; use super::util::org_line_ending;
use crate::context::parser_with_context;
use crate::context::RefContext; use crate::context::RefContext;
use crate::error::Res; use crate::error::Res;
use crate::parser::util::get_consumed; use crate::parser::util::get_consumed;
@ -50,8 +51,11 @@ where
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub(crate) fn detect_diary_sexp<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, ()> { pub(crate) fn detect_diary_sexp<'b, 'g, 'r, 's>(
let (input, _) = many0(affiliated_keyword)(input)?; context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, ()> {
let (input, _) = many0(parser_with_context!(affiliated_keyword)(context))(input)?;
tuple((start_of_line, tag("%%(")))(input)?; tuple((start_of_line, tag("%%(")))(input)?;
Ok((input, ())) Ok((input, ()))
} }

View File

@ -56,7 +56,8 @@ fn _element<'b, 'g, 'r, 's>(
input: OrgSource<'s>, input: OrgSource<'s>,
can_be_paragraph: bool, can_be_paragraph: bool,
) -> Res<OrgSource<'s>, Element<'s>> { ) -> Res<OrgSource<'s>, Element<'s>> {
let (post_affiliated_keywords_input, affiliated_keywords) = many0(affiliated_keyword)(input)?; let (post_affiliated_keywords_input, affiliated_keywords) =
many0(parser_with_context!(affiliated_keyword)(context))(input)?;
let mut affiliated_keywords = affiliated_keywords.into_iter(); let mut affiliated_keywords = affiliated_keywords.into_iter();
@ -272,13 +273,14 @@ fn _detect_element<'b, 'g, 'r, 's>(
input: OrgSource<'s>, input: OrgSource<'s>,
can_be_paragraph: bool, can_be_paragraph: bool,
) -> Res<OrgSource<'s>, ()> { ) -> Res<OrgSource<'s>, ()> {
// TODO: unify parsing of affiliated keywords like we did for the element parser.
if alt(( if alt((
parser_with_context!(detect_plain_list)(context), parser_with_context!(detect_plain_list)(context),
detect_footnote_definition, parser_with_context!(detect_footnote_definition)(context),
detect_diary_sexp, parser_with_context!(detect_diary_sexp)(context),
detect_comment, detect_comment,
detect_fixed_width_area, parser_with_context!(detect_fixed_width_area)(context),
detect_table, parser_with_context!(detect_table)(context),
))(input) ))(input)
.is_ok() .is_ok()
{ {

View File

@ -90,8 +90,11 @@ fn fixed_width_area_line<'b, 'g, 'r, 's>(
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub(crate) fn detect_fixed_width_area<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, ()> { pub(crate) fn detect_fixed_width_area<'b, 'g, 'r, 's>(
let (input, _) = many0(affiliated_keyword)(input)?; context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, ()> {
let (input, _) = many0(parser_with_context!(affiliated_keyword)(context))(input)?;
tuple(( tuple((
start_of_line, start_of_line,
space0, space0,

View File

@ -125,7 +125,7 @@ fn footnote_definition_end<'b, 'g, 'r, 's>(
let (remaining, source) = alt(( let (remaining, source) = alt((
recognize(tuple(( recognize(tuple((
parser_with_context!(maybe_consume_trailing_whitespace)(context), parser_with_context!(maybe_consume_trailing_whitespace)(context),
detect_footnote_definition, parser_with_context!(detect_footnote_definition)(context),
))), ))),
recognize(tuple(( recognize(tuple((
start_of_line, start_of_line,
@ -139,8 +139,11 @@ fn footnote_definition_end<'b, 'g, 'r, 's>(
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub(crate) fn detect_footnote_definition<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, ()> { pub(crate) fn detect_footnote_definition<'b, 'g, 'r, 's>(
let (input, _) = many0(affiliated_keyword)(input)?; context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, ()> {
let (input, _) = many0(parser_with_context!(affiliated_keyword)(context))(input)?;
tuple((start_of_line, tag_no_case("[fn:"), label, tag("]")))(input)?; tuple((start_of_line, tag_no_case("[fn:"), label, tag("]")))(input)?;
Ok((input, ())) Ok((input, ()))
} }

View File

@ -21,6 +21,7 @@ use super::org_source::BracketDepth;
use super::org_source::OrgSource; use super::org_source::OrgSource;
use super::util::get_consumed; use super::util::get_consumed;
use super::util::maybe_consume_trailing_whitespace_if_not_exiting; use super::util::maybe_consume_trailing_whitespace_if_not_exiting;
use crate::context::parser_with_context;
use crate::context::Matcher; use crate::context::Matcher;
use crate::context::RefContext; use crate::context::RefContext;
use crate::error::CustomError; use crate::error::CustomError;
@ -30,15 +31,9 @@ use crate::parser::util::start_of_line;
use crate::types::AffiliatedKeywords; use crate::types::AffiliatedKeywords;
use crate::types::Keyword; use crate::types::Keyword;
const ORG_ELEMENT_AFFILIATED_KEYWORDS: [&'static str; 13] = [ pub(crate) fn filtered_keyword<'s, F: Fn(OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>>>(
"caption", "data", "headers", "header", "label", "name", "plot", "resname", "results",
"result", "source", "srcname", "tblname",
];
const ORG_ELEMENT_DUAL_KEYWORDS: [&'static str; 2] = ["caption", "results"];
pub(crate) fn filtered_keyword<F: Matcher>(
key_parser: F, key_parser: F,
) -> impl for<'s> Fn(OrgSource<'s>) -> Res<OrgSource<'s>, Keyword<'s>> { ) -> impl Fn(OrgSource<'s>) -> Res<OrgSource<'s>, Keyword<'s>> {
move |input| _filtered_keyword(&key_parser, input) move |input| _filtered_keyword(&key_parser, input)
} }
@ -46,7 +41,7 @@ pub(crate) fn filtered_keyword<F: Matcher>(
feature = "tracing", feature = "tracing",
tracing::instrument(ret, level = "debug", skip(key_parser)) tracing::instrument(ret, level = "debug", skip(key_parser))
)] )]
fn _filtered_keyword<'s, F: Matcher>( fn _filtered_keyword<'s, F: Fn(OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>>>(
key_parser: F, key_parser: F,
input: OrgSource<'s>, input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Keyword<'s>> { ) -> Res<OrgSource<'s>, Keyword<'s>> {
@ -113,8 +108,11 @@ where
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub(crate) fn affiliated_keyword<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, Keyword<'s>> { pub(crate) fn affiliated_keyword<'b, 'g, 'r, 's>(
filtered_keyword(affiliated_key)(input) context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Keyword<'s>> {
filtered_keyword(parser_with_context!(affiliated_key)(context))(input)
} }
#[cfg_attr( #[cfg_attr(
@ -149,18 +147,29 @@ fn regular_keyword_key<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn affiliated_key<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> { fn affiliated_key<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, OrgSource<'s>> {
alt(( alt((
recognize(tuple((dual_affiliated_key, tag("["), optval, tag("]")))), recognize(tuple((
plain_affiliated_key, parser_with_context!(dual_affiliated_key)(context),
tag("["),
optval,
tag("]"),
))),
parser_with_context!(plain_affiliated_key)(context),
export_keyword, export_keyword,
))(input) ))(input)
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn plain_affiliated_key<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> { fn plain_affiliated_key<'b, 'g, 'r, 's>(
for keyword in ORG_ELEMENT_AFFILIATED_KEYWORDS { context: RefContext<'b, 'g, 'r, 's>,
let result = tag_no_case::<_, _, CustomError<_>>(keyword)(input); input: OrgSource<'s>,
) -> Res<OrgSource<'s>, OrgSource<'s>> {
for keyword in context.get_global_settings().element_affiliated_keywords {
let result = tag_no_case::<_, _, CustomError<_>>(*keyword)(input);
match result { match result {
Ok((remaining, ent)) => { Ok((remaining, ent)) => {
return Ok((remaining, ent)); return Ok((remaining, ent));
@ -175,9 +184,12 @@ fn plain_affiliated_key<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSourc
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn dual_affiliated_key<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> { fn dual_affiliated_key<'b, 'g, 'r, 's>(
for keyword in ORG_ELEMENT_DUAL_KEYWORDS { context: RefContext<'b, 'g, 'r, 's>,
let result = tag_no_case::<_, _, CustomError<_>>(keyword)(input); input: OrgSource<'s>,
) -> Res<OrgSource<'s>, OrgSource<'s>> {
for keyword in context.get_global_settings().element_dual_keywords {
let result = tag_no_case::<_, _, CustomError<_>>(*keyword)(input);
match result { match result {
Ok((remaining, ent)) => { Ok((remaining, ent)) => {
return Ok((remaining, ent)); return Ok((remaining, ent));

View File

@ -59,7 +59,7 @@ pub(crate) fn detect_plain_list<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>, context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>, input: OrgSource<'s>,
) -> Res<OrgSource<'s>, ()> { ) -> Res<OrgSource<'s>, ()> {
let (input, _) = many0(affiliated_keyword)(input)?; let (input, _) = many0(parser_with_context!(affiliated_keyword)(context))(input)?;
if verify( if verify(
tuple(( tuple((
start_of_line, start_of_line,

View File

@ -93,8 +93,11 @@ where
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub(crate) fn detect_table<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, ()> { pub(crate) fn detect_table<'b, 'g, 'r, 's>(
let (input, _) = many0(affiliated_keyword)(input)?; context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, ()> {
let (input, _) = many0(parser_with_context!(affiliated_keyword)(context))(input)?;
tuple((start_of_line, space0, tag("|")))(input)?; tuple((start_of_line, space0, tag("|")))(input)?;
Ok((input, ())) Ok((input, ()))
} }