danish-dynaword / tests /readme_parsing.py
KennethEnevoldsen's picture
Added tests to validate datasheets
c6d9de7 unverified
raw
history blame
839 Bytes
from pathlib import Path
from typing import Any
import yaml
def read_frontmatter_and_body(file_path: Path) -> tuple[dict[str, Any], str]:
with file_path.open("r") as f:
content = f.read()
if content.startswith("---"):
end_idx = content.find("---", 3)
if end_idx != -1:
frontmatter = content[3:end_idx].strip()
return yaml.safe_load(frontmatter), content[end_idx:]
raise ValueError(f"No frontmatter found in file: {file_path}")
def get_tag_idx(readme: str, tag: str):
tag_start = f"<!-- START-{tag} -->"
tag_end = f"<!-- END-{tag} -->"
start_idx = readme.find(tag_start)
end_idx = readme.find(tag_end)
if end_idx != -1 and start_idx != -1 and start_idx < end_idx:
return start_idx, end_idx
raise ValueError(f"tag ({tag}) not found in readme")