30declare(strict_types = 1);
32if ($argc !== 2 || !is_dir($argv[1])) {
33 fwrite(STDERR,
"Expected the generated HTML directory.\n");
39 $html = file_get_contents($file);
40 if (str_contains($html,
'href="#index_') && !str_contains($html,
'id="index_')) {
41 $html = preg_replace(
'/^<div class="qindex">[^\r\n]*<\/div>\r?\n/m',
'', $html);
43 preg_match_all(
'/\b(?:id|name)="([^"]*)"/', $html, $ids);
44 $pages[$file] = [
'html' => $html,
'ids' => array_fill_keys($ids[1],
true)];
47foreach (
$pages as $file => $page) {
48 $html = preg_replace_callback(
'/\bhref="([^"#]*)#(index_\$|index__)"/',
49 static function(array $match) use ($file,
$pages):
string {
50 $path = html_entity_decode($match[1], ENT_QUOTES | ENT_HTML5,
'UTF-8');
51 if (preg_match(
'/^(?:[a-z][a-z0-9+.-]*:|\/\/)/i', $path)) {
return $match[0]; }
52 $url = parse_url($path);
53 $path = rawurldecode($url[
'path'] ??
'');
54 $target = $path ===
'' ? $file : realpath(dirname($file).
'/'.$path);
55 $anchor = [
'index_$' =>
'index__24',
'index__' =>
'index__5F'][$match[2]];
56 if (!isset(
$pages[$target][
'ids'][$match[2]]) && isset(
$pages[$target][
'ids'][$anchor])) {
57 return 'href="'.$match[1].
'#'.$anchor.
'"';
61 file_put_contents($file, $html);
62 $pages[$file][
'html'] = $html;
65foreach (
$pages as $file => $page) {
66 preg_match_all(
'/\b(?:href|src|data)="([^"]*)"/', $page[
'html'], $links);
67 foreach ($links[1] as $link) {
68 $link = html_entity_decode($link, ENT_QUOTES | ENT_HTML5,
'UTF-8');
69 if ($link ===
'' || preg_match(
'/^(?:[a-z][a-z0-9+.-]*:|\/\/)/i', $link)) {
continue; }
70 $url = parse_url($link);
71 $path = rawurldecode($url[
'path'] ??
'');
72 $target = $path ===
'' ? $file : realpath(dirname($file).
'/'.$path);
73 if ($target ===
false || !file_exists($target)) {
74 $errors[] = basename($file).
': missing local resource '.$link;
75 } elseif (isset($url[
'fragment'],
$pages[$target]) &&
76 !isset(
$pages[$target][
'ids'][rawurldecode($url[
'fragment'])])) {
77 $errors[] = basename($file).
': missing anchor '.$link;
82 fwrite(STDERR, implode(PHP_EOL, array_unique(
$errors)).PHP_EOL);
85echo
'Verified '.count(
$pages).
" HTML pages and their local links/assets.\n";
foreach(glob( $directory.'/*.html') as $file) foreach($pages as $file=> $page) $errors