From 59434a4045e78f95e3a47ecfe16d4b5dd58ab0d4 Mon Sep 17 00:00:00 2001 From: Lars Date: Sun, 18 Oct 2026 14:56:36 +0200 Subject: [PATCH] Strukturiere index.html und inhalt.md nach Vorgangshierarchie. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Kapitel, Teilvorgänge und Schriftstücke erscheinen verschachtelt; Anlagen, Belege, Versand und Anmerkungen sind dem jeweiligen Eintrag zugeordnet, mit Inhaltsverzeichnis und klarerer HTML-Darstellung. Co-authored-by: Cursor --- backend/app/exporting.py | 343 ++++++++++++++++++++++++++++++----- backend/tests/test_export.py | 5 + 2 files changed, 302 insertions(+), 46 deletions(-) diff --git a/backend/app/exporting.py b/backend/app/exporting.py index e1fca0e..ef18ed4 100644 --- a/backend/app/exporting.py +++ b/backend/app/exporting.py @@ -136,6 +136,15 @@ class Entry: } +@dataclass +class OverviewNode: + kind: str + title: str + label: str | None = None + entry: Entry | None = None + children: list[OverviewNode] = field(default_factory=list) + + @dataclass class ExportPlan: matter_title: str @@ -263,7 +272,7 @@ def _plan(conn, actor, matter_id, context, selected, assignments, media_root) -> ) _attach_replies(conn, actor, page, entries, leaves) _stitch_replies(files, entries) - files = _with_overview(matter["title"], generated, entries, files) + files = _with_overview(matter["title"], generated, root, entries, files) return ExportPlan(matter["title"], generated, True, [], notices, entries, files, root) @@ -866,67 +875,309 @@ def _attach_replies(conn, actor, page, entries: list[Entry], leaves: list[Export entry.replies.append((target["title"], path)) -def _with_overview(title: str, generated: str, entries: list[Entry], files: list[tuple[str, bytes]]): - html_doc = _html_overview(title, generated, entries) - markdown = _markdown_overview(title, generated, entries) +def _with_overview(title: str, generated: str, root: ExportNode, entries: list[Entry], files: list[tuple[str, bytes]]): + by_id = {entry.node_id: entry for entry in entries} + sections = _overview_sections(root, by_id) + html_doc = _html_overview(title, generated, sections) + markdown = _markdown_overview(title, generated, sections) return [("index.html", html_doc.encode("utf-8")), ("inhalt.md", markdown.encode("utf-8")), *files] -def _html_overview(title: str, generated: str, entries: list[Entry]) -> str: - items = [] - for entry in entries: - bits = [f"

{html.escape(entry.title)}

", f"

{html.escape(entry.kind_label)}

"] - facts = _fact_lines(entry, generated) - if facts: - bits.append("") - if entry.summary: - bits.append(f"

Kurzfassung: {html.escape(entry.summary)}

") - for label, path in _entry_links(entry): - bits.append(f'

{html.escape(label)}

') - for reply_title, path in entry.replies: - bits.append( - f'

Antwort auf {html.escape(reply_title)}

' - ) - if entry.notice: - bits.append(f"

{html.escape(entry.notice)}

") - items.append(f"
{''.join(bits)}
") - body = "".join(items) or "

Die bestätigte Auswahl enthält keinen ausgebbaren Inhalt.

" +def _overview_sections(root: ExportNode, entries: dict[str, Entry]) -> list[OverviewNode]: + found: list[OverviewNode] = [] + for child in root.children: + section = _overview_node(child, entries) + if section is not None: + found.append(section) + return found + + +def _overview_node(node: ExportNode, entries: dict[str, Entry]) -> OverviewNode | None: + if node.kind in {"matter", "chapter"}: + children = [] + for child in node.children: + section = _overview_node(child, entries) + if section is not None: + children.append(section) + if not children: + return None + return OverviewNode("container", node.title, node.label or _KIND_LABEL.get(node.kind, ""), None, children) + if node.kind == "binding": + return _overview_binding(node, entries) + return None + + +def _overview_binding(node: ExportNode, entries: dict[str, Entry]) -> OverviewNode | None: + groups: list[OverviewNode] = [] + annotations = [ + OverviewNode("leaf", child.title, _label(child), entries[child.id]) + for child in node.children + if child.kind == "annotation" and child.id in entries + ] + if annotations: + groups.append(OverviewNode("group", "Anmerkungen", None, None, annotations)) + anlagen = [ + OverviewNode("leaf", child.title, _label(child), entries[child.id]) + for child in node.children + if child.kind == "attachment" and child.id in entries + ] + if anlagen: + groups.append(OverviewNode("group", "Anlagen", None, None, anlagen)) + belege: list[OverviewNode] = [] + versand: list[OverviewNode] = [] + for dispatch in node.children: + if dispatch.kind != "dispatch": + continue + items = [ + OverviewNode("leaf", item.title, _label(item), entries[item.id]) + for item in dispatch.children + if item.kind in {"dispatch_item", "evidence"} and item.id in entries + ] + if not items: + continue + bucket = "Belege" if (dispatch.label or "").startswith("Beleg") else (dispatch.label or "Versand") + target = belege if bucket == "Belege" else versand + target.append(OverviewNode("group", dispatch.title, bucket, None, items)) + if belege: + groups.append(OverviewNode("group", "Belege", None, None, belege)) + if versand: + groups.extend(versand) + host = entries.get(node.id) + if host is None and not groups: + return None + return OverviewNode("binding", node.title, node.label or _label(node), host, groups) + + +def _overview_toc(sections: list[OverviewNode], counter: list[int]) -> tuple[list[tuple[str, str]], list[int]]: + items: list[tuple[str, str]] = [] + + def walk(nodes: list[OverviewNode]) -> None: + for node in nodes: + if node.kind not in {"container", "binding"}: + continue + counter[0] += 1 + anchor = f"inhalt-{counter[0]}" + label = node.title + if node.label and node.kind == "container": + label = f"{node.label}: {node.title}" + items.append((anchor, label)) + if node.kind == "container": + walk(node.children) + + walk(sections) + return items, counter + + +def _html_overview(title: str, generated: str, sections: list[OverviewNode]) -> str: + counter = [0] + toc, _counter = _overview_toc(sections, counter) + counter[0] = 0 + toc_html = "" + if toc: + toc_html = ( + '" + ) + body = _html_overview_nodes(sections, generated, counter) or "

Die bestätigte Auswahl enthält keinen ausgebbaren Inhalt.

" return ( "" f"Übergabe {html.escape(title)}" - "" - f"

Übergabe {html.escape(title)}

" - f"

Erzeugt am {html.escape(generated)}. Fachliche Daten stehen bei den einzelnen Inhalten und sind kein Erzeugungszeitpunkt.

" - f"{body}" + "" + f"

Übergabe {html.escape(title)}

" + f"

Erzeugt am {html.escape(generated)}. Fachliche Daten stehen bei den einzelnen Inhalten und sind kein Erzeugungszeitpunkt.

" + f"{toc_html}{body}" ) -def _markdown_overview(title: str, generated: str, entries: list[Entry]) -> str: +def _html_overview_nodes(sections: list[OverviewNode], generated: str, counter: list[int]) -> str: + parts = [] + for section in sections: + parts.append(_html_overview_node(section, generated, counter, depth=2)) + return "".join(parts) + + +def _html_overview_node(node: OverviewNode, generated: str, counter: list[int], depth: int) -> str: + counter[0] += 1 + anchor = f"inhalt-{counter[0]}" + if node.kind == "container": + inner = "".join(_html_overview_node(child, generated, counter, depth + 1) for child in node.children) + label = f'{html.escape(node.label)}' if node.label else "" + return ( + f'
' + f'

{label}{html.escape(node.title)}

{inner}
' + ) + if node.kind == "binding": + bits = [ + f'
', + '
', + f'{html.escape(node.title)}', + ] + if node.label: + bits.append(f'{html.escape(node.label)}') + bits.append("
") + if node.entry: + bits.append(_html_entry_body(node.entry, generated)) + for child in node.children: + bits.append(_html_overview_group(child, generated)) + bits.append("
") + return "".join(bits) + return "" + + +def _html_overview_group(node: OverviewNode, generated: str) -> str: + if node.kind == "leaf": + return _html_leaf(node.entry, generated) + if node.kind != "group": + return "" + subtitle = f" · {html.escape(node.label)}" if node.label and node.title != node.label else "" + title = html.escape(node.title) + if node.children and all(child.kind == "leaf" for child in node.children): + inner = "".join(_html_leaf(child.entry, generated) for child in node.children) + return f'

{title}{subtitle}

    {inner}
' + inner = "".join(_html_overview_group(child, generated) for child in node.children) + return f'

{title}{subtitle}

{inner}
' + + +def _html_leaf(entry: Entry | None, generated: str) -> str: + if entry is None: + return "" + links = _entry_links(entry) + link_bits = "".join( + f'{html.escape(label)}' for label, path in links + ) + meta = f'{html.escape(entry.kind_label)}' if entry.kind_label else "" + return f'
  • {html.escape(entry.title)} {meta} — {link_bits}
  • ' + + +def _html_entry_body(entry: Entry, generated: str) -> str: + bits = [] + facts = _fact_lines(entry, generated) + if facts: + bits.append("") + if entry.summary: + bits.append(f'') + links = _entry_links(entry) + if links: + bits.append( + '

    ' + + "".join(f'{html.escape(label)}' for label, path in links) + + "

    " + ) + for reply_title, path in entry.replies: + bits.append(f'') + if entry.notice: + bits.append(f'

    {html.escape(entry.notice)}

    ') + return "".join(bits) + + +def _markdown_overview(title: str, generated: str, sections: list[OverviewNode]) -> str: lines = [ f"# Übergabe {title}", "", f"Erzeugt am {generated}. Fachliche Daten stehen bei den einzelnen Inhalten und sind kein Erzeugungszeitpunkt.", "", ] - for entry in entries: - lines.append(f"## {entry.title}") + if sections: + lines.extend(["## Inhalt", ""]) + for anchor, label in _overview_toc(sections, [0])[0]: + lines.append(f"- [{label}](#{anchor})") lines.append("") - lines.append(entry.kind_label) - lines.append("") - for fact in _fact_lines(entry, generated): - lines.append(f"- {fact}") - if entry.summary: - lines.append(f"- Kurzfassung: {entry.summary}") - for label, path in _entry_links(entry): - lines.append(f"- [{label}]({_href(path)})") - for reply_title, path in entry.replies: - lines.append(f"- Antwort auf [{reply_title}]({_href(path)})") - if entry.notice: - lines.append("") - lines.append(entry.notice) - lines.append("") - return "\n".join(lines).rstrip() + "\n" + lines.extend(_markdown_overview_nodes(sections, generated, depth=2)) + return "\n".join(line for line in lines if line is not None).rstrip() + "\n" + + +def _markdown_overview_nodes(sections: list[OverviewNode], generated: str, depth: int) -> list[str]: + lines: list[str] = [] + counter = [0] + + def walk(nodes: list[OverviewNode], level: int) -> None: + for node in nodes: + if node.kind == "container": + counter[0] += 1 + anchor = f"inhalt-{counter[0]}" + heading = "#" * min(level, 6) + label = f"{node.label}: " if node.label else "" + lines.extend(["", f"{heading} {label}{node.title}", f"", ""]) + walk(node.children, level + 1) + elif node.kind == "binding": + counter[0] += 1 + anchor = f"inhalt-{counter[0]}" + heading = "#" * min(level, 6) + label = f" ({node.label})" if node.label else "" + lines.extend(["", f"{heading} {node.title}{label}", f"", ""]) + if node.entry: + lines.extend(_markdown_entry_body(node.entry, generated)) + for child in node.children: + lines.extend(_markdown_group(child, generated, level + 1)) + lines.append("") + + walk(sections, depth) + return lines + + +def _markdown_group(node: OverviewNode, generated: str, depth: int) -> list[str]: + if node.kind == "leaf": + return _markdown_leaf(node.entry, depth) + if node.kind != "group": + return [] + heading = "#" * min(depth, 6) + title = node.title + if node.label and node.label not in title: + title = f"{title} ({node.label})" + lines = ["", f"{heading} {title}", ""] + for child in node.children: + lines.extend(_markdown_group(child, generated, depth + 1)) + return lines + + +def _markdown_leaf(entry: Entry | None, depth: int) -> list[str]: + if entry is None: + return [] + indent = " " * max(0, depth - 4) + links = _entry_links(entry) + if not links: + return [f"{indent}- {entry.title} ({entry.kind_label})"] + link = " · ".join(f"[{label}]({_href(path)})" for label, path in links) + return [f"{indent}- **{entry.title}** ({entry.kind_label}): {link}"] + + +def _markdown_entry_body(entry: Entry, generated: str) -> list[str]: + lines = [] + for fact in _fact_lines(entry, generated): + lines.append(f"- {fact}") + if entry.summary: + lines.append(f"- Kurzfassung: {entry.summary}") + for label, path in _entry_links(entry): + lines.append(f"- [{label}]({_href(path)})") + for reply_title, path in entry.replies: + lines.append(f"- Antwort auf [{reply_title}]({_href(path)})") + if entry.notice: + lines.extend(["", entry.notice]) + return lines def _entry_links(entry: Entry) -> list[tuple[str, str]]: diff --git a/backend/tests/test_export.py b/backend/tests/test_export.py index 4f7c65b..f0a1edc 100644 --- a/backend/tests/test_export.py +++ b/backend/tests/test_export.py @@ -976,6 +976,11 @@ def test_correspondence_attachments_stay_under_each_host_in_one_chapter(client, assert sorted(name.rsplit("/", 1)[-1] for name in second_paths) == ["Anlage B1.pdf", "Anlage B2.pdf"] assert any("/01 Erstes Schreiben/Belege/" in name.replace("\\", "/") for name in names) assert not any(name.replace("\\", "/").startswith("01 Schriftwechsel/2026-04") for name in names) + overview_md = _archive(response.content).read("inhalt.md").decode("utf-8") + overview_html = _archive(response.content).read("index.html").decode("utf-8") + assert "#### Anlagen" in overview_md + assert "Anlage A1" in overview_md + assert "entry-group" in overview_html and "Anlagen" in overview_html def test_export_warns_when_assigned_attachment_is_not_shareable(client, db):