Ein Dokument gliedert sich vor allem durch Kapitel der ersten Ebene und Überschriften im Text. Der Editor bietet h3 bis h6; h1 und h2 aus eingefügtem Text werden h3. Alle Überschriften des Kapiteltexts stehen automatisch im Inhaltsverzeichnis der Seite und des PDF (eingerückt, ohne Nummer), mit Sprungmarken, im PDF mit Seitenzahl, Verweis und verschachtelten Lesezeichen. Die PDF-Gliederung kennt jetzt bis zu sechs Ebenen und zählt alle Nachkommen. Unter einem Unterkapitel rücken die Überschriften eine Stufe tiefer (höchstens h6). Der bestehende Test erwartete h4 für eine Zwischenüberschrift im Kapitel; das gilt bewusst nicht mehr (h3 bleibt h3 und trägt eine Sprungmarke). Keine Migration, der Änderungsnachweis bleibt unverändert. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
138 lines
5.3 KiB
PHP
138 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Catalog;
|
|
|
|
use App\Domain\Entitlement\DecideAccess;
|
|
use App\Http\LibraryCatalog;
|
|
use App\Models\User;
|
|
|
|
/**
|
|
* Das Dokument, wie ein Stand es festschreibt: die im Stand gebundene Dokumentfassung mit ihren Kapiteln und je Kapitel die
|
|
* Entscheidung der zentralen Rechteprüfung (`DecideAccess`). Ansicht und PDF lesen dieses Ergebnis; beide zeigen dieselbe
|
|
* Kapitelreihenfolge und dieselben gebundenen Textfassungen. Eine spätere Bearbeitung des Dokuments ändert einen älteren Stand
|
|
* nicht: Der Stand verweist auf die Fassung, die er festgeschrieben hat.
|
|
*/
|
|
class DocumentGuide
|
|
{
|
|
public function __construct(
|
|
private readonly DecideAccess $access,
|
|
private readonly DocumentReader $reader,
|
|
) {}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function build(User $user, LicenseArticle $article, ArticleRelease $release, CompositionVersion $document): array
|
|
{
|
|
$document->loadMissing('composition');
|
|
$library = app(LibraryCatalog::class);
|
|
$embedded = app(EmbeddedImages::class);
|
|
$chapters = [];
|
|
|
|
foreach ($this->reader->fromVersion($document) as $chapter) {
|
|
$version = $chapter['version'];
|
|
$view = ($this->access)($user, $article, $release, $version, 'ansehen');
|
|
$download = ($this->access)($user, $article, $release, $version, 'laden');
|
|
|
|
$entry = [
|
|
'number' => $chapter['number'],
|
|
'level' => $chapter['level'],
|
|
'anchor' => $chapter['anchor'],
|
|
'title' => $chapter['title'],
|
|
'version' => $version,
|
|
'allowed' => $view->allowed,
|
|
'grant' => $view->grant,
|
|
'may_download' => $download->allowed,
|
|
'download_grant' => $download->grant,
|
|
'state' => $view->allowed ? 'ok' : ($version->status === ContentVersionStatus::Gesperrt ? 'gesperrt' : 'nicht_lizenziert'),
|
|
'html' => null,
|
|
'blocks' => $view->allowed ? FormattedText::blocks($version->body) : null,
|
|
];
|
|
$entry['html'] = $view->allowed ? self::anchoredHtml($entry, FormattedText::html($version->body, $embedded->forView($user, $article, $release, $version))) : null;
|
|
$chapters[] = self::withHeadings($entry);
|
|
}
|
|
|
|
$text = $library->text($document->composition);
|
|
|
|
return [
|
|
'document' => $document,
|
|
'title' => $text['title'],
|
|
'summary_html' => $text['summary_html'],
|
|
'pdf_template' => $document->composition->pdfTemplate,
|
|
'meta' => DocumentMeta::normalize($document->document_meta),
|
|
'chapters' => $chapters,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Um wie viele Stufen die Zwischenüberschriften eines Kapitels nach unten rücken: Im Kapitel (h2) bleiben h3 bis h6 wie
|
|
* geschrieben, unter einem Unterkapitel (h3) rücken sie eine Stufe tiefer (höchstens h6). So steht nie eine Überschrift
|
|
* des Texts auf der Stufe des Kapitels oder Unterkapitels, zu dem sie gehört.
|
|
*/
|
|
public static function headingShift(int $chapterLevel): int
|
|
{
|
|
return $chapterLevel >= 2 ? 1 : 0;
|
|
}
|
|
|
|
/**
|
|
* Das HTML eines Kapitels mit Sprungmarken an den Zwischenüberschriften und der Stufe, die zum Kapitel passt.
|
|
*
|
|
* @param array<string, mixed> $chapter
|
|
*/
|
|
public static function anchoredHtml(array $chapter, string $html): string
|
|
{
|
|
return FormattedText::anchorHeadings($html, (string) $chapter['anchor'], self::headingShift((int) $chapter['level']))['html'];
|
|
}
|
|
|
|
/**
|
|
* Ergänzt ein Kapitel um seine Zwischenüberschriften (`headings`: Stufe, Text, Sprungmarke) für das Inhaltsverzeichnis und
|
|
* legt Stufe und Sprungmarke in die Überschriftenblöcke des PDF. Ansicht und PDF zählen gleich; sie lesen beide diese Liste.
|
|
*
|
|
* @param array<string, mixed> $chapter
|
|
* @return array<string, mixed>
|
|
*/
|
|
public static function withHeadings(array $chapter): array
|
|
{
|
|
$shift = self::headingShift((int) $chapter['level']);
|
|
$headings = [];
|
|
$blocks = $chapter['blocks'] ?? null;
|
|
|
|
foreach (is_array($blocks) ? $blocks : [] as $index => $block) {
|
|
if ($block['type'] !== 'heading' || trim((string) ($block['text'] ?? '')) === '') {
|
|
continue;
|
|
}
|
|
|
|
$level = min(6, (int) $block['level'] + $shift);
|
|
$anchor = $chapter['anchor'].'-u'.(count($headings) + 1);
|
|
$blocks[$index]['level'] = $level;
|
|
$blocks[$index]['anchor'] = $anchor;
|
|
$headings[] = ['level' => $level, 'text' => (string) $block['text'], 'anchor' => $anchor];
|
|
}
|
|
|
|
$chapter['blocks'] = is_array($blocks) ? $blocks : null;
|
|
$chapter['headings'] = $headings;
|
|
|
|
return $chapter;
|
|
}
|
|
|
|
/**
|
|
* Darf das PDF entstehen? Jedes Kapitel muss für `laden` zulässig sein; sonst fehlte ein Teil des Handbuchs.
|
|
*
|
|
* @param array<string, mixed> $guide
|
|
*/
|
|
public function pdfAllowed(array $guide): bool
|
|
{
|
|
if ($guide['chapters'] === []) {
|
|
return false;
|
|
}
|
|
|
|
foreach ($guide['chapters'] as $chapter) {
|
|
if (! $chapter['allowed'] || ! $chapter['may_download']) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|