From 5ff0cd43ee04a4b51e42a2175644eb649a33c138 Mon Sep 17 00:00:00 2001 From: Lars Date: Mon, 23 Nov 2026 22:12:14 +0100 Subject: [PATCH] Deliver stored exercise HTML on the catalog API. The receiving application needs paragraphs, line breaks, lists and emphasis as stored in Shinkan. The catalog now returns that HTML and marks the text format, while titles and short variant descriptions stay plain text. Co-authored-by: Cursor --- backend/distribution_contract.py | 31 +++++++++++++++------ backend/tests/test_distribution_contract.py | 22 +++++++++++++++ backend/version.py | 11 ++++++-- 3 files changed, 54 insertions(+), 10 deletions(-) diff --git a/backend/distribution_contract.py b/backend/distribution_contract.py index 8145778..609bb64 100644 --- a/backend/distribution_contract.py +++ b/backend/distribution_contract.py @@ -15,6 +15,8 @@ from datetime import datetime, timezone from html import unescape from typing import Any, Iterable, Optional +from exercise_rich_text import normalize_inline_exercise_media_markup + PART_ROLES = ( "goal", "prerequisites", @@ -235,8 +237,20 @@ def catalog_available(offered: bool, in_scope: bool) -> bool: return bool(offered) and bool(in_scope) +def catalog_html(value: Optional[str]) -> str: + """Gespeichertes Übungs-HTML. Absätze, Umbrüche, Listen und Hervorhebungen bleiben erhalten.""" + if value is None: + return "" + normalized = normalize_inline_exercise_media_markup(str(value)) + return normalized or "" + + def catalog_document(exercise: dict, variants: list[dict], media: list[dict]) -> dict: - """Aktueller Übungsstand. Die Reihenfolge der Varianten und Medien gehört zum Inhalt.""" + """Aktueller Übungsstand. Die Reihenfolge der Varianten und Medien gehört zum Inhalt. + + text_format html gilt für summary, goal, execution, preparation, trainer_notes + und variants.execution_changes. Titel, Variantenbeschreibung und Material bleiben Text. + """ ordered_variants = sorted( variants, key=lambda item: (item.get("sequence_order") is None, item.get("sequence_order") or 0, int(item.get("id") or 0)), @@ -247,12 +261,13 @@ def catalog_document(exercise: dict, variants: list[dict], media: list[dict]) -> ) return { "source_id": str(exercise.get("public_id")), + "text_format": "html", "title": exercise.get("title") or "", - "summary": plaintext(exercise.get("summary")), - "goal": plaintext(exercise.get("goal")), - "execution": plaintext(exercise.get("execution")), - "preparation": plaintext(exercise.get("preparation")), - "trainer_notes": plaintext(exercise.get("trainer_notes")), + "summary": catalog_html(exercise.get("summary")), + "goal": catalog_html(exercise.get("goal")), + "execution": catalog_html(exercise.get("execution")), + "preparation": catalog_html(exercise.get("preparation")), + "trainer_notes": catalog_html(exercise.get("trainer_notes")), "equipment": _equipment_text(exercise) or "", "duration_min": exercise.get("duration_min"), "duration_max": exercise.get("duration_max"), @@ -260,8 +275,8 @@ def catalog_document(exercise: dict, variants: list[dict], media: list[dict]) -> { "id": str(item.get("public_id")), "name": item.get("variant_name") or "", - "description": plaintext(item.get("description")), - "execution_changes": plaintext(item.get("execution_changes")), + "description": (item.get("description") or "").strip(), + "execution_changes": catalog_html(item.get("execution_changes")), "sequence_order": item.get("sequence_order"), } for item in ordered_variants diff --git a/backend/tests/test_distribution_contract.py b/backend/tests/test_distribution_contract.py index 76b4447..a1f51ae 100644 --- a/backend/tests/test_distribution_contract.py +++ b/backend/tests/test_distribution_contract.py @@ -345,6 +345,28 @@ def test_catalog_reports_variant_and_media_order_changes() -> None: ] +def test_catalog_keeps_stored_html() -> None: + exercise = _catalog_exercise() + exercise["execution"] = "

Stand aufrecht.

Drehe die Hüfte.


" + exercise["goal"] = "

Sehen

" + variant = [{ + "id": 1, + "public_id": "var-1", + "variant_name": "Karte", + "description": "Kurzer Text", + "execution_changes": "

Langsamer.

Mit Partner.

", + "sequence_order": 1, + }] + document = catalog_document(exercise, variant, []) + assert document["text_format"] == "html" + assert document["execution"] == "

Stand aufrecht.

Drehe die Hüfte.


" + assert document["goal"] == "

Sehen

" + assert "