added metadata parsing

This commit is contained in:
Daniel Fichtinger 2025-06-15 22:07:08 -04:00
parent 8f0e420a8f
commit 729bde2f55
6 changed files with 43 additions and 28 deletions

View file

@ -9,7 +9,6 @@ authors = [
requires-python = ">=3.12"
dependencies = [
"dacite>=1.9.2",
"python-dateutil>=2.9.0.post0",
"python-frontmatter>=1.1.0",
"typer>=0.16.0",
]
@ -39,6 +38,7 @@ executionEnvironments = [
# off | basic | standard | strict | recommended | all
typeCheckingMode = "recommended"
reportExplicitAny = false
reportUnusedCallResult = false
allowedUntypedLibraries = ["frontmatter"]
[tool.ruff]

View file

@ -1,7 +1,17 @@
from zona.models import Item
from zona.models import Item, Metadata
from pathlib import Path
import frontmatter
import os
from dacite import from_dict
def split_metadata(path: Path) -> tuple[Metadata, str]:
post = frontmatter.load(str(path))
meta = post.metadata
metadata = from_dict(
data_class=Metadata,
data=meta,
)
return metadata, post.content
def discover(root: str, out_dir: str) -> list[Item]:
@ -18,6 +28,9 @@ def discover(root: str, out_dir: str) -> list[Item]:
for path in base.rglob("*"):
if path.is_file():
print(f"{subdir}: {path.relative_to(root_path)}")
if path.name.endswith(".md"):
pass
# page = Frontmatter.read_file(path.name)
# TODO: build Item here
# items.append(...)
return items

View file

@ -1,12 +1,12 @@
from pathlib import Path
from datetime import datetime
from datetime import date
from dataclasses import dataclass
@dataclass
class Metadata:
title: str
date: datetime
date: date
description: str
# add more options later...

View file

@ -0,0 +1 @@

24
tests/test_metadata.py Normal file
View file

@ -0,0 +1,24 @@
from datetime import date
from zona.models import Metadata
from zona.builder import split_metadata
from pathlib import Path
def test_split_metadata(tmp_path: Path):
content = """---
title: Test Post
date: 2025-06-03
description: This is a test.
---
# Hello World
"""
test_file = tmp_path / "test.md"
test_file.write_text(content)
meta, content = split_metadata(test_file)
assert isinstance(meta, Metadata)
assert meta.title == "Test Post"
assert meta.description == "This is a test."
assert meta.date == date(2025, 6, 3)
assert content == "# Hello World"

23
uv.lock generated
View file

@ -133,18 +133,6 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/2f/de/afa024cbe022b1b318a3d224125aa24939e99b4ff6f22e0ba639a2eaee47/pytest-8.4.0-py3-none-any.whl", hash = "sha256:f40f825768ad76c0977cbacdf1fd37c6f7a468e460ea6a0636078f8972d4517e", size = 363797, upload-time = "2025-06-02T17:36:27.859Z" },
]
[[package]]
name = "python-dateutil"
version = "2.9.0.post0"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "six" },
]
sdist = { url = "https://files.pythonhosted.org/packages/66/c0/0c8b6ad9f17a802ee498c46e004a0eb49bc148f2fd230864601a86dcf6db/python-dateutil-2.9.0.post0.tar.gz", hash = "sha256:37dd54208da7e1cd875388217d5e00ebd4179249f90fb72437e91a35459a0ad3", size = 342432, upload-time = "2024-03-01T18:36:20.211Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/ec/57/56b9bcc3c9c6a792fcbaf139543cee77261f3651ca9da0c93f5c1221264b/python_dateutil-2.9.0.post0-py2.py3-none-any.whl", hash = "sha256:a8b2bc7bffae282281c8140a97d3aa9c14da0b136dfe83f850eea9a5f7470427", size = 229892, upload-time = "2024-03-01T18:36:18.57Z" },
]
[[package]]
name = "python-frontmatter"
version = "1.1.0"
@ -230,15 +218,6 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/e0/f9/0595336914c5619e5f28a1fb793285925a8cd4b432c9da0a987836c7f822/shellingham-1.5.4-py2.py3-none-any.whl", hash = "sha256:7ecfff8f2fd72616f7481040475a65b2bf8af90a56c89140852d1120324e8686", size = 9755, upload-time = "2023-10-24T04:13:38.866Z" },
]
[[package]]
name = "six"
version = "1.17.0"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "https://files.pythonhosted.org/packages/94/e7/b2c673351809dca68a0e064b6af791aa332cf192da575fd474ed7d6f16a2/six-1.17.0.tar.gz", hash = "sha256:ff70335d468e7eb6ec65b95b99d3a2836546063f63acc5171de367e834932a81", size = 34031, upload-time = "2024-12-04T17:35:28.174Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/b7/ce/149a00dd41f10bc29e5921b496af8b574d8413afcd5e30dfa0ed46c2cc5e/six-1.17.0-py2.py3-none-any.whl", hash = "sha256:4721f391ed90541fddacab5acf947aa0d3dc7d27b2e1e8eda2be8970586c3274", size = 11050, upload-time = "2024-12-04T17:35:26.475Z" },
]
[[package]]
name = "typer"
version = "0.16.0"
@ -269,7 +248,6 @@ version = "0.1.0"
source = { editable = "." }
dependencies = [
{ name = "dacite" },
{ name = "python-dateutil" },
{ name = "python-frontmatter" },
{ name = "typer" },
]
@ -284,7 +262,6 @@ dev = [
[package.metadata]
requires-dist = [
{ name = "dacite", specifier = ">=1.9.2" },
{ name = "python-dateutil", specifier = ">=2.9.0.post0" },
{ name = "python-frontmatter", specifier = ">=1.1.0" },
{ name = "typer", specifier = ">=0.16.0" },
]