From cb3b364430fc262e7d9fbcec469e2c81a6f6ea61 Mon Sep 17 00:00:00 2001 From: Marekkon5 Date: Tue, 9 Jan 2024 14:46:03 +0100 Subject: [PATCH] Add titlecase to renamer --- Cargo.lock | 18 ++++++++++++++++++ crates/onetagger-renamer/Cargo.toml | 1 + crates/onetagger-renamer/src/docs.rs | 3 ++- crates/onetagger-renamer/src/parser.rs | 5 +++++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 21417ebe..d76334f2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4005,6 +4005,12 @@ dependencies = [ "libc", ] +[[package]] +name = "joinery" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72167d68f5fce3b8655487b8038691a3c9984ee769590f93f2a631f4ad64e4f5" + [[package]] name = "jpeg-decoder" version = "0.3.0" @@ -5182,6 +5188,7 @@ dependencies = [ "pulldown-cmark", "regex", "serde", + "titlecase", ] [[package]] @@ -8036,6 +8043,17 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" +[[package]] +name = "titlecase" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38397a8cdb017cfeb48bf6c154d6de975ac69ffeed35980fde199d2ee0842042" +dependencies = [ + "joinery", + "lazy_static", + "regex", +] + [[package]] name = "tokio" version = "1.35.1" diff --git a/crates/onetagger-renamer/Cargo.toml b/crates/onetagger-renamer/Cargo.toml index f11fc018..3baa6d0a 100644 --- a/crates/onetagger-renamer/Cargo.toml +++ b/crates/onetagger-renamer/Cargo.toml @@ -11,6 +11,7 @@ pad = "0.1" dunce = "1.0" regex = "1.7" anyhow = "1.0" +titlecase = "2.2" lazy_static = "1.4" pulldown-cmark = "0.9" diff --git a/crates/onetagger-renamer/src/docs.rs b/crates/onetagger-renamer/src/docs.rs index 8c55fa17..57eba665 100644 --- a/crates/onetagger-renamer/src/docs.rs +++ b/crates/onetagger-renamer/src/docs.rs @@ -42,7 +42,7 @@ lazy_static! { SymbolDoc::prop("last", "Get the last item in an array"), ]; - pub static ref FUNCTIONS: [SymbolDoc; 14] = [ + pub static ref FUNCTIONS: [SymbolDoc; 15] = [ SymbolDoc::f("lower", "Convert all to lowercase", vec![]), SymbolDoc::f("lowercase", "Convert all to lowercase", vec![]), SymbolDoc::f("upper", "Convert all to uppercase", vec![]), @@ -50,6 +50,7 @@ lazy_static! { SymbolDoc::f("slice", "Take a range out of array or substring", vec![DocParameter::n("start", true), DocParameter::n("end", false)]), SymbolDoc::f("range", "Take a range out of array or substring", vec![DocParameter::n("start", true), DocParameter::n("end", false)]), SymbolDoc::f("capitalize", "Convert first letter to uppercase", vec![]), + SymbolDoc::f("titlecase", "Conver to titlecase (Capitalize Every Word)", vec![]), SymbolDoc::f("replace", "Replace text. `from` parameter accepts regex expressions.", vec![DocParameter::s("from", true), DocParameter::s("to", true)]), SymbolDoc::f("pad", "Pad on the left side with given character to reach given length", vec![DocParameter::s("char", true), DocParameter::n("length", true)]), SymbolDoc::f("sort", "Sort the array alphabetically", vec![]), diff --git a/crates/onetagger-renamer/src/parser.rs b/crates/onetagger-renamer/src/parser.rs index abd90f12..c28621c4 100644 --- a/crates/onetagger-renamer/src/parser.rs +++ b/crates/onetagger-renamer/src/parser.rs @@ -699,6 +699,11 @@ impl Token for TokenFunction { }; Some(Data::String(o)) }, + // Convert to title case + "titlecase" => { + let s = data.to_string(&config.separator); + Some(Data::String(titlecase::titlecase(&s))) + }, // Replace string with string "replace" => { let from = self.param_str(0, true)?;