#!/usr/bin/env python3 """Konsolidiert das HiveGrid-2.0-Pflichtenheft in ein einziges PDF. Liest alle MD-Teile in fester Reihenfolge, fügt sie zusammen, generiert ein Inhaltsverzeichnis aus den H1/H2/H3-Überschriften und schreibt eine PDF. """ import sys import re from pathlib import Path import markdown from weasyprint import HTML, CSS ROOT = Path("/Users/raffaelsheikh/Documents/GitHub/HiveGrid-Cloud/_bmad-output/pflichtenheft") OUT_PDF = ROOT / "HiveGrid-2.0-Pflichtenheft.pdf" OUT_HTML = ROOT / "HiveGrid-2.0-Pflichtenheft.html" # Reihenfolge: Frame, dann Module-Blöcke, dann Anhang PARTS = [ ROOT / "00-cover.md", ROOT / "01-vorwort.md", ROOT / "02-vision.md", ROOT / "03-architektur.md", ROOT / "blocks" / "block1.md", ROOT / "blocks" / "block2.md", ROOT / "blocks" / "block3.md", ROOT / "blocks" / "block4.md", ROOT / "blocks" / "block5.md", ROOT / "blocks" / "block6.md", ROOT / "blocks" / "block7.md", ROOT / "99-anhang.md", ] CSS_STYLE = """ @page { size: A4 portrait; margin: 24mm 20mm 22mm 20mm; @bottom-center { content: "HiveGrid 2.0 · Pflichtenheft · Seite " counter(page) " / " counter(pages); font-family: "Inter", -apple-system, "Helvetica Neue", sans-serif; font-size: 8.5pt; color: #8b949e; } @top-right { content: "Stand 9. Mai 2026 · Entwurf"; font-family: "Inter", -apple-system, sans-serif; font-size: 8pt; color: #b0b8c0; } } @page :first { margin: 0; @bottom-center { content: ""; } @top-right { content: ""; } } @page cover { margin: 0; @bottom-center { content: ""; } @top-right { content: ""; } } * { box-sizing: border-box; } body { font-family: "Inter", -apple-system, BlinkMacSystemFont, "Helvetica Neue", "Segoe UI", sans-serif; font-size: 10.5pt; line-height: 1.55; color: #1f2328; margin: 0; font-feature-settings: "ss01", "cv11"; } /* ============================================================ */ /* COVER */ /* ============================================================ */ .cover { page: cover; width: 210mm; height: 297mm; padding: 35mm 28mm 30mm 28mm; background: #0d1117; background-image: radial-gradient(circle at 80% 10%, rgba(74, 108, 247, 0.18) 0%, transparent 45%), radial-gradient(circle at 15% 90%, rgba(255, 145, 60, 0.10) 0%, transparent 50%), linear-gradient(160deg, #0d1117 0%, #161b22 100%); color: #f0f6fc; display: flex; flex-direction: column; justify-content: space-between; page-break-after: always; } .cover-eyebrow { font-size: 9.5pt; letter-spacing: 2.5pt; text-transform: uppercase; color: #8b949e; font-weight: 500; } .cover-title { font-size: 64pt; font-weight: 800; letter-spacing: -2pt; line-height: 1.0; margin: 0; padding: 0; border: none; color: #f0f6fc; } .cover-product { font-size: 28pt; font-weight: 600; color: #c9d1d9; margin-top: 2pt; letter-spacing: -0.5pt; } .cover-tagline { font-size: 14pt; color: #8b949e; margin-top: 4pt; font-weight: 400; } .cover-rule { width: 72pt; height: 3pt; background: #4a6cf7; margin: 22pt 0 18pt 0; } .cover-meta { font-size: 10pt; color: #c9d1d9; display: block; } .cover-meta > div { display: block; padding: 6pt 0; border-bottom: 1px solid #21262d; } .cover-meta > div:last-child { border-bottom: none; } .cover-meta span { display: inline-block; width: 28%; color: #6e7681; font-size: 8.5pt; letter-spacing: 1pt; text-transform: uppercase; font-weight: 500; } .cover-meta strong { color: #f0f6fc; font-weight: 500; } .cover-foot { font-size: 9pt; color: #6e7681; line-height: 1.5; border-top: 1px solid #21262d; padding-top: 12pt; font-style: italic; } /* ============================================================ */ /* PAGE-BREAK HELPER */ /* ============================================================ */ .page-break { page-break-after: always; } /* ============================================================ */ /* HEADINGS */ /* ============================================================ */ /* H1 = Hauptkapitel (Vorwort, Vision, Architektur, Block N, Anhang). Each starts a new page. */ h1 { font-size: 28pt; margin: 0 0 16pt 0; padding: 0 0 12pt 0; color: #0d1117; border-bottom: 4px solid #0d1117; page-break-before: always; page-break-after: avoid; font-weight: 800; letter-spacing: -1pt; line-height: 1.15; } h1:first-of-type { page-break-before: auto; } /* H2 = Unter-Kapitel oder Modul-Überschrift */ h2 { font-size: 17pt; margin: 28pt 0 10pt 0; padding: 14pt 0 6pt 0; border-top: 2px solid #0d1117; page-break-after: avoid; page-break-inside: avoid; color: #0d1117; font-weight: 700; letter-spacing: -0.3pt; } /* H3 = Sub-Section innerhalb eines Moduls (z.B. "27.1 Anmeldung") */ h3 { font-size: 12.5pt; margin: 20pt 0 6pt 0; padding: 0 0 0 10pt; border-left: 3px solid #4a6cf7; page-break-after: avoid; page-break-inside: avoid; color: #0d1117; font-weight: 700; letter-spacing: -0.1pt; } h4 { font-size: 11pt; margin: 14pt 0 4pt 0; page-break-after: avoid; color: #1f2328; font-weight: 600; } /* keep heading together with first paragraph/list */ h2 + p, h2 + ul, h2 + ol, h3 + p, h3 + ul, h3 + ol, h4 + p, h4 + ul, h4 + ol { page-break-before: avoid; } /* ============================================================ */ /* PARAGRAPHS, LISTS */ /* ============================================================ */ p { margin: 0 0 8pt 0; orphans: 3; widows: 3; } ul, ol { margin: 6pt 0 10pt 0; padding-left: 22pt; orphans: 3; widows: 3; } li { margin: 3pt 0; } li > ul, li > ol { margin: 3pt 0 3pt 0; } /* ============================================================ */ /* CODE */ /* ============================================================ */ code { font-family: "JetBrains Mono", "SF Mono", "Monaco", "Menlo", monospace; font-size: 9pt; background: #f6f8fa; padding: 1pt 4pt; border-radius: 3pt; border: 1px solid #e1e4e8; color: #24292f; } pre { background: #f6f8fa; border: 1px solid #d0d7de; border-left: 3px solid #4a6cf7; border-radius: 4pt; padding: 9pt 12pt; font-family: "JetBrains Mono", "SF Mono", "Monaco", "Menlo", monospace; font-size: 8.5pt; line-height: 1.5; page-break-inside: avoid; white-space: pre-wrap; word-wrap: break-word; margin: 10pt 0; color: #24292f; } pre code { background: transparent; border: none; padding: 0; font-size: 8.5pt; } /* ============================================================ */ /* TABLES */ /* ============================================================ */ table { width: 100%; border-collapse: collapse; margin: 10pt 0 14pt 0; page-break-inside: avoid; font-size: 9.5pt; border: 1px solid #d0d7de; } th, td { border-bottom: 1px solid #e1e4e8; border-right: 1px solid #e1e4e8; padding: 6pt 9pt; text-align: left; vertical-align: top; line-height: 1.45; } th:last-child, td:last-child { border-right: none; } tr:last-child td { border-bottom: none; } th { background: #f6f8fa; font-weight: 600; color: #0d1117; border-bottom: 2px solid #d0d7de; } tr:nth-child(even) td { background: #fbfcfd; } /* ============================================================ */ /* BLOCKQUOTES (used for boxed callouts in vorwort) */ /* ============================================================ */ blockquote { border-left: 3px solid #4a6cf7; padding: 8pt 14pt; margin: 10pt 0; background: #eff5ff; color: #1c357a; page-break-inside: avoid; border-radius: 0 4pt 4pt 0; font-style: normal; } blockquote p { margin: 0 0 4pt 0; } blockquote p:last-child { margin-bottom: 0; } blockquote strong { color: #0d1117; } blockquote ul, blockquote ol { margin: 4pt 0; padding-left: 18pt; } /* ============================================================ */ /* HORIZONTAL RULES */ /* ============================================================ */ hr { border: none; border-top: 1px solid #d0d7de; margin: 16pt 0; } /* ============================================================ */ /* TABLE OF CONTENTS */ /* ============================================================ */ .toc { margin: 0; padding: 0; page-break-after: always; } .toc h1 { page-break-before: auto; border-bottom: 4px solid #0d1117; } .toc ul { list-style: none; padding-left: 0; margin: 0; } .toc ul ul { padding-left: 16pt; margin: 0; } .toc li { margin: 0; padding: 0; } .toc a { color: #1f2328; text-decoration: none; display: block; padding: 1pt 0; } .toc .toc-h1 { font-weight: 700; font-size: 11pt; margin-top: 12pt; color: #0d1117; letter-spacing: -0.2pt; } .toc .toc-h1:first-child { margin-top: 0; } .toc .toc-h2 { font-weight: 500; font-size: 9.5pt; color: #424a53; margin-top: 2pt; } .toc .toc-h3 { font-size: 9pt; color: #6e7681; font-weight: 400; } """ def slugify(text): """Build an anchor-safe slug.""" s = re.sub(r"[^\w\s-]", "", text.lower()) s = re.sub(r"\s+", "-", s).strip("-") return s def build_toc(combined_md): """Parse top-level headings (h1+h2 only) from the combined markdown. Sub-sections (h3, e.g. "27.1 Anmeldung") are not listed in the TOC because they would blow it up to 6+ pages. Readers locate sub-sections via the numbered scheme (M17.5.2) once they're inside a module. """ lines = combined_md.split("\n") headings = [] in_code_block = False for line in lines: if line.strip().startswith("```"): in_code_block = not in_code_block continue if in_code_block: continue m = re.match(r"^(#{1,2})\s+(.*?)(\s*\{#.*\})?$", line) if m: level = len(m.group(1)) title = m.group(2).strip() slug = slugify(title) headings.append((level, title, slug)) if not headings: return "" html = ['
', "

