CI Feature / test (push) Successful in 3m32s
Neuer kind 'exercise', Varianten je Fassung, Verwendung (erforderlich/ergänzend) und Variantenauswahl am einbindenden Mitglied; der Stand schreibt nur gewählte Varianten fest. Änderungsnachweis über structure_hash. Formatierte Anleitung im Mitgliederbereich und PDF (Fußzeile, Metadaten, Ausgabenachweis, keine gespeicherten Kopien); Sperre trifft nur das Medium. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
86 lines
2.5 KiB
PHP
86 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Catalog;
|
|
|
|
/**
|
|
* Anleitungstext als Klartext mit wenigen Konventionen: Absätze an Leerzeilen, Listen mit „- “ oder „• “ und
|
|
* nummerierte Listen mit „1. “, Zwischenüberschriften mit „## “. Ansicht und PDF lesen dieselben Blöcke und zeigen damit
|
|
* dieselbe Fassung. Es gibt kein freies HTML.
|
|
*/
|
|
final class GuideText
|
|
{
|
|
/**
|
|
* @return list<array{type: 'paragraph'|'bullets'|'numbered'|'heading', text?: string, items?: list<string>}>
|
|
*/
|
|
public static function blocks(?string $body): array
|
|
{
|
|
$blocks = [];
|
|
$paragraph = [];
|
|
$list = null;
|
|
|
|
$flushParagraph = function () use (&$blocks, &$paragraph): void {
|
|
if ($paragraph !== []) {
|
|
$blocks[] = ['type' => 'paragraph', 'text' => implode(' ', $paragraph)];
|
|
$paragraph = [];
|
|
}
|
|
};
|
|
$flushList = function () use (&$blocks, &$list): void {
|
|
if ($list !== null) {
|
|
$blocks[] = ['type' => $list['type'], 'items' => $list['items']];
|
|
$list = null;
|
|
}
|
|
};
|
|
|
|
foreach (preg_split('/\R/u', trim((string) $body)) ?: [] as $line) {
|
|
$line = rtrim($line);
|
|
|
|
if (trim($line) === '') {
|
|
$flushParagraph();
|
|
$flushList();
|
|
|
|
continue;
|
|
}
|
|
|
|
if (preg_match('/^##\s+(.+)$/u', $line, $match) === 1) {
|
|
$flushParagraph();
|
|
$flushList();
|
|
$blocks[] = ['type' => 'heading', 'text' => trim($match[1])];
|
|
|
|
continue;
|
|
}
|
|
|
|
if (preg_match('/^\s*(?:[-•*])\s+(.+)$/u', $line, $match) === 1) {
|
|
$flushParagraph();
|
|
if ($list === null || $list['type'] !== 'bullets') {
|
|
$flushList();
|
|
$list = ['type' => 'bullets', 'items' => []];
|
|
}
|
|
|
|
$list['items'][] = trim($match[1]);
|
|
|
|
continue;
|
|
}
|
|
|
|
if (preg_match('/^\s*\d+[.)]\s+(.+)$/u', $line, $match) === 1) {
|
|
$flushParagraph();
|
|
if ($list === null || $list['type'] !== 'numbered') {
|
|
$flushList();
|
|
$list = ['type' => 'numbered', 'items' => []];
|
|
}
|
|
|
|
$list['items'][] = trim($match[1]);
|
|
|
|
continue;
|
|
}
|
|
|
|
$flushList();
|
|
$paragraph[] = trim($line);
|
|
}
|
|
|
|
$flushParagraph();
|
|
$flushList();
|
|
|
|
return $blocks;
|
|
}
|
|
}
|