Speichere Name, Sprungmarke und Ausblenden je Abschnitt und gib sie auf der Website aus
Name nur zur Orientierung, Sprungmarke als Element-Kennung (je Seite eindeutig, reservierte Kennungen gesperrt), ausgeblendete Abschnitte erscheinen nie öffentlich und zählen weder für die Hauptüberschrift noch für Trenner. SafeUrl nimmt #marke an. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
00b7ce9b3f
commit
3a37e8a7a1
5 files changed
+391
-14
No files matched your search
@@ -18,6 +18,13 @@
|
||||
* Version 2 (Abstände): oben, unten und Widget-Abstand sind drei unabhängige Werte. Hüllen der Version 1 werden beim
|
||||
* Lesen übersetzt (SectionStyle::migrateSpacing), ohne die Darstellung zu ändern; gespeicherte Revisionen bleiben.
|
||||
*
|
||||
* Optionale Felder des Abschnitts (abwärtskompatibel, fehlen in älteren Revisionen):
|
||||
* - name: interner Name zur Orientierung im Seitenaufbau (nie öffentlich), höchstens 80 Zeichen;
|
||||
* - anchor: Sprungmarke, wird als Element-Kennung des Abschnitts ausgegeben (Link „#marke“ oder „/pfad#marke“),
|
||||
* je Seite eindeutig, nur Kleinbuchstaben, Ziffern und Bindestriche;
|
||||
* - hidden: true blendet den Abschnitt auf der Website aus; er bleibt im Entwurf und in den Revisionen erhalten.
|
||||
* Ältere Programmstände kennen „hidden“ nicht und würden einen ausgeblendeten Abschnitt zeigen (siehe Release-Vorbereitung).
|
||||
*
|
||||
* Jeder Slot enthält höchstens ein Widget aus dem zentralen Widget-Register. Abschnitte sind nicht verschachtelbar.
|
||||
* Revisionen ohne Abschnitte (Bestand vor dieser Fassung) werden beim Lesen als einspaltige Abschnitte gelesen.
|
||||
*/
|
||||
@@ -29,6 +36,20 @@ class PageSections
|
||||
|
||||
public const MAX_WIDGETS = 60;
|
||||
|
||||
public const MAX_NAME = 80;
|
||||
|
||||
public const MAX_ANCHOR = 60;
|
||||
|
||||
/**
|
||||
* Kennungen, die im Seitengerüst schon vergeben sind (Hauptbereich, Navigation, Fuß) oder als Vorgabe dienen.
|
||||
* Eine Sprungmarke darf sie nicht wiederholen.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
public const RESERVED_ANCHORS = ['inhalt', 'main', 'top', 'site-nav', 'fuss-themen', 'mb-account-menu'];
|
||||
|
||||
public const ANCHOR_PATTERN = '/^[a-z0-9]+(?:-[a-z0-9]+)*$/';
|
||||
|
||||
public function __construct(private readonly WidgetRegistry $widgets) {}
|
||||
|
||||
/**
|
||||
@@ -36,7 +57,7 @@ public function __construct(private readonly WidgetRegistry $widgets) {}
|
||||
*
|
||||
* @param array<int|string, mixed>|null $sections gespeicherte Abschnitte (Spalte sections)
|
||||
* @param array<int|string, mixed>|null $blocks flache Bausteinliste (Spalte blocks), nur ohne Abschnitte verwendet
|
||||
* @return list<array{id: ?string, v: int, layout: string, style: array<string, mixed>, slots: list<array<string, mixed>|null>}>
|
||||
* @return list<array{id: ?string, v: int, name: ?string, anchor: ?string, hidden: bool, layout: string, style: array<string, mixed>, slots: list<array<string, mixed>|null>}>
|
||||
*/
|
||||
public function read(?array $sections, ?array $blocks = null): array
|
||||
{
|
||||
@@ -59,7 +80,7 @@ public function read(?array $sections, ?array $blocks = null): array
|
||||
* „sections.N.layout“, „sections.N.style.image“, „sections.N.slots“, „sections.N.slots.M.data.feld“.
|
||||
*
|
||||
* @param array<int|string, mixed> $content
|
||||
* @return list<array{id: string, v: int, layout: string, style: array<string, mixed>, slots: list<array<string, mixed>|null>}>
|
||||
* @return list<array{id: string, v: int, name: ?string, anchor: ?string, hidden: bool, layout: string, style: array<string, mixed>, slots: list<array<string, mixed>|null>}>
|
||||
*/
|
||||
public function normalize(array $content): array
|
||||
{
|
||||
@@ -75,6 +96,7 @@ public function normalize(array $content): array
|
||||
$sections = [];
|
||||
$seenSectionIds = [];
|
||||
$seenWidgetIds = [];
|
||||
$seenAnchors = [];
|
||||
$widgetCount = 0;
|
||||
|
||||
// Eingaben mit Abständen der Version 1 (zum Beispiel ältere Aufrufer) werden wie beim Lesen übersetzt.
|
||||
@@ -96,7 +118,7 @@ public function normalize(array $content): array
|
||||
$item['style'] = $migrated[$index]['style'] ?? ($item['style'] ?? []);
|
||||
}
|
||||
|
||||
$sections[] = $this->normalizeSection($item, $index, $errors, $seenSectionIds, $seenWidgetIds, $widgetCount);
|
||||
$sections[] = $this->normalizeSection($item, $index, $errors, $seenSectionIds, $seenWidgetIds, $widgetCount, $seenAnchors);
|
||||
}
|
||||
|
||||
if ($widgetCount > self::MAX_WIDGETS) {
|
||||
@@ -164,12 +186,14 @@ public function mediaPaths(array $sections): array
|
||||
*/
|
||||
public function pageTitle(array $sections): ?string
|
||||
{
|
||||
return $this->widgets->pageTitle($this->flatten($sections));
|
||||
// Ein ausgeblendeter Abschnitt liefert keinen Seitentitel; seine Medien bleiben dagegen erfasst (mediaPaths),
|
||||
// damit ihre Bilder nicht als ungenutzt gelten.
|
||||
return $this->widgets->pageTitle($this->flatten(array_values(array_filter($sections, fn (array $section): bool => ! ($section['hidden'] ?? false)))));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int|string, mixed> $blocks
|
||||
* @return list<array{id: ?string, v: int, layout: string, style: array<string, mixed>, slots: list<array<string, mixed>|null>}>
|
||||
* @return list<array{id: ?string, v: int, name: ?string, anchor: ?string, hidden: bool, layout: string, style: array<string, mixed>, slots: list<array<string, mixed>|null>}>
|
||||
*/
|
||||
private function wrapWidgets(array $blocks): array
|
||||
{
|
||||
@@ -179,6 +203,9 @@ private function wrapWidgets(array $blocks): array
|
||||
$sections[] = [
|
||||
'id' => null,
|
||||
'v' => self::VERSION,
|
||||
'name' => null,
|
||||
'anchor' => null,
|
||||
'hidden' => false,
|
||||
'layout' => SectionLayouts::DEFAULT,
|
||||
// Bestand vor dem Raster stand in einem durchgehenden Blatt: kompakte Abstände, zwischen den Widgets
|
||||
// nur einmal (oben kein Abstand ab dem zweiten), wie bisher.
|
||||
@@ -192,7 +219,7 @@ private function wrapWidgets(array $blocks): array
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $section
|
||||
* @return array{id: ?string, v: int, layout: string, style: array<string, mixed>, slots: list<array<string, mixed>|null>}
|
||||
* @return array{id: ?string, v: int, name: ?string, anchor: ?string, hidden: bool, layout: string, style: array<string, mixed>, slots: list<array<string, mixed>|null>}
|
||||
*/
|
||||
private function upcastSection(array $section): array
|
||||
{
|
||||
@@ -210,6 +237,9 @@ private function upcastSection(array $section): array
|
||||
return [
|
||||
'id' => is_string($section['id'] ?? null) ? $section['id'] : null,
|
||||
'v' => self::VERSION,
|
||||
'name' => self::cleanName($section['name'] ?? null),
|
||||
'anchor' => self::cleanAnchor($section['anchor'] ?? null),
|
||||
'hidden' => self::isHidden($section['hidden'] ?? false),
|
||||
'layout' => $layout,
|
||||
'style' => SectionStyle::fromStored($section['style'] ?? []),
|
||||
'slots' => $slots,
|
||||
@@ -221,9 +251,9 @@ private function upcastSection(array $section): array
|
||||
* @param array<string, list<string>> $errors
|
||||
* @param array<string, true> $seenSectionIds
|
||||
* @param array<string, true> $seenWidgetIds
|
||||
* @return array{id: string, v: int, layout: string, style: array<string, mixed>, slots: list<array<string, mixed>|null>}
|
||||
* @return array{id: string, v: int, name: ?string, anchor: ?string, hidden: bool, layout: string, style: array<string, mixed>, slots: list<array<string, mixed>|null>}
|
||||
*/
|
||||
private function normalizeSection(array $section, int $index, array &$errors, array &$seenSectionIds, array &$seenWidgetIds, int &$widgetCount): array
|
||||
private function normalizeSection(array $section, int $index, array &$errors, array &$seenSectionIds, array &$seenWidgetIds, int &$widgetCount, array &$seenAnchors): array
|
||||
{
|
||||
$prefix = "sections.$index";
|
||||
|
||||
@@ -294,15 +324,90 @@ private function normalizeSection(array $section, int $index, array &$errors, ar
|
||||
}
|
||||
$seenSectionIds[$id] = true;
|
||||
|
||||
[$name, $anchor] = $this->normalizeLabels($section, $prefix, $errors, $seenAnchors);
|
||||
|
||||
return [
|
||||
'id' => $id,
|
||||
'v' => self::VERSION,
|
||||
'name' => $name,
|
||||
'anchor' => $anchor,
|
||||
'hidden' => self::isHidden($section['hidden'] ?? false),
|
||||
'layout' => $layout,
|
||||
'style' => $style,
|
||||
'slots' => $slots,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Name und Sprungmarke eines Abschnitts. Fehler: „sections.N.name“ und „sections.N.anchor“.
|
||||
*
|
||||
* @param array<string, mixed> $section
|
||||
* @param array<string, list<string>> $errors
|
||||
* @param array<string, true> $seenAnchors
|
||||
* @return array{0: ?string, 1: ?string}
|
||||
*/
|
||||
private function normalizeLabels(array $section, string $prefix, array &$errors, array &$seenAnchors): array
|
||||
{
|
||||
$name = null;
|
||||
$rawName = $section['name'] ?? null;
|
||||
|
||||
if (is_string($rawName) && trim($rawName) !== '') {
|
||||
$name = self::cleanName($rawName);
|
||||
|
||||
if ($name === null) {
|
||||
$errors["$prefix.name"] = ['Der Name darf höchstens '.self::MAX_NAME.' Zeichen lang sein.'];
|
||||
}
|
||||
}
|
||||
|
||||
$anchor = null;
|
||||
$rawAnchor = $section['anchor'] ?? null;
|
||||
|
||||
if (is_string($rawAnchor) && trim($rawAnchor) !== '') {
|
||||
$candidate = mb_strtolower(trim($rawAnchor));
|
||||
|
||||
if (strlen($candidate) > self::MAX_ANCHOR || preg_match(self::ANCHOR_PATTERN, $candidate) !== 1) {
|
||||
$errors["$prefix.anchor"] = ['Eine Sprungmarke besteht aus Kleinbuchstaben, Ziffern und einzelnen Bindestrichen (höchstens '.self::MAX_ANCHOR.' Zeichen), zum Beispiel „anmeldung“.'];
|
||||
} elseif (in_array($candidate, self::RESERVED_ANCHORS, true) || str_starts_with($candidate, 'bereich-')) {
|
||||
$errors["$prefix.anchor"] = ['Diese Sprungmarke ist im Seitengerüst schon vergeben. Bitte eine andere wählen.'];
|
||||
} elseif (isset($seenAnchors[$candidate])) {
|
||||
$errors["$prefix.anchor"] = ['Diese Sprungmarke gibt es auf der Seite schon. Jede Sprungmarke darf nur einmal vorkommen.'];
|
||||
} else {
|
||||
$seenAnchors[$candidate] = true;
|
||||
$anchor = $candidate;
|
||||
}
|
||||
} elseif ($rawAnchor !== null && ! is_string($rawAnchor)) {
|
||||
$errors["$prefix.anchor"] = ['Die Sprungmarke ist ungültig.'];
|
||||
}
|
||||
|
||||
return [$name, $anchor];
|
||||
}
|
||||
|
||||
/** Interner Name: Leerraum vereinheitlicht, ohne Steuerzeichen; leer oder über 80 Zeichen ergibt null. */
|
||||
public static function cleanName(mixed $value): ?string
|
||||
{
|
||||
if (! is_string($value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$name = trim((string) preg_replace('/\s+/u', ' ', (string) preg_replace('/[\x00-\x1F\x7F]/u', ' ', $value)));
|
||||
|
||||
return $name === '' || mb_strlen($name) > self::MAX_NAME ? null : $name;
|
||||
}
|
||||
|
||||
/** Lesepfad: eine gespeicherte Sprungmarke nur, wenn sie der Form entspricht. */
|
||||
public static function cleanAnchor(mixed $value): ?string
|
||||
{
|
||||
return is_string($value) && strlen($value) <= self::MAX_ANCHOR && preg_match(self::ANCHOR_PATTERN, $value) === 1
|
||||
&& ! in_array($value, self::RESERVED_ANCHORS, true) && ! str_starts_with($value, 'bereich-')
|
||||
? $value
|
||||
: null;
|
||||
}
|
||||
|
||||
public static function isHidden(mixed $value): bool
|
||||
{
|
||||
return $value === true || $value === 1 || $value === '1' || $value === 'true';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $style
|
||||
* @param array<string, list<string>> $errors
|
||||
|
||||
@@ -17,6 +17,9 @@
|
||||
* - Ein Abschnitt ohne darstellbares Widget entfällt samt Hintergrund und Trenner.
|
||||
* - Das erste dargestellte Widget der Seite ist der Seiteneinstieg (Überschrift erster Ordnung).
|
||||
* - Trenner stehen zwischen Abschnitten, nie nach dem letzten.
|
||||
* - Ein ausgeblendeter Abschnitt („hidden“) erscheint nie auf der Website und zählt weder für das erste Widget noch für
|
||||
* Trenner. Nur die Vorschau zeigt an seiner Stelle einen schmalen Hinweis (ohne Inhalt).
|
||||
* - Eine Sprungmarke („anchor“) ist die Element-Kennung des Abschnitts; ohne Sprungmarke gilt „bereich-<Kennung>“.
|
||||
*/
|
||||
class SectionRenderer
|
||||
{
|
||||
@@ -31,6 +34,14 @@ public function renderAll(array $sections, RenderContext $context): HtmlString
|
||||
$first = true;
|
||||
|
||||
foreach ($sections as $section) {
|
||||
if ($section['hidden'] ?? false) {
|
||||
if ($context->preview) {
|
||||
$rendered[] = ['note' => $this->hiddenNote($section)];
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$cells = [];
|
||||
$shown = false;
|
||||
$units = SectionLayouts::units($section['layout']);
|
||||
@@ -54,13 +65,28 @@ public function renderAll(array $sections, RenderContext $context): HtmlString
|
||||
}
|
||||
|
||||
$html = '';
|
||||
$last = count($rendered) - 1;
|
||||
$last = -1;
|
||||
|
||||
foreach ($rendered as $position => $item) {
|
||||
if (isset($item['section'])) {
|
||||
$last = $position;
|
||||
}
|
||||
}
|
||||
|
||||
$index = 0;
|
||||
|
||||
foreach ($rendered as $position => $item) {
|
||||
if (! isset($item['section'])) {
|
||||
$html .= $item['note'];
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($rendered as $index => $item) {
|
||||
$html .= $this->renderSection($item['section'], $item['cells'], $index, $context);
|
||||
$index++;
|
||||
|
||||
$divider = $item['section']['style']['divider'] ?? 'none';
|
||||
if ($index < $last && in_array($divider, ['line', 'accent'], true)) {
|
||||
if ($position < $last && in_array($divider, ['line', 'accent'], true)) {
|
||||
$html .= '<div class="section-divider section-divider-'.$divider.'" aria-hidden="true"><span></span></div>';
|
||||
}
|
||||
}
|
||||
@@ -68,6 +94,18 @@ public function renderAll(array $sections, RenderContext $context): HtmlString
|
||||
return new HtmlString($html);
|
||||
}
|
||||
|
||||
/**
|
||||
* Hinweis der Vorschau an der Stelle eines ausgeblendeten Abschnitts.
|
||||
*
|
||||
* @param array<string, mixed> $section
|
||||
*/
|
||||
private function hiddenNote(array $section): string
|
||||
{
|
||||
$name = is_string($section['name'] ?? null) && $section['name'] !== '' ? '„'.$section['name'].'“ ' : '';
|
||||
|
||||
return '<div class="section-hidden-note" role="note">Abschnitt '.e($name).'ist für die Website ausgeblendet und erscheint hier nur als Hinweis.</div>';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $section
|
||||
* @param list<array{units: int, html: string}> $cells alle Plätze des Rasters in ihrer Reihenfolge; html '' = leer
|
||||
@@ -106,7 +144,11 @@ private function renderSection(array $section, array $cells, int $index, RenderC
|
||||
$classes[] = $scope;
|
||||
}
|
||||
|
||||
$id = is_string($section['id'] ?? null) ? ' id="bereich-'.e($section['id']).'"' : '';
|
||||
// Die Sprungmarke ist die Kennung, die ein Link „#marke“ anspringt; ohne sie bleibt die bisherige Kennung.
|
||||
$anchor = PageSections::cleanAnchor($section['anchor'] ?? null);
|
||||
$id = $anchor !== null
|
||||
? ' id="'.e($anchor).'"'
|
||||
: (is_string($section['id'] ?? null) ? ' id="bereich-'.e($section['id']).'"' : '');
|
||||
$html = '<div class="'.e(implode(' ', $classes)).'"'.$id.'>';
|
||||
|
||||
if ($hasImage) {
|
||||
|
||||
Reference in new issue
Block a user