#!/usr/bin/env python3 """ Simple build script for static site generation. Replaces template variables with values from config/services.json """ import json import os from datetime import datetime from pathlib import Path # Paths BASE_DIR = Path(__file__).parent TEMPLATE_DIR = BASE_DIR / "src" CONFIG_FILE = BASE_DIR / "config" / "services.json" OUTPUT_DIR = BASE_DIR / "dist" def load_config(): """Load configuration from JSON file""" with open(CONFIG_FILE, 'r') as f: return json.load(f) def generate_services_html(services): """Generate HTML for services list""" html = "" for service in services: # Handle URLs that may or may not have protocol url = service['url'] if not url.startswith(('http://', 'https://')): href = f"https://{url}" else: href = url # Display URL without protocol for cleaner look url = url.replace('https://', '').replace('http://', '') html += f"""
{service['name']}
{service['description']}
URL: {url}
""" return html.strip() def build_site(): """Main build function""" # Load config config = load_config() # Read template template_path = TEMPLATE_DIR / "index.html.template" with open(template_path, 'r') as f: template = f.read() # Generate services HTML services_html = generate_services_html(config['services']) # Prepare replacements replacements = { '{{SITE_TITLE}}': config['site']['title'], '{{SITE_SUBTITLE}}': config['site']['subtitle'], '{{SITE_DESCRIPTION}}': config['site']['description'], '{{SERVICES_LIST}}': services_html, '{{FOOTER_TEXT}}': config['footer']['text'], '{{BUILD_TIMESTAMP}}': datetime.now().strftime('%Y-%m-%d %H:%M:%S UTC') } # Replace variables output = template for placeholder, value in replacements.items(): output = output.replace(placeholder, value) # Create output directory OUTPUT_DIR.mkdir(exist_ok=True) # Write output output_path = OUTPUT_DIR / "index.html" with open(output_path, 'w') as f: f.write(output) # Copy CSS file css_source = TEMPLATE_DIR / "styles.css" css_dest = OUTPUT_DIR / "styles.css" if css_source.exists(): with open(css_source, 'r') as src, open(css_dest, 'w') as dst: dst.write(src.read()) print(f"✓ Site built successfully!") print(f"✓ Output: {output_path}") print(f"✓ CSS copied to: {css_dest}") if __name__ == "__main__": build_site()