From f07d041eb9d4a972ec7035aab4bdb8384430ef29 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sun, 8 Oct 2023 16:51:44 -0400 Subject: [PATCH] Fix handling capitalization in macro names. --- org_mode_samples/object/macro/capitalize.org | 1 + src/compare/diff.rs | 8 +++---- src/parser/org_macro.rs | 6 ++--- src/types/object.rs | 25 ++++++++++++++------ src/types/util.rs | 8 +++++++ 5 files changed, 34 insertions(+), 14 deletions(-) create mode 100644 org_mode_samples/object/macro/capitalize.org diff --git a/org_mode_samples/object/macro/capitalize.org b/org_mode_samples/object/macro/capitalize.org new file mode 100644 index 0000000..8bbd61d --- /dev/null +++ b/org_mode_samples/object/macro/capitalize.org @@ -0,0 +1 @@ +{{{Foo(Bar,Baz)}}} diff --git a/src/compare/diff.rs b/src/compare/diff.rs index 137441e..5afb6e0 100644 --- a/src/compare/diff.rs +++ b/src/compare/diff.rs @@ -3077,20 +3077,20 @@ fn compare_org_macro<'b, 's>( rust, ( EmacsField::Required(":key"), - |r| Some(r.macro_name), + |r| Some(r.get_key()), compare_property_quoted_string ), ( EmacsField::Required(":value"), - |r| Some(r.macro_value), + |r| Some(r.value), compare_property_quoted_string ), ( EmacsField::Required(":args"), - |r| if r.macro_args.is_empty() { + |r| if r.args.is_empty() { None } else { - Some(r.get_macro_args()) + Some(r.get_args()) }, compare_property_list_of_quoted_string ) diff --git a/src/parser/org_macro.rs b/src/parser/org_macro.rs index dcc1566..9ead92e 100644 --- a/src/parser/org_macro.rs +++ b/src/parser/org_macro.rs @@ -37,13 +37,13 @@ pub(crate) fn org_macro<'b, 'g, 'r, 's>( remaining, OrgMacro { source: source.into(), - macro_name: macro_name.into(), - macro_args: macro_args + key: macro_name.into(), + args: macro_args .unwrap_or_else(|| Vec::with_capacity(0)) .into_iter() .map(|arg| arg.into()) .collect(), - macro_value: Into::<&str>::into(macro_value), + value: Into::<&str>::into(macro_value), }, )) } diff --git a/src/types/object.rs b/src/types/object.rs index 59198ef..2242043 100644 --- a/src/types/object.rs +++ b/src/types/object.rs @@ -5,6 +5,7 @@ use super::util::coalesce_whitespace_escaped; use super::util::coalesce_whitespace_if_line_break; use super::util::remove_line_break; use super::util::remove_whitespace_if_line_break; +use super::util::to_lowercase; use super::GetStandardProperties; use super::StandardProperties; @@ -149,12 +150,18 @@ pub struct AngleLink<'s> { #[derive(Debug, PartialEq)] pub struct OrgMacro<'s> { pub source: &'s str, - pub macro_name: &'s str, - /// The macro args from the source. + + /// The key from the source. /// - /// This does not take into account the post-processing that you would get from the upstream emacs org-mode AST. Use `get_macro_args` for an equivalent value. - pub macro_args: Vec<&'s str>, - pub macro_value: &'s str, + /// This does not take into account the post-processing that you would get from the upstream emacs org-mode AST. Use `get_key` for an equivalent value. + pub key: &'s str, + + /// The args from the source. + /// + /// This does not take into account the post-processing that you would get from the upstream emacs org-mode AST. Use `get_args` for an equivalent value. + pub args: Vec<&'s str>, + + pub value: &'s str, } #[derive(Debug, PartialEq)] @@ -739,8 +746,12 @@ impl<'s> AngleLink<'s> { } impl<'s> OrgMacro<'s> { - pub fn get_macro_args<'b>(&'b self) -> impl Iterator> + 'b { - self.macro_args + pub fn get_key<'b>(&'b self) -> Cow<'s, str> { + to_lowercase(self.key) + } + + pub fn get_args<'b>(&'b self) -> impl Iterator> + 'b { + self.args .iter() .map(|arg| coalesce_whitespace_escaped('\\', |c| ",".contains(c))(*arg)) } diff --git a/src/types/util.rs b/src/types/util.rs index d7e998d..99cf5ed 100644 --- a/src/types/util.rs +++ b/src/types/util.rs @@ -451,6 +451,14 @@ enum CoalesceWhitespaceEscaped { }, } +pub(crate) fn to_lowercase<'s>(input: &'s str) -> Cow<'s, str> { + if input.chars().any(|c| !c.is_lowercase()) { + Cow::Owned(input.to_lowercase()) + } else { + Cow::Borrowed(input) + } +} + #[cfg(test)] mod tests { use super::*;