Skip to content

Commit 5a445cd

Browse files
committed
feat(new): use title case for norg documents
Also small fixes to the regex used while inserting authors and categories in the norg document metadata
1 parent bed95de commit 5a445cd

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

Cargo.lock

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ tracing = "0.1.41"
5151
tracing-subscriber = { version = "0.3.19", features = ["ansi", "chrono", "env-filter"] }
5252
colored = "3.0.0"
5353
local-ip-address = "0.6.3"
54+
titlecase = "3.3.0"
5455

5556
[dev-dependencies]
5657
mockall = "0.13.1"

src/cmd/new.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use eyre::{bail, eyre, Context, Result};
66
use indoc::formatdoc;
77
use inquire::Text;
88
use regex::Regex;
9+
use titlecase::titlecase;
910
use tracing::{debug, info, instrument, warn};
1011
use whoami::username;
1112

@@ -71,9 +72,11 @@ fn generate_content_title(base_path: &Path, full_path: &Path) -> String {
7172
.iter()
7273
.filter(|c| *c != "index.norg")
7374
.map(|c| {
74-
c.to_string_lossy()
75-
.trim_end_matches(".norg")
76-
.replace(['-', '_'], " ")
75+
titlecase(
76+
&c.to_string_lossy()
77+
.trim_end_matches(".norg")
78+
.replace(['-', '_'], " "),
79+
)
7780
})
7881
.collect::<Vec<_>>();
7982

@@ -98,7 +101,7 @@ fn generate_content_title(base_path: &Path, full_path: &Path) -> String {
98101
#[instrument(level = "debug", skip(path, title))]
99102
async fn create_norg_document(path: &Path, title: &str) -> Result<()> {
100103
debug!("Creating new norg document: {}", path.display());
101-
let re = Regex::new(", ?")?;
104+
let re = Regex::new(r"[,\s+?]+")?;
102105
let creation_date = Local::now().to_rfc3339_opts(SecondsFormat::Secs, false);
103106

104107
// Prompt norg file metadata
@@ -145,8 +148,8 @@ async fn create_norg_document(path: &Path, title: &str) -> Result<()> {
145148
* {title}
146149
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
147150
labore et dolore magna aliqua. Lobortis scelerisque fermentum dui faucibus in ornare."#,
148-
re.replace(&authors, "\n "),
149-
re.replace(&categories, "\n "),
151+
re.replace_all(&authors, "\n "),
152+
re.replace_all(&categories, "\n "),
150153
);
151154
tokio::fs::write(path, content)
152155
.await

0 commit comments

Comments
 (0)