add directory processing
This commit is contained in:
parent
729bde2f55
commit
2251d3495b
3 changed files with 75 additions and 15 deletions
|
@ -6,7 +6,9 @@ from dacite import from_dict
|
|||
|
||||
def split_metadata(path: Path) -> tuple[Metadata, str]:
|
||||
post = frontmatter.load(str(path))
|
||||
# TODO: handle missing metadata
|
||||
meta = post.metadata
|
||||
# TODO: handle malformed metadata
|
||||
metadata = from_dict(
|
||||
data_class=Metadata,
|
||||
data=meta,
|
||||
|
@ -14,23 +16,33 @@ def split_metadata(path: Path) -> tuple[Metadata, str]:
|
|||
return metadata, post.content
|
||||
|
||||
|
||||
def discover(root: str, out_dir: str) -> list[Item]:
|
||||
root_path = Path(root)
|
||||
required_dirs = {"content", "static", "templates"}
|
||||
found_dirs = {p.name for p in root_path.iterdir() if p.is_dir()}
|
||||
def discover(root: Path, out_dir: Path) -> list[Item]:
|
||||
required_dirs = {"content", "templates"}
|
||||
found_dirs = {p.name for p in root.iterdir() if p.is_dir()}
|
||||
missing = required_dirs - found_dirs
|
||||
assert not missing, f"Missing required directories: {', '.join(missing)}"
|
||||
|
||||
items: list[Item] = []
|
||||
|
||||
for subdir in required_dirs:
|
||||
base = root_path / subdir
|
||||
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(...)
|
||||
subdir = "content"
|
||||
base = root / subdir
|
||||
for path in base.rglob("*"):
|
||||
if path.is_file():
|
||||
print(f"{subdir}: {path.relative_to(root)}")
|
||||
if path.name.endswith(".md") and not path.is_relative_to(
|
||||
root / "content" / "static"
|
||||
):
|
||||
meta, content = split_metadata(path)
|
||||
else:
|
||||
meta = None
|
||||
content = path.read_bytes()
|
||||
destination = out_dir / path.relative_to(base)
|
||||
item = Item(
|
||||
source=path,
|
||||
destination=destination,
|
||||
url=str(destination.relative_to(out_dir)),
|
||||
metadata=meta,
|
||||
content=content,
|
||||
)
|
||||
items.append(item)
|
||||
return items
|
||||
|
|
|
@ -17,3 +17,4 @@ class Item:
|
|||
destination: Path
|
||||
url: str # relative to site root
|
||||
metadata: Metadata | None # frontmatter
|
||||
content: str | bytes
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue