#!/usr/bin/env bash set -euo pipefail echo "==> Staticize: PHP → HTML conversion" # 1) Backup all PHP files preserving structure if [ -d "_backup_php" ]; then echo "[skip] _backup_php already exists" else echo "Backing up *.php to _backup_php/" find . -type f -name "*.php" -print0 | tar --null -T - -cf - | (mkdir -p _backup_php && tar -xf - -C _backup_php) fi # 2) Convert each .php to .html stripping PHP blocks echo "Converting PHP to HTML (strip blocks)" php_count=0 while IFS= read -r -d '' f; do php_count=$((php_count+1)) rel="${f#./}" out="${rel%.php}.html" outdir="$(dirname "$out")" mkdir -p "$outdir" # Remove all PHP code blocks including multiline # Note: GNU sed required. macOS: use 'gsed' (brew install gnu-sed) or run via Docker. sed -E 's/<\?php[\s\S]*?\?>//g' "$f" > "$out" done < <(find . -type f -name "*.php" -print0) echo "Converted $php_count PHP files." # 3) Rewrite internal links .php → .html across common text assets echo "Rewriting internal links from .php to .html" files_to_patch=$(find . -type f \( -name "*.html" -o -name "*.htm" -o -name "*.css" -o -name "*.js" -o -name "*.md" -o -name "*.txt" -o -name "*.xml" -o -name ".htaccess" \) ! -path "./_backup_php/*") while IFS= read -r file; do # Use in-place edit (GNU sed). macOS: gsed -i sed -i -E 's/\.php\b/.html/g' "$file" || true done <<< "$files_to_patch" # 4) Ensure index.html at repo root if [ ! -f "./index.html" ]; then if [ -f "./index.php" ]; then echo "Creating index.html from index.php" sed -E 's/<\?php[\s\S]*?\?>//g' "./index.php" > "./index.html" else echo "No index.php found; creating a minimal index.html" cat > ./index.html <<'EOF' Static Site

Static conversion complete

This page was generated because no index.php existed.

Edit this file to match your layout.

EOF fi fi # 5) Provision include system if not present mkdir -p asset/partials if [ ! -f "asset/include.js" ]; then cat > asset/include.js <<'EOF' async function inject(id, url){ const el = document.getElementById(id); if (!el) return; const html = await (await fetch(url)).text(); el.innerHTML = html; } document.addEventListener('DOMContentLoaded', () => { inject('site-header', '/asset/partials/header.html'); inject('site-footer', '/asset/partials/footer.html'); }); EOF fi : > asset/partials/header.html : > asset/partials/footer.html # 6) Summary echo "==> Staticize complete." echo " - Backup of originals: _backup_php/" echo " - Entry point ensured: index.html" echo " - Include system: asset/include.js + asset/partials/{header,footer}.html" echo "Next: review pages, commit, push, and set Space runtime to Static."