"""Sync author markdown content into the Hugo HTML builder. Run from anywhere: python skills-scripts/sync_content_to_hugo.py The root ``content/`` folder is the source of truth. This script copies those markdown files into ``hugo-html-builder/content/`` and applies the small Hugo rendering adaptations needed for this site. """ from __future__ import annotations import re import shutil from pathlib import Path REPO_ROOT = Path(__file__).resolve().parents[1] AUTHOR_CONTENT = REPO_ROOT / "content" HUGO_CONTENT = REPO_ROOT / "hugo-html-builder" / "content" CONTACT_FORM = """
""" HEALTHZ_PAGE = """--- title: "Health Check" layout: "healthz" url: "/healthz/" _build: list: never render: always --- ok """ SECTION_INDEXES = { Path("community-summaries") / "_index.md": """--- title: "CO+ Platform Summaries" summary: "iNDX CO+ platform summaries organized by audience." --- """, } MOJIBAKE_REPLACEMENTS = { "’": "’", "‘": "‘", "“": "“", "â€\x9d": "”", "–": "–", "—": "—", "…": "…", "Â\xa0": " ", "Â": "", } def read_text(path: Path) -> str: return path.read_text(encoding="utf-8-sig") def write_text(path: Path, content: str) -> None: path.parent.mkdir(parents=True, exist_ok=True) path.write_text(normalize_text(content), encoding="utf-8", newline="\n") def normalize_text(content: str) -> str: for broken, fixed in MOJIBAKE_REPLACEMENTS.items(): content = content.replace(broken, fixed) return content def remove_frontmatter_field(content: str, field_name: str) -> str: return re.sub(rf"(?m)^{re.escape(field_name)}:\s*.*\n?", "", content, count=1) def upsert_frontmatter_field(content: str, field_name: str, value: str) -> str: field_line = f'{field_name}: "{value}"' if re.search(rf"(?m)^{re.escape(field_name)}:\s*", content): return re.sub(rf"(?m)^{re.escape(field_name)}:\s*.*$", field_line, content, count=1) match = re.search(r"(?m)^slug:\s*.*$", content) if match: return content[: match.end()] + "\n" + field_line + content[match.end() :] if content.startswith("---\n"): end = content.find("\n---", 4) if end != -1: return content[:end] + "\n" + field_line + content[end:] return content def extract_slug(content: str, fallback: str) -> str: match = re.search(r'(?m)^slug:\s*"?([^"\n\r]+)"?\s*$', content) return match.group(1).strip() if match else fallback def copy_author_markdown() -> None: if not AUTHOR_CONTENT.exists(): raise FileNotFoundError(f"Missing source content directory: {AUTHOR_CONTENT}") if HUGO_CONTENT.exists(): shutil.rmtree(HUGO_CONTENT) HUGO_CONTENT.mkdir(parents=True) for source in AUTHOR_CONTENT.rglob("*.md"): relative = source.relative_to(AUTHOR_CONTENT) target = HUGO_CONTENT / relative write_text(target, read_text(source)) def build_homepage() -> None: source = AUTHOR_CONTENT / "home.md" if not source.exists(): return content = read_text(source) content = remove_frontmatter_field(content, "slug") content = re.sub(r"