initial COmmit: Add KB Antora Importer plugin files
This commit is contained in:
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