MakerFLOSS/scripts/mkdocs_hooks.py

55 lines
1.8 KiB
Python
Raw Normal View History

"""MkDocs build hook: render a Specs section on each hardware or service page from its YAML frontmatter."""
from __future__ import annotations
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
from gen_overview import fmt_cpu, fmt_nic, fmt_ram, fmt_storage # noqa: E402
def _table(rows: list[tuple[str, str]]) -> str:
lines = ["| Field | Value |", "|---|---|"]
for label, value in rows:
if not value:
continue
lines.append(f"| {label} | {value} |")
return "\n".join(lines)
def _hardware_page(meta: dict, body: str) -> str:
rows = [
("Location", meta.get("location")),
("CPU", fmt_cpu(meta)),
("RAM", fmt_ram(meta)),
("Storage", fmt_storage(meta)),
("NIC", fmt_nic(meta)),
("Status", meta.get("status")),
]
return f"# {meta['hostname']}\n\n## Specs\n\n{_table(rows)}\n\n{body}"
def _service_page(meta: dict, body: str) -> str:
url = meta.get("url")
upstream = meta.get("upstream")
rows = [
("Kind", meta.get("kind")),
("Host", meta.get("host")),
("URL", f"[{url}]({url})" if url else ""),
("Tech", meta.get("tech")),
("Upstream", f"[{upstream}]({upstream})" if upstream else ""),
("TLS", meta.get("tls")),
("Status", meta.get("status")),
]
return f"# {meta['name']}\n\n## Service\n\n{_table(rows)}\n\n{body}"
def on_page_markdown(markdown, page, config, files): # noqa: ARG001
meta = page.meta or {}
src = (page.file.src_uri or page.file.src_path or "").replace("\\", "/")
if src.startswith("hardware/") and meta.get("hostname"):
return _hardware_page(meta, markdown)
if src.startswith("services/") and meta.get("name"):
return _service_page(meta, markdown)
return markdown