initial COmmit: Add KB Antora Importer plugin files

This commit is contained in:
Sven Steinert
2026-05-12 14:37:09 +02:00
parent cf253c1367
commit 6abf6f9c3d
39 changed files with 2945 additions and 0 deletions

View File

@@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
namespace KbAntoraImporter\Antora;
final class AntoraNavParser
{
public function parse(string $nav): array
{
$root = [];
$stack = [];
foreach (preg_split('/\R/', $nav) ?: [] as $line) {
if (! preg_match('/^(\*+)\s+(.+)$/', trim($line), $matches)) {
continue;
}
$level = strlen($matches[1]);
$raw = trim($matches[2]);
$item = [
'title' => $raw,
'target' => '',
'children' => [],
];
if (preg_match('/xref:([^\[]+)\[([^\]]*)\]/', $raw, $xref)) {
$item['target'] = trim($xref[1]);
$item['title'] = trim($xref[2]) ?: basename($item['target']);
}
while (count($stack) >= $level) {
array_pop($stack);
}
if (empty($stack)) {
$root[] = $item;
$stack[$level - 1] = &$root[array_key_last($root)];
} else {
$parent = &$stack[array_key_last($stack)];
$parent['children'][] = $item;
$stack[$level - 1] = &$parent['children'][array_key_last($parent['children'])];
}
unset($parent);
}
return $root;
}
public function flatten(array $tree): array
{
$items = [];
$walk = static function (array $nodes, int $level = 1) use (&$walk, &$items): void {
foreach ($nodes as $node) {
$items[] = [
'title' => (string) ($node['title'] ?? ''),
'target' => (string) ($node['target'] ?? ''),
'level' => $level,
];
$walk((array) ($node['children'] ?? []), $level + 1);
}
};
$walk($tree);
return $items;
}
}