initial COmmit: Add KB Antora Importer plugin files
This commit is contained in:
67
kb-antora-importer/includes/Antora/AntoraNavParser.php
Normal file
67
kb-antora-importer/includes/Antora/AntoraNavParser.php
Normal 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;
|
||||
}
|
||||
}
|
||||
22
kb-antora-importer/includes/Antora/AntoraParser.php
Normal file
22
kb-antora-importer/includes/Antora/AntoraParser.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KbAntoraImporter\Antora;
|
||||
|
||||
final class AntoraParser
|
||||
{
|
||||
public function pageSlugFromPath(string $path): string
|
||||
{
|
||||
$name = preg_replace('/\.adoc$/', '', basename($path)) ?: basename($path);
|
||||
return 'index' === $name ? '' : sanitize_title($name);
|
||||
}
|
||||
|
||||
public function moduleFromPath(string $path): string
|
||||
{
|
||||
if (preg_match('#^modules/([^/]+)/#', $path, $matches)) {
|
||||
return $matches[1];
|
||||
}
|
||||
|
||||
return 'ROOT';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KbAntoraImporter\Antora;
|
||||
|
||||
final class AntoraResourceResolver
|
||||
{
|
||||
public function imagePath(string $imageName, string $module = 'ROOT'): string
|
||||
{
|
||||
return 'modules/' . trim($module, '/') . '/images/' . ltrim($imageName, '/');
|
||||
}
|
||||
|
||||
public function partialPath(string $partialName, string $module = 'ROOT'): string
|
||||
{
|
||||
return 'modules/' . trim($module, '/') . '/partials/' . ltrim($partialName, '/');
|
||||
}
|
||||
}
|
||||
44
kb-antora-importer/includes/Antora/AntoraYamlReader.php
Normal file
44
kb-antora-importer/includes/Antora/AntoraYamlReader.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KbAntoraImporter\Antora;
|
||||
|
||||
final class AntoraYamlReader
|
||||
{
|
||||
public function parse(string $yaml): array
|
||||
{
|
||||
$data = [
|
||||
'name' => '',
|
||||
'title' => '',
|
||||
'version' => '',
|
||||
'nav' => [],
|
||||
];
|
||||
|
||||
$inNav = false;
|
||||
foreach (preg_split('/\R/', $yaml) ?: [] as $line) {
|
||||
$trimmed = trim($line);
|
||||
|
||||
if ('' === $trimmed || str_starts_with($trimmed, '#')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (preg_match('/^([a-zA-Z0-9_-]+):\s*(.*)$/', $trimmed, $matches)) {
|
||||
$key = $matches[1];
|
||||
$value = trim($matches[2], " \"'");
|
||||
$inNav = 'nav' === $key;
|
||||
|
||||
if (array_key_exists($key, $data) && 'nav' !== $key) {
|
||||
$data[$key] = $value;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($inNav && preg_match('/^-\s*(.+)$/', $trimmed, $matches)) {
|
||||
$data['nav'][] = trim($matches[1], " \"'");
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user