*/ 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 $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 $chapter * @return array */ 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 $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; } }