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"