|
Server : LiteSpeed System : Linux terra.hostitbro.com 5.14.0-611.54.3.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Thu May 7 16:31:24 EDT 2026 x86_64 User : outerorb ( 1091) PHP Version : 8.1.34 Disable Function : mail Directory : /home2/outerorb/public_html/tools/ | |
|
Path: /home2/outerorb/public_html/tools/align-h1-bgtitle.php
Size: 2.93 KB
Permissions: 0666
<?php
// Align H1s to the visible bg-title on pages.
// Usage: php tools/align-h1-bgtitle.php
$root = realpath(__DIR__ . '/../');
$exclude = ['inc', 'assets', 'vendor', '.git', 'node_modules', 'tools', 'team', 'projects', 'blog'];
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($root));
$changed = [];
$skipped = [];
foreach ($it as $f) {
if (!$f->isFile()) continue;
if (strtolower($f->getExtension()) !== 'php') continue;
$path = str_replace('\\','/',$f->getPathname());
$rel = substr($path, strlen($root) + 1);
$parts = explode('/', $rel);
if (in_array($parts[0], $exclude, true)) { $skipped[] = $rel; continue; }
$content = @file_get_contents($path);
if ($content === false) continue;
// find first bg-title span
if (!preg_match('/<span[^>]*class=["\']bg-title["\'][^>]*>(.*?)<\/span>/is', $content, $m)) continue;
$bgRaw = trim($m[1]);
$bgText = trim(strip_tags($bgRaw));
if ($bgText === '') continue;
$modified = false;
if (preg_match('/<h1\b([^>]*)>(.*?)<\/h1>/is', $content, $h)) {
$hInner = trim(strip_tags($h[2]));
$normBg = preg_replace('/\s+/',' ', html_entity_decode($bgText));
$normH = preg_replace('/\s+/',' ', html_entity_decode($hInner));
if ($normBg !== $normH) {
// preserve attributes, replace inner HTML with plain bg text
$attrs = $h[1];
$newH = '<h1' . $attrs . '>' . htmlspecialchars($bgText, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') . '</h1>';
$pos = strpos($content, $h[0]);
if ($pos !== false) {
if (!file_exists($path . '.bak')) copy($path, $path . '.bak');
$content = substr_replace($content, $newH, $pos, strlen($h[0]));
$modified = true;
}
}
} else {
// insert H1 after the bg-title span
$insert = $m[0] . "\n <h1 class=\"title\">" . htmlspecialchars($bgText, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') . "</h1>";
$newContent = preg_replace('/' . preg_quote($m[0], '/') . '/s', $insert, $content, 1);
if ($newContent !== null && $newContent !== $content) {
if (!file_exists($path . '.bak')) copy($path, $path . '.bak');
$content = $newContent;
$modified = true;
}
}
if ($modified) {
file_put_contents($path, $content);
$changed[] = $rel;
}
}
$report = [];
$report[] = "# H1 Alignment Report";
$report[] = "Generated: " . date('c');
$report[] = "";
$report[] = "Changed files:";
if (empty($changed)) {
$report[] = "- (none)";
} else {
foreach ($changed as $c) $report[] = "- $c";
}
$report[] = "";
$report[] = "Skipped (excluded dirs):";
foreach (array_unique($skipped) as $s) $report[] = "- $s";
$out = implode("\n", $report) . "\n";
file_put_contents($root . '/tools/align-h1-report.md', $out);
echo $out;
?>