File size: 3,146 Bytes
1517f5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

#!/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 <?php ... ?> 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'
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>Static Site</title>
  <link rel="stylesheet" href="/styles.css">
  <script src="/main.js" defer></script>
  <script src="/asset/include.js" defer></script>
</head>
<body>
  <div id="site-header"></div>
  <main style="padding: 2rem;">
    <h1>Static conversion complete</h1>
    <p>This page was generated because no index.php existed.</p>
    <p>Edit this file to match your layout.</p>
  </main>
  <div id="site-footer"></div>
</body>
</html>
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."