add function to convert filename to title
This commit is contained in:
parent
0b4de161b5
commit
71982435f3
2 changed files with 16 additions and 0 deletions
|
@ -1,5 +1,6 @@
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from shutil import copy2
|
from shutil import copy2
|
||||||
|
import string
|
||||||
|
|
||||||
|
|
||||||
def ensure_parents(target: Path):
|
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."""
|
"""Copy a static file from one location to another."""
|
||||||
ensure_parents(dst)
|
ensure_parents(dst)
|
||||||
copy2(src, dst)
|
copy2(src, dst)
|
||||||
|
|
||||||
|
|
||||||
|
def filename_to_title(path: Path) -> str:
|
||||||
|
name = path.stem
|
||||||
|
words = name.replace("-", " ").replace("_", " ")
|
||||||
|
return string.capwords(words)
|
||||||
|
|
9
tests/test_util.py
Normal file
9
tests/test_util.py
Normal file
|
@ -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"
|
Loading…
Add table
Add a link
Reference in a new issue