new-site/scripts/inject_order_headers.py

104 lines
4.5 KiB
Python

#!/usr/bin/env python3
"""Inject the vertical-specific <VerticalOrderHeader> into every order page.
Single source of truth for slug -> vertical. Idempotent: re-running it
normalizes import + placement and migrates the legacy <TruckingOrderHeader /> to
<VerticalOrderHeader vertical="trucking" />.
Placement: immediately after the closing </section> of the page's title block
(the same spot the trucking header used), or as the first child of <main>/the
top wrapper if no obvious anchor is found.
"""
import os, re, sys
ORDER_DIR = os.path.join(os.path.dirname(__file__), "..", "site", "src", "pages", "order")
# slug -> vertical. Anything not listed defaults to "trucking" (the original set).
VERTICAL = {
# ── Telecom / FCC ─────────────────────────────────────────────
"bdc-broadband": "telecom", "bdc-filing": "telecom", "bdc-voice": "telecom",
"calea-ssi": "telecom", "cdr-analysis": "telecom",
"cores-frn-registration": "telecom", "cpni-certification": "telecom",
"fcc-499a": "telecom", "fcc-499a-499q": "telecom", "fcc-499-initial": "telecom",
"fcc-63-11-notification": "telecom", "fcc-compliance-checkup": "telecom",
"fcc-full-compliance": "telecom", "ocn-registration": "telecom",
"stir-shaken": "telecom",
# ── Healthcare ────────────────────────────────────────────────
"medicare-enrollment": "healthcare", "npi-reactivation": "healthcare",
"npi-revalidation": "healthcare", "nppes-update": "healthcare",
"oig-sam-screening": "healthcare", "provider-compliance-bundle": "healthcare",
"rmd-filing": "healthcare",
# ── Corporate ─────────────────────────────────────────────────
"dc-agent": "corporate", "foreign-qualification": "corporate",
}
IMPORT_LINE = 'import VerticalOrderHeader from "../../components/VerticalOrderHeader.astro";'
LEGACY_IMPORT_RE = re.compile(r'^\s*import\s+TruckingOrderHeader\s+from\s+["\'][^"\']+["\'];\s*$', re.M)
NEW_IMPORT_RE = re.compile(r'^\s*import\s+VerticalOrderHeader\s+from\s+["\'][^"\']+["\'];\s*$', re.M)
def vertical_for(slug: str) -> str:
return VERTICAL.get(slug, "trucking")
def process(path: str) -> str:
slug = os.path.basename(path)[:-len(".astro")]
v = vertical_for(slug)
src = open(path).read()
tag = f'<VerticalOrderHeader vertical="{v}" />'
# 1) Imports: ensure VerticalOrderHeader import, drop the legacy one.
src = LEGACY_IMPORT_RE.sub("", src)
if not NEW_IMPORT_RE.search(src):
# insert after the Base import (first import line in frontmatter)
m = re.search(r'^(import\s+Base\s+from[^\n]*\n)', src, re.M)
if m:
src = src[:m.end()] + IMPORT_LINE + "\n" + src[m.end():]
else:
# fallback: after frontmatter opening ---
src = re.sub(r'^---\n', "---\n" + IMPORT_LINE + "\n", src, count=1)
# 2) Replace any legacy header usage with the new one.
src = re.sub(r'<TruckingOrderHeader\s*/>', tag, src)
# 3) If the new tag isn't placed yet, insert it after the first </section>
# in the body (the title block) — matching the trucking convention.
if tag not in src:
m = re.search(r'</section>\s*\n', src)
if m:
src = src[:m.end()] + "\n " + tag + "\n" + src[m.end():]
else:
# last resort: right after <main ...> or the first wrapper div
m = re.search(r'(<main[^>]*>\s*\n)', src)
if m:
src = src[:m.end()] + " " + tag + "\n" + src[m.end():]
else:
print(f" WARN: no anchor in {slug}, skipped placement", file=sys.stderr)
# tidy: collapse 3+ blank lines created by import removal
src = re.sub(r'\n{3,}', "\n\n", src)
return src
def main():
order = os.path.normpath(ORDER_DIR)
changed = 0
counts = {}
for fn in sorted(os.listdir(order)):
if not fn.endswith(".astro"):
continue
path = os.path.join(order, fn)
slug = fn[:-len(".astro")]
v = vertical_for(slug)
new = process(path)
old = open(path).read()
if new != old:
open(path, "w").write(new)
changed += 1
counts[v] = counts.get(v, 0) + 1
print(f"order pages processed; changed={changed}")
print("vertical counts:", dict(sorted(counts.items())))
if __name__ == "__main__":
main()