Inhaltsverzeichnis

", "" * prev) html.append("
") return "\n".join(html) def add_anchors(html): """Inject id="slug" attributes into h1/h2/h3 tags so TOC links resolve.""" def inject(match): tag = match.group(1) text = match.group(2) # strip any HTML inside text for slug clean = re.sub(r"<[^>]+>", "", text) slug = slugify(clean) return f'<{tag} id="{slug}">{text}' return re.sub(r"<(h[1-3])>(.*?)", inject, html) def main(): parts = [] for p in PARTS: if not p.exists(): print(f" ! Missing: {p}") continue text = p.read_text(encoding="utf-8") parts.append(text) print(f" + {p.name}: {len(text.splitlines())} lines") combined = "\n\n".join(parts) print(f"\nCombined: {len(combined.splitlines())} total lines") # Build TOC from headings toc_html = build_toc(combined) # Render markdown to HTML md = markdown.Markdown( extensions=["tables", "fenced_code", "sane_lists", "attr_list", "md_in_html"], ) body_html = md.convert(combined) body_html = add_anchors(body_html) # Insert TOC after the cover. Cover ends with a
# marker (set in 00-cover.md). Cover already has its own page-break-after, # so we do not add an extra one here. cover_split = body_html.split('
', 1) if len(cover_split) == 2: cover_html, rest_html = cover_split full_body = cover_html + toc_html + rest_html else: full_body = toc_html + body_html html_full = f""" HiveGrid 2.0 - Pflichtenheft {full_body} """ OUT_HTML.write_text(html_full, encoding="utf-8") print(f" → {OUT_HTML.name} ({OUT_HTML.stat().st_size // 1024} KB)") HTML(string=html_full, base_url=str(ROOT)).write_pdf( OUT_PDF, stylesheets=[CSS(string=CSS_STYLE)] ) print(f" → {OUT_PDF.name} ({OUT_PDF.stat().st_size // 1024} KB)") return 0 if __name__ == "__main__": sys.exit(main())