-
Notifications
You must be signed in to change notification settings - Fork 269
/
Copy pathutil.php
49 lines (41 loc) · 1.22 KB
/
util.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
<?php
/* Iterator of spec files, using $fileName => $path. */
function spec_files($skipFiles = []) {
$dir = __DIR__ . '/../spec/';
$files = scandir($dir);
foreach ($files as $file) {
if (pathinfo($file, PATHINFO_EXTENSION) != 'md') {
continue;
}
if ($file == '00-specification-for-php.md'
|| in_array($file, $skipFiles)) {
continue;
}
yield $file => $dir . $file;
}
}
/* Iterator of heading information.
* Assoc array with title, anchor, level. */
function heading_info($code) {
$anchors = [];
$lines = explode("\n", $code);
foreach ($lines as $line) {
if (!preg_match('/^(#+)\s*(.+)/', $line, $matches)) {
continue;
}
list(, $hashes, $title) = $matches;
$anchor = strtr(strtolower($title), ' ', '-');
$anchor = preg_replace('/[^\w-]/', '', $anchor);
if (isset($anchors[$anchor])) {
$anchors[$anchor]++;
$anchor .= '-' . $anchors[$anchor];
} else {
$anchors[$anchor] = 0;
}
yield [
'title' => $title,
'anchor' => $anchor,
'level' => strlen($hashes) - 1
];
}
}