From 71982435f3f9ad32dc2bc4d4ceac1616f4520b11 Mon Sep 17 00:00:00 2001 From: Daniel Fichtinger Date: Sat, 21 Jun 2025 16:41:14 -0400 Subject: [PATCH] add function to convert filename to title --- src/zona/util.py | 7 +++++++ tests/test_util.py | 9 +++++++++ 2 files changed, 16 insertions(+) create mode 100644 tests/test_util.py diff --git a/src/zona/util.py b/src/zona/util.py index cb3427b..6c96d87 100644 --- a/src/zona/util.py +++ b/src/zona/util.py @@ -1,5 +1,6 @@ from pathlib import Path from shutil import copy2 +import string def ensure_parents(target: Path): @@ -11,3 +12,9 @@ def copy_static_file(src: Path, dst: Path): """Copy a static file from one location to another.""" ensure_parents(dst) copy2(src, dst) + + +def filename_to_title(path: Path) -> str: + name = path.stem + words = name.replace("-", " ").replace("_", " ") + return string.capwords(words) diff --git a/tests/test_util.py b/tests/test_util.py new file mode 100644 index 0000000..a32e99b --- /dev/null +++ b/tests/test_util.py @@ -0,0 +1,9 @@ +from pathlib import Path +from zona import util + + +def test_title(tmp_path: Path): + a = tmp_path / "my-first-post.md" + b = tmp_path / "Writing_emails_Post.md" + assert util.filename_to_title(a) == "My First Post" + assert util.filename_to_title(b) == "Writing Emails Post"