-
Notifications
You must be signed in to change notification settings - Fork 269
/
Copy pathcheck_refs.php
51 lines (42 loc) · 1.29 KB
/
check_refs.php
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
<?php error_reporting(E_ALL);
require __DIR__ . '/util.php';
// Collect all valid anchors
$anchors = [];
foreach (spec_files() as $fileName => $path) {
$contents = file_get_contents($path);
foreach (heading_info($contents) as $info) {
$fullAnchor = $fileName . '#' . $info['anchor'];
$anchors[$fullAnchor] = true;
}
// Collect manual anchors as well
if (preg_match_all('/<a name="([^"]+)">/', $contents, $matches)) {
foreach ($matches[1] as $anchor) {
$fullAnchor = $fileName . '#' . $anchor;
$anchors[$fullAnchor] = true;
}
}
}
// Find unknown anchor references
$foundUnknown = false;
foreach (spec_files() as $fileName => $path) {
$contents = file_get_contents($path);
if (!preg_match_all('/\]\(([^)]+)\)/', $contents, $matches)) {
continue;
}
foreach ($matches[1] as $anchor) {
if (false === strpos($anchor, '#')) {
continue;
}
if (!preg_match('/^(#|\d{2})/', $anchor)) {
continue;
}
if ('#' === $anchor[0]) {
$anchor = $fileName . $anchor;
}
if (!isset($anchors[$anchor])) {
$foundUnknown = true;
echo "Unknown anchor $anchor in $fileName\n";
}
}
}
exit($foundUnknown ? 1 : 0);