From b2422bac552758b9bf710f14824f5d995fd56ba9 Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Fri, 19 Mar 2021 21:26:08 +0100 Subject: [PATCH] feat(render): introduce casing style to capitalize each word first letter. #625 --- espanso-render/src/lib.rs | 1 + espanso-render/src/renderer/mod.rs | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/espanso-render/src/lib.rs b/espanso-render/src/lib.rs index 4ae454f..436c8fd 100644 --- a/espanso-render/src/lib.rs +++ b/espanso-render/src/lib.rs @@ -73,6 +73,7 @@ impl Default for RenderOptions { pub enum CasingStyle { None, Capitalize, + CapitalizeWords, Uppercase, } diff --git a/espanso-render/src/renderer/mod.rs b/espanso-render/src/renderer/mod.rs index b4fd152..ce0afff 100644 --- a/espanso-render/src/renderer/mod.rs +++ b/espanso-render/src/renderer/mod.rs @@ -33,6 +33,7 @@ mod util; lazy_static! { pub(crate) static ref VAR_REGEX: Regex = Regex::new(r"\{\{\s*((?P\w+)(\.(?P(\w+)))?)\s*\}\}").unwrap(); + static ref WORD_REGEX: Regex = Regex::new(r"(\w+)").unwrap(); } pub(crate) struct DefaultRenderer { @@ -198,6 +199,19 @@ impl Renderer for DefaultRenderer { let mut v: Vec = body.chars().collect(); v[0] = v[0].to_uppercase().next().unwrap(); v.into_iter().collect() + }, + CasingStyle::CapitalizeWords => { + // Capitalize the first letter of each word + WORD_REGEX.replace_all(&body, |caps: &Captures| { + if let Some(word_match) = caps.get(0) { + let mut v: Vec = word_match.as_str().chars().collect(); + v[0] = v[0].to_uppercase().next().unwrap(); + let capitalized_word: String = v.into_iter().collect(); + capitalized_word + } else { + "".to_string() + } + }).to_string() } }; @@ -315,6 +329,15 @@ mod tests { assert!(matches!(res, RenderResult::Success(str) if str == "Plain body")); } + #[test] + fn no_variable_capitalize_words() { + let renderer = get_renderer(); + let res = renderer.render(&template_for_str("ordinary least squares, with other.punctuation !Marks"), &Default::default(), &RenderOptions { + casing_style: CasingStyle::CapitalizeWords, + }); + assert!(matches!(res, RenderResult::Success(str) if str == "Ordinary Least Squares, With Other.Punctuation !Marks")); + } + #[test] fn no_variable_uppercase() { let renderer = get_renderer();