erick.is/_utils.py
Erick Ruiz de Chavez 40121b1c49 Refactor site content and worflow
Since the site is now Gemini first, it doesn't make sense to use Markdown. In this commit I am refactoring the site generator to remove lowdown and frontmatter dependencies.
2025-01-08 05:38:01 -05:00

50 lines
1.1 KiB
Python

from dataclasses import dataclass, field
import yaml
@dataclass
class Article:
metadata: dict = field(default_factory=dict)
content: str = ""
def load(file_name) -> Article:
content = ""
with open(file=file_name, mode="r", encoding="utf8") as file:
content = file.read()
return loads(content)
def loads(content: str) -> Article:
lines = content.splitlines()
start = -1
end = -1
for index, line in enumerate(lines):
if line.startswith("---"):
if start == -1:
start = index
elif end == -1:
end = index
break
if start == -1 or end == -1:
raise ValueError("Missing frontmatter delimiters")
metadata = lines[start + 1 : end]
content = lines[end + 1 :]
metadata = yaml.safe_load("\n".join(metadata))
if not metadata:
metadata = {}
return Article(
metadata=metadata,
content="\n".join(content),
)
def dumps(article: Article) -> str:
return f"""---\n{yaml.dump(article.metadata)}---\n{article.content}\n"""