|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Appends the ids that Sphinx gave each heading, so that links to the old anchors still land. |
| 7 | + */ |
| 8 | +function after_filter_anchor_aliases(string $data, string $folder, string $path): ?string |
| 9 | +{ |
| 10 | + $queues = []; |
| 11 | + |
| 12 | + $page = substr($path, strlen(dirname(__DIR__, 2) . '/docs/'), -strlen('.md')); |
| 13 | + |
| 14 | + foreach (alias_entries($page) as $entry) { |
| 15 | + $queues[$entry['slug']][] = $entry['ids']; |
| 16 | + } |
| 17 | + |
| 18 | + if ($queues === []) { |
| 19 | + return $data; |
| 20 | + } |
| 21 | + |
| 22 | + $fence = null; |
| 23 | + $lines = explode("\n", $data); |
| 24 | + |
| 25 | + foreach ($lines as $i => $line) { |
| 26 | + if (preg_match('/^\s*(```+|~~~+)/', $line, $matches) === 1) { |
| 27 | + $fence = $fence === null ? $matches[1][0] : ($matches[1][0] === $fence ? null : $fence); |
| 28 | + |
| 29 | + continue; |
| 30 | + } |
| 31 | + |
| 32 | + if ($fence !== null || preg_match('/^#{1,6}\s+(.+?)\s*$/', $line, $matches) !== 1) { |
| 33 | + continue; |
| 34 | + } |
| 35 | + |
| 36 | + $slug = heading_slug(html_entity_decode(preg_replace('/\[([^\]]*)\]\([^)]*\)/', '$1', $matches[1]), ENT_QUOTES | ENT_HTML5)); |
| 37 | + |
| 38 | + foreach (isset($queues[$slug]) ? array_shift($queues[$slug]) : [] as $id) { |
| 39 | + $lines[$i] .= sprintf('<a id="%s"></a>', $id); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + return implode("\n", $lines); |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * The headings of the source page `$page` in order, each with its MkDocs slug and the Sphinx ids that differ from it. |
| 48 | + * |
| 49 | + * @return list<array{slug: string, ids: list<string>}> |
| 50 | + */ |
| 51 | +function alias_entries(string $page): array |
| 52 | +{ |
| 53 | + $file = source_dir() . $page . '.rst'; |
| 54 | + |
| 55 | + if (!is_file($file)) { |
| 56 | + return []; |
| 57 | + } |
| 58 | + |
| 59 | + $lines = file($file, FILE_IGNORE_NEW_LINES); |
| 60 | + $entries = []; |
| 61 | + $labels = []; |
| 62 | + $namespace = ''; |
| 63 | + $class = ''; |
| 64 | + |
| 65 | + foreach ($lines as $i => $line) { |
| 66 | + if (preg_match('/^\s*\.\. _([^:`]+):\s*$/', $line, $matches) === 1) { |
| 67 | + $labels[] = docutils_id($matches[1]); |
| 68 | + } elseif (is_rst_heading($lines, $i)) { |
| 69 | + $text = rst_heading_text($line); |
| 70 | + $entries[] = alias_entry(heading_slug($text), [...$labels, docutils_id($text)]); |
| 71 | + $labels = []; |
| 72 | + } elseif (preg_match('/^\s*\.\.\s+php:(namespace|class|interface|trait|method|staticmethod|function|attr|const):: *(.+?)\s*$/', $line, $matches) === 1) { |
| 73 | + [, $kind, $signature] = $matches; |
| 74 | + $name = preg_replace('/[\s(].*$/', '', $signature); |
| 75 | + $prefix = $namespace === '' ? '' : $namespace . '\\'; |
| 76 | + |
| 77 | + if ($kind === 'namespace') { |
| 78 | + $namespace = $signature; |
| 79 | + $class = ''; |
| 80 | + $entries[] = alias_entry(heading_slug($signature), ['namespace-' . $signature]); |
| 81 | + } elseif (in_array($kind, ['class', 'interface', 'trait'], true)) { |
| 82 | + $class = $prefix . $name; |
| 83 | + $entries[] = alias_entry(heading_slug($name), [$class]); |
| 84 | + } else { |
| 85 | + $owner = $class !== '' && !in_array($kind, ['function', 'const'], true) ? $class . '::' : $prefix; |
| 86 | + $entries[] = alias_entry(heading_slug($kind === 'const' ? $name : $signature), [$owner . $name]); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return $entries; |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * The entry for a heading with the MkDocs slug `$slug`, keeping only the ids of `$ids` that differ from it. |
| 96 | + * |
| 97 | + * @param list<string> $ids |
| 98 | + * |
| 99 | + * @return array{slug: string, ids: list<string>} |
| 100 | + */ |
| 101 | +function alias_entry(string $slug, array $ids): array |
| 102 | +{ |
| 103 | + return ['slug' => $slug, 'ids' => array_values(array_unique(array_diff($ids, [$slug, ''])))]; |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * The MkDocs slug of the heading text `$text`, which Python-Markdown reads without its HTML tags. |
| 108 | + */ |
| 109 | +function heading_slug(string $text): string |
| 110 | +{ |
| 111 | + return markdown_slug(preg_replace('/<[^>]*>/', '', $text)); |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * The id that docutils makes from the section title or label `$text`. |
| 116 | + */ |
| 117 | +function docutils_id(string $text): string |
| 118 | +{ |
| 119 | + return preg_replace('/^[-0-9]+|-+$/', '', preg_replace('/[^a-z0-9]+/', '-', strtolower($text))); |
| 120 | +} |
0 commit comments