Files
floorithardwoodfloors.com/build_services.py
T
Concept Agent 9907a2ab7d update
2026-05-07 11:45:16 +02:00

87 lines
2.8 KiB
Python

#!/usr/bin/env python3
"""
build_services.py — Service page generator for floorithardwoodfloors.com
Reads data/services.json + services/_template.html
Outputs flat .html files: services/{slug}.html
Usage:
python3 build_services.py
python3 build_services.py --slug floor-refinishing
python3 build_services.py --list
"""
import json
import sys
import argparse
from pathlib import Path
from datetime import datetime
SITE_ROOT = Path(__file__).parent
DATA_FILE = SITE_ROOT / "data" / "services.json"
TEMPLATE_FILE = SITE_ROOT / "services" / "_template.html"
OUTPUT_DIR = SITE_ROOT / "services"
def render_page(service: dict, template: str) -> str:
html = template
for key, value in service.items():
if isinstance(value, str):
html = html.replace(f"{{{{{key}}}}}", value)
return html
def build_one(service: dict, template: str, verbose: bool = True) -> Path:
slug = service["slug"]
output_path = OUTPUT_DIR / f"{slug}.html"
html = render_page(service, template)
output_path.write_text(html, encoding="utf-8")
if verbose:
print(f" Built: services/{slug}.html ({service.get('service_name', slug)})")
return output_path
def main():
parser = argparse.ArgumentParser(description="Build flat service HTML pages from JSON database.")
parser.add_argument("--slug", help="Build a single service by slug (e.g. floor-refinishing)")
parser.add_argument("--list", action="store_true", help="List all services in database")
args = parser.parse_args()
if not DATA_FILE.exists():
print(f"ERROR: data file not found: {DATA_FILE}")
sys.exit(1)
if not TEMPLATE_FILE.exists():
print(f"ERROR: template not found: {TEMPLATE_FILE}")
sys.exit(1)
services = json.loads(DATA_FILE.read_text(encoding="utf-8"))
template = TEMPLATE_FILE.read_text(encoding="utf-8")
if args.list:
print(f"\nServices in database ({len(services)} total):")
for svc in services:
print(f" {svc['slug']:30s} {svc.get('service_name','')}")
return
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
if args.slug:
matches = [s for s in services if s["slug"] == args.slug]
if not matches:
print(f"ERROR: slug '{args.slug}' not found in database.")
sys.exit(1)
print(f"\nBuilding single service: {args.slug}")
build_one(matches[0], template)
else:
print(f"\nBuilding {len(services)} service pages...")
built = []
for svc in services:
path = build_one(svc, template)
built.append(path)
print(f"\nDone. {len(built)} pages written to services/")
print(f"Completed at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
if __name__ == "__main__":
main()