mirror of
https://github.com/php/php-langspec.git
synced 2026-03-24 07:12:08 +01:00
50 lines
1.2 KiB
PHP
50 lines
1.2 KiB
PHP
<?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
|
|
];
|
|
}
|
|
}
|