initial COmmit: Add KB Antora Importer plugin files
This commit is contained in:
132
kb-antora-importer/includes/AsciiDoc/AsciiDocRenderer.php
Normal file
132
kb-antora-importer/includes/AsciiDoc/AsciiDocRenderer.php
Normal file
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KbAntoraImporter\AsciiDoc;
|
||||
|
||||
final class AsciiDocRenderer
|
||||
{
|
||||
private ShortcodeTransformer $transformer;
|
||||
|
||||
public function __construct(?ShortcodeTransformer $transformer = null)
|
||||
{
|
||||
$this->transformer = $transformer ?: new ShortcodeTransformer();
|
||||
}
|
||||
|
||||
public function render(string $adoc, array $context = []): string
|
||||
{
|
||||
$lines = preg_split('/\R/', $adoc) ?: [];
|
||||
$html = '';
|
||||
$paragraph = [];
|
||||
$listOpen = false;
|
||||
$codeOpen = false;
|
||||
$code = [];
|
||||
|
||||
$flushParagraph = function () use (&$html, &$paragraph, $context): void {
|
||||
if (! $paragraph) {
|
||||
return;
|
||||
}
|
||||
|
||||
$text = implode(' ', array_map('trim', $paragraph));
|
||||
$html .= '<p>' . $this->transformer->transformInline($text, $context) . '</p>' . "\n";
|
||||
$paragraph = [];
|
||||
};
|
||||
|
||||
$closeList = static function () use (&$html, &$listOpen): void {
|
||||
if ($listOpen) {
|
||||
$html .= "</ul>\n";
|
||||
$listOpen = false;
|
||||
}
|
||||
};
|
||||
|
||||
foreach ($lines as $line) {
|
||||
$trimmed = trim($line);
|
||||
|
||||
if ('----' === $trimmed) {
|
||||
$flushParagraph();
|
||||
$closeList();
|
||||
if ($codeOpen) {
|
||||
$html .= '<pre><code>' . esc_html(implode("\n", $code)) . '</code></pre>' . "\n";
|
||||
$code = [];
|
||||
$codeOpen = false;
|
||||
} else {
|
||||
$codeOpen = true;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($codeOpen) {
|
||||
$code[] = $line;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ('' === $trimmed) {
|
||||
$flushParagraph();
|
||||
$closeList();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (preg_match('/^:[A-Za-z0-9_-]+:\s*/', $trimmed)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (preg_match('/^(={1,6})\s+(.+)$/', $trimmed, $matches)) {
|
||||
$flushParagraph();
|
||||
$closeList();
|
||||
$level = min(6, strlen($matches[1]));
|
||||
$html .= sprintf('<h%d>%s</h%d>', $level, esc_html($matches[2]), $level) . "\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
if (preg_match('/^\*\s+(.+)$/', $trimmed, $matches)) {
|
||||
$flushParagraph();
|
||||
if (! $listOpen) {
|
||||
$html .= "<ul>\n";
|
||||
$listOpen = true;
|
||||
}
|
||||
$html .= '<li>' . $this->transformer->transformInline($matches[1], $context) . '</li>' . "\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
if (preg_match('/^(NOTE|TIP|IMPORTANT|WARNING|CAUTION):\s+(.+)$/', $trimmed, $matches)) {
|
||||
$flushParagraph();
|
||||
$closeList();
|
||||
$class = strtolower($matches[1]);
|
||||
$html .= '<aside class="kb-admonition kb-admonition-' . esc_attr($class) . '"><strong>' . esc_html($matches[1]) . '</strong><p>' . $this->transformer->transformInline($matches[2], $context) . '</p></aside>' . "\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
if (preg_match('/^image::([^\[]+)\[([^\]]*)\]/', $trimmed, $matches)) {
|
||||
$flushParagraph();
|
||||
$closeList();
|
||||
$imageName = trim($matches[1]);
|
||||
$alt = trim($matches[2]) ?: basename($imageName);
|
||||
$url = (string) ($context['images'][$imageName] ?? $context['images'][basename($imageName)] ?? '');
|
||||
|
||||
if ($url) {
|
||||
$image = sprintf('<img src="%s" alt="%s">', esc_url($url), esc_attr($alt));
|
||||
if (! empty($context['lightbox'])) {
|
||||
$image = sprintf('<a href="%s" class="kb-lightbox">%s</a>', esc_url($url), $image);
|
||||
}
|
||||
$html .= '<figure class="kb-image">' . $image . '</figure>' . "\n";
|
||||
} else {
|
||||
$html .= '<figure class="kb-image kb-image-missing"><span>' . esc_html($alt) . '</span></figure>' . "\n";
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (preg_match('/^\|===/', $trimmed)) {
|
||||
$flushParagraph();
|
||||
$closeList();
|
||||
$html .= "<hr>\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
$paragraph[] = $line;
|
||||
}
|
||||
|
||||
$flushParagraph();
|
||||
$closeList();
|
||||
|
||||
return wp_kses_post($html);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user