180 lines
5.0 KiB
Python
180 lines
5.0 KiB
Python
"""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 = """<form class="contact-form" id="email-subscribe" method="POST" action="/api/contact">
|
||
<label>Name
|
||
<input type="text" name="name" autocomplete="name" required>
|
||
</label>
|
||
<label>Email
|
||
<input type="email" name="email" autocomplete="email" required>
|
||
</label>
|
||
<label>Message
|
||
<textarea name="message" rows="5" required></textarea>
|
||
</label>
|
||
<div class="g-recaptcha" data-sitekey="<recaptcha-site-key>"></div>
|
||
<label class="form-hidden">Company
|
||
<input type="text" name="company" tabindex="-1" autocomplete="off">
|
||
</label>
|
||
<button type="submit">Send</button>
|
||
<p class="form-status" role="status" aria-live="polite"></p>
|
||
</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"<form>\s*", CONTACT_FORM + "\n", content, count=1)
|
||
write_text(HUGO_CONTENT / "_index.md", content)
|
||
|
||
|
||
def build_healthz() -> None:
|
||
write_text(HUGO_CONTENT / "healthz.md", HEALTHZ_PAGE)
|
||
|
||
|
||
def add_section_indexes() -> None:
|
||
for relative, content in SECTION_INDEXES.items():
|
||
directory = (HUGO_CONTENT / relative).parent
|
||
if directory.exists():
|
||
write_text(HUGO_CONTENT / relative, content)
|
||
|
||
|
||
def add_flat_community_urls() -> None:
|
||
community_root = HUGO_CONTENT / "community-summaries"
|
||
if not community_root.exists():
|
||
return
|
||
|
||
for page in community_root.rglob("*.md"):
|
||
if page.name == "_index.md":
|
||
continue
|
||
|
||
content = read_text(page)
|
||
relative_dir = page.parent.relative_to(HUGO_CONTENT).as_posix()
|
||
slug = extract_slug(content, page.stem)
|
||
url = f"/{relative_dir}/{slug}.html"
|
||
content = upsert_frontmatter_field(content, "url", url)
|
||
write_text(page, content)
|
||
|
||
|
||
def main() -> None:
|
||
copy_author_markdown()
|
||
build_homepage()
|
||
build_healthz()
|
||
add_section_indexes()
|
||
add_flat_community_urls()
|
||
|
||
synced = len(list(HUGO_CONTENT.rglob("*.md")))
|
||
print(f"Synced {synced} markdown files into {HUGO_CONTENT}")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|