From be24f3bf64a9bd441b7ab96cf386dda4a5bfa4b1 Mon Sep 17 00:00:00 2001 From: Lars Date: Mon, 23 Nov 2026 23:45:54 +0100 Subject: [PATCH 01/12] =?UTF-8?q?=C3=9Cbernimm=20Texte=20aus=20Shinkan=20a?= =?UTF-8?q?ls=20bereinigtes=20HTML?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Der Abruf wertet text_format aus. Bei html gelten Kurzbeschreibung, Lernziel, Durchführung, Vorbereitung, Trainerhinweise und die Abweichung der Durchführung als HTML; Verweise auf Quellmedien entfallen. Vergleich und Dreiwegeabgleich arbeiten in kanonischer Form, die Vorschau zeigt Klartext. Ohne Angabe bleibt alles Klartext wie bisher. Co-Authored-By: Claude Sonnet 5.5 --- app/Domain/Catalog/ShinkanImport.php | 106 +++++++++++++----- .../uebungen-shinkan-feldzuordnung.md | 6 +- tests/Feature/ShinkanUebernahmeTest.php | 105 ++++++++++++++++- .../Fixtures/shinkan/katalog-detail-html.json | 38 +++++++ 4 files changed, 224 insertions(+), 31 deletions(-) create mode 100644 tests/Fixtures/shinkan/katalog-detail-html.json diff --git a/app/Domain/Catalog/ShinkanImport.php b/app/Domain/Catalog/ShinkanImport.php index d6569f2..9aa89ae 100644 --- a/app/Domain/Catalog/ShinkanImport.php +++ b/app/Domain/Catalog/ShinkanImport.php @@ -33,6 +33,9 @@ class ShinkanImport 'trainer_notes' => 'Trainerhinweise', ]; + /** Felder, die Shinkan bei `text_format: html` als HTML liefert (ADR-50). Ohne diese Angabe sind alle Texte Klartext. */ + private const HTML_FIELDS = ['summary', 'goal', 'execution', 'preparation', 'trainer_notes']; + /** Änderungshinweis der Textfassungen, die die Übernahme angelegt hat. Alles andere ist eine lokale Bearbeitung. */ private const IMPORT_NOTE = 'Übernahme aus Shinkan'; @@ -212,8 +215,8 @@ public function preview(ExerciseSource $source): array $sections = []; foreach (self::SECTIONS as $field => $label) { - if (trim((string) ($document[$field] ?? '')) !== '') { - $sections[] = ['label' => $label, 'text' => mb_strimwidth(trim((string) $document[$field]), 0, 280, '…')]; + if ($this->value($document, $field) !== '') { + $sections[] = ['label' => $label, 'text' => FormattedText::excerpt($this->value($document, $field), 280)]; } } @@ -223,16 +226,16 @@ public function preview(ExerciseSource $source): array 'hash' => $snapshot?->content_hash, 'state' => $state, 'title' => (string) ($document['title'] ?? $source->title), - 'summary' => (string) ($document['summary'] ?? ''), - 'goal' => (string) ($document['goal'] ?? ''), + 'summary' => FormattedText::plain($this->value($document, 'summary')), + 'goal' => FormattedText::plain($this->value($document, 'goal')), 'duration' => $this->duration($document), - 'equipment' => (string) ($document['equipment'] ?? ''), + 'equipment' => FormattedText::plain($this->value($document, 'equipment')), 'sections' => $sections, 'variants' => array_map(fn (array $variant): array => [ 'key' => (string) ($variant['id'] ?? ''), 'name' => (string) ($variant['name'] ?? ''), 'description' => (string) ($variant['description'] ?? ''), - 'changes' => trim((string) ($variant['execution_changes'] ?? '')) !== '', + 'changes' => $this->variantChanges($document, $variant) !== '', ], array_values(array_filter((array) ($document['variants'] ?? []), 'is_array'))), 'media' => array_map(fn (array $media): string => ($media['title'] ?? 'Medium').' ('.($media['media_type'] ?? '').')', array_values(array_filter((array) ($document['media'] ?? []), 'is_array'))), 'changes' => $previous === null ? [] : $this->changes($previous->payload, $document, $source), @@ -260,7 +263,7 @@ public function changes(array $before, array $after, ?ExerciseSource $source = n ] + self::SECTIONS; foreach ($labels as $field => $label) { - if (trim((string) ($before[$field] ?? '')) !== trim((string) ($after[$field] ?? ''))) { + if (! $this->sameValue($field, $before, $after)) { $lines[] = $label.' geändert'; } } @@ -282,7 +285,7 @@ public function changes(array $before, array $after, ?ExerciseSource $source = n } if (trim((string) ($variant['description'] ?? '')) !== trim((string) ($old[$key]['description'] ?? '')) - || trim((string) ($variant['execution_changes'] ?? '')) !== trim((string) ($old[$key]['execution_changes'] ?? ''))) { + || ! FormattedText::same($this->variantChanges($after, $variant), $this->variantChanges($before, (array) $old[$key]))) { $lines[] = 'Variante „'.($variant['name'] ?? '').'“ geändert'; } } @@ -298,7 +301,7 @@ public function changes(array $before, array $after, ?ExerciseSource $source = n $lines[] = 'Der Titel wurde lokal geändert und bleibt unverändert'; } - if (($before['summary'] ?? '') !== ($after['summary'] ?? '') && filled($source->composition->summary) && $source->composition->summary !== ($before['summary'] ?? '')) { + if (! $this->sameValue('summary', $before, $after) && filled($source->composition->summary) && ! FormattedText::same($source->composition->summary, $this->value($before, 'summary'))) { $lines[] = 'Die Kurzbeschreibung wurde lokal geändert und bleibt unverändert'; } } @@ -367,7 +370,11 @@ public function textDecisions(ExerciseSource $source, array $document, string $w $latest = $source->composition->versions()->orderByDesc('number')->first(); foreach (self::FIELDS as $field => [$column, $label]) { - if ($latest !== null && $this->classifyField($latest->{$column}, $previousSource[$field] ?? null, $document[$field] ?? null) === $wanted) { + $isText = in_array($field, ['goal', 'equipment'], true); + $old = $isText ? ($this->value($previousSource, $field) ?: null) : ($previousSource[$field] ?? null); + $new = $isText ? ($this->value($document, $field) ?: null) : ($document[$field] ?? null); + + if ($latest !== null && $this->classifyField($latest->{$column}, $old, $new, $isText) === $wanted) { $result[] = ['key' => $field, 'label' => $label]; } } @@ -436,7 +443,7 @@ public function adopt(ExerciseSourceSnapshot $snapshot, ?User $actor = null, arr $title = trim((string) ($document['title'] ?? '')) ?: 'Übung aus Shinkan'; $previousSnapshot = $this->lastAdopted($source); $previousSource = $previousSnapshot?->payload ?? []; - $composition = $source->composition ?? $this->createComposition($title, (string) ($document['summary'] ?? '')); + $composition = $source->composition ?? $this->createComposition($title, $this->value($document, 'summary')); if ($source->composition !== null && $previousSnapshot !== null) { $this->carryOverTexts($composition, $previousSource, $document); @@ -479,10 +486,11 @@ public function adopt(ExerciseSourceSnapshot $snapshot, ?User $actor = null, arr $fields = []; foreach (self::FIELDS as $field => [$column, $label]) { - $value = $document[$field] ?? null; + $isText = in_array($field, ['goal', 'equipment'], true); + $value = $isText ? ($this->value($document, $field) ?: null) : ($document[$field] ?? null); if ($previous !== null && $previousSnapshot !== null) { - $decision = $this->classifyField($previous->{$column}, $previousSource[$field] ?? null, $value); + $decision = $this->classifyField($previous->{$column}, $isText ? ($this->value($previousSource, $field) ?: null) : ($previousSource[$field] ?? null), $value, $isText); if ($decision === 'conflict') { $decision = ($choices[$field] ?? null) === 'lokal' ? 'keep_local' : (($choices[$field] ?? null) === 'quelle' ? 'source' : 'conflict'); @@ -535,10 +543,10 @@ private function plan(ExerciseSource $source, array $document, array $previousSo $title = trim((string) ($document['title'] ?? '')) ?: 'Übung aus Shinkan'; foreach (self::SECTIONS as $field => $label) { - $text = trim((string) ($document[$field] ?? '')); + $text = $this->value($document, $field); if ($text !== '') { - $plan[] = $this->textItem($source, $field, $label, $title.' · '.$label, $label, $text, null, $previousSource[$field] ?? null); + $plan[] = $this->textItem($source, $field, $label, $title.' · '.$label, $label, $text, null, $this->value($previousSource, $field) ?: null); } } @@ -554,7 +562,7 @@ private function plan(ExerciseSource $source, array $document, array $previousSo 'kind' => 'variant', 'variant' => ['key' => $key, 'name' => $name, 'description' => (string) ($variant['description'] ?? ''), 'origin' => 'quelle'], ]; - $changes = trim((string) ($variant['execution_changes'] ?? '')); + $changes = $this->variantChanges($document, $variant); if ($changes !== '') { $before = collect((array) ($previousSource['variants'] ?? []))->firstWhere('id', $key); @@ -566,7 +574,7 @@ private function plan(ExerciseSource $source, array $document, array $previousSo 'Abweichung der Durchführung', $changes, $key, - is_array($before) ? ($before['execution_changes'] ?? null) : null, + is_array($before) ? ($this->variantChanges($previousSource, $before) ?: null) : null, 'variant_'.$key, ); } @@ -599,22 +607,68 @@ private function textItem(ExerciseSource $source, string $field, string $label, private function classifyText(?ContentElementVersion $latest, string $text, ?string $previousText): string { - if ($latest === null || trim((string) $latest->body) === $text || $latest->change_note === self::IMPORT_NOTE) { + if ($latest === null || FormattedText::same($latest->body, $text) || $latest->change_note === self::IMPORT_NOTE) { return 'source'; } - return $previousText === null || trim($previousText) !== $text ? 'conflict' : 'keep_local'; + return $previousText === null || ! FormattedText::same($previousText, $text) ? 'conflict' : 'keep_local'; } - private function classifyField(mixed $local, mixed $previousSource, mixed $new): string + private function classifyField(mixed $local, mixed $previousSource, mixed $new, bool $text = false): string { - $plain = fn (mixed $value): string => $value === null ? '' : trim((string) $value); + $same = $text + ? fn (mixed $a, mixed $b): bool => FormattedText::same($a === null ? null : (string) $a, $b === null ? null : (string) $b) + : fn (mixed $a, mixed $b): bool => ($a === null ? '' : trim((string) $a)) === ($b === null ? '' : trim((string) $b)); - if ($plain($local) === $plain($new) || $plain($local) === $plain($previousSource)) { + if ($same($local, $new) || $same($local, $previousSource)) { return 'source'; } - return $plain($previousSource) !== $plain($new) ? 'conflict' : 'keep_local'; + return ! $same($previousSource, $new) ? 'conflict' : 'keep_local'; + } + + /** + * Text eines Felds der Quelle in der Form, in der er übernommen wird: Bei `text_format: html` wird das HTML bereinigt + * und kanonisch, sonst bleibt der Klartext, wie er geliefert wird. + * + * @param array $document + */ + private function value(array $document, string $field): string + { + $raw = trim((string) ($document[$field] ?? '')); + + return ($document['text_format'] ?? null) === 'html' && in_array($field, self::HTML_FIELDS, true) + ? FormattedText::canonicalHtml($raw) + : $raw; + } + + /** + * Abweichung der Durchführung einer Variante, im Format des Dokuments. + * + * @param array $document + * @param array $variant + */ + private function variantChanges(array $document, array $variant): string + { + $raw = trim((string) ($variant['execution_changes'] ?? '')); + + return ($document['text_format'] ?? null) === 'html' ? FormattedText::canonicalHtml($raw) : $raw; + } + + /** + * Ist das Feld in beiden Quellständen inhaltlich gleich? Texte werden als kanonischer Text verglichen, alles andere + * ohne führenden und folgenden Leerraum. + * + * @param array $before + * @param array $after + */ + private function sameValue(string $field, array $before, array $after): bool + { + if (in_array($field, [...self::HTML_FIELDS, 'equipment'], true)) { + return FormattedText::same($this->value($before, $field), $this->value($after, $field)); + } + + return trim((string) ($before[$field] ?? '')) === trim((string) ($after[$field] ?? '')); } /** @@ -630,8 +684,8 @@ private function carryOverTexts(Composition $composition, array $before, array $ $composition->designation = trim((string) $after['title']); } - if (array_key_exists('summary', $after) && ((string) $composition->summary === '' || $composition->summary === ($before['summary'] ?? null))) { - $composition->summary = trim((string) $after['summary']) ?: null; + if (array_key_exists('summary', $after) && ((string) $composition->summary === '' || FormattedText::same($composition->summary, $this->value($before, 'summary')))) { + $composition->summary = $this->value($after, 'summary') ?: null; } $composition->save(); @@ -708,7 +762,7 @@ private function sectionVersion(ExerciseSource $source, string $field, string $t $element->save(); } - return $latest !== null && trim((string) $latest->body) === $text + return $latest !== null && FormattedText::same($latest->body, $text) ? $latest : $content->publishElementVersion($element, $text, [], self::IMPORT_NOTE); } diff --git a/docs/fachkonzept/uebungen-shinkan-feldzuordnung.md b/docs/fachkonzept/uebungen-shinkan-feldzuordnung.md index cbc2a2f..3d176e8 100644 --- a/docs/fachkonzept/uebungen-shinkan-feldzuordnung.md +++ b/docs/fachkonzept/uebungen-shinkan-feldzuordnung.md @@ -35,8 +35,12 @@ ## 1. Felder, die der Katalog liefert und die Lara & Felix übernimmt | Variante `sequence_order` | `variants[].sequence_order` | Position der Variante (Reihenfolge der Antwort) | Kundenansicht | | Medien `public_id`, `media_type`, `title`, `sort_order` | `media[]` | **Nur Quellenangabe**: gespeichert im Stand der Quelle, gezeigt in der Vorschau. Nie Medienausgabe, nie Teil eines Artikel-Stands | Übernahme (Vorschau) | -Klartext: Der Katalog liefert Texte als Klartext (`plaintext`). Absätze und Listen entstehen aus den Konventionen der +Textformat: Der Katalog nennt das Format mit `text_format`. Bei `html` (Stand Shinkan vom 23.11.2026) sind Kurzbeschreibung, +Lernziel, Durchführung, Vorbereitung, Trainerhinweise und die Abweichung der Durchführung einer Variante bereinigtes HTML +(Absatz, Zwischenüberschrift, fett, kursiv, Listen, Link, Zeilenumbruch; ADR-50). Verweise auf Quellmedien im Text entfallen. +Titel, Variantenbeschreibung und Ausstattung bleiben Text. Fehlt die Angabe, sind alle Texte Klartext mit den Konventionen der Anleitung (Leerzeilen, „- “, „1. “). Zeichen außerhalb von Windows-1252 erscheinen im PDF lesbar ersetzt, sonst als „?“. +Fixtures: `katalog-detail.json` (Klartext) und `katalog-detail-html.json` (HTML). ## 2. Verhalten beim Fassungswechsel diff --git a/tests/Feature/ShinkanUebernahmeTest.php b/tests/Feature/ShinkanUebernahmeTest.php index ac4c20c..1e320d2 100644 --- a/tests/Feature/ShinkanUebernahmeTest.php +++ b/tests/Feature/ShinkanUebernahmeTest.php @@ -134,6 +134,81 @@ public function test_the_representative_catalog_answer_lands_in_the_documented_p $this->assertSame(['Quellvideo Sicherer Stand (video)'], $preview['media'], 'Medien bleiben Quellenangabe ohne Datei.'); } + public function test_the_representative_html_answer_lands_formatted_in_the_documented_places(): void + { + $document = json_decode((string) file_get_contents(base_path('tests/Fixtures/shinkan/katalog-detail-html.json')), true, 512, JSON_THROW_ON_ERROR); + $import = app(ShinkanImport::class); + + $version = $import->adopt($import->record($document)); + $body = fn (string $role): string => (string) $version->members->firstWhere('role', $role)->contentElementVersion->body; + + $this->assertSame('

Die Kinder üben einen sicheren Stand und ein deutliches Stopp.

', $version->composition->summary); + $this->assertSame('

Die Kinder können mit fester Stimme und sicherem Stand Stopp sagen.

', $version->learning_goal); + $this->assertSame('Matten, Pratzen', $version->material_note); + $this->assertStringContainsString('
  1. Sicheren Stand einnehmen

  2. ', $body('Durchführung')); + $this->assertStringContainsString('wie sich der Stand angefühlt hat', $body('Durchführung')); + $this->assertStringNotContainsString('data-shinkan', $body('Durchführung'), 'Der Verweis auf ein Quellmedium entfällt.'); + $this->assertSame('

    Matten im Kreis auslegen.
    Genug Platz für Abstand zwischen den Kindern lassen.

    ', str_replace('
    ', '
    ', $body('Vorbereitung'))); + $this->assertStringContainsString('