backend-lehrer (11 files): - llm_gateway/routes/schools.py (867 → 5), recording_api.py (848 → 6) - messenger_api.py (840 → 5), print_generator.py (824 → 5) - unit_analytics_api.py (751 → 5), classroom/routes/context.py (726 → 4) - llm_gateway/routes/edu_search_seeds.py (710 → 4) klausur-service (12 files): - ocr_labeling_api.py (845 → 4), metrics_db.py (833 → 4) - legal_corpus_api.py (790 → 4), page_crop.py (758 → 3) - mail/ai_service.py (747 → 4), github_crawler.py (767 → 3) - trocr_service.py (730 → 4), full_compliance_pipeline.py (723 → 4) - dsfa_rag_api.py (715 → 4), ocr_pipeline_auto.py (705 → 4) website (6 pages): - audit-checklist (867 → 8), content (806 → 6) - screen-flow (790 → 4), scraper (789 → 5) - zeugnisse (776 → 5), modules (745 → 4) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
295 lines
7.7 KiB
Python
295 lines
7.7 KiB
Python
"""
|
|
AI Processing - Print Version Generator: Worksheet.
|
|
|
|
Generates print-optimized HTML for general worksheets from analysis data.
|
|
"""
|
|
|
|
from pathlib import Path
|
|
import json
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def generate_print_version_worksheet(analysis_path: Path) -> str:
|
|
"""
|
|
Generiert eine druckoptimierte HTML-Version des Arbeitsblatts.
|
|
|
|
Eigenschaften:
|
|
- Grosse, gut lesbare Schrift (16pt)
|
|
- Schwarz-weiss / Graustufen-tauglich
|
|
- Klare Struktur fuer Druck
|
|
- Keine interaktiven Elemente
|
|
|
|
Args:
|
|
analysis_path: Pfad zur *_analyse.json Datei
|
|
|
|
Returns:
|
|
HTML-String zum direkten Ausliefern
|
|
"""
|
|
if not analysis_path.exists():
|
|
raise FileNotFoundError(f"Analysedatei nicht gefunden: {analysis_path}")
|
|
|
|
try:
|
|
data = json.loads(analysis_path.read_text(encoding="utf-8"))
|
|
except json.JSONDecodeError as e:
|
|
raise RuntimeError(f"Analyse-Datei enthaelt kein gueltiges JSON: {analysis_path}\n{e}") from e
|
|
|
|
title = data.get("title") or "Arbeitsblatt"
|
|
subject = data.get("subject") or ""
|
|
grade_level = data.get("grade_level") or ""
|
|
instructions = data.get("instructions") or ""
|
|
tasks = data.get("tasks", []) or []
|
|
canonical_text = data.get("canonical_text") or ""
|
|
printed_blocks = data.get("printed_blocks") or []
|
|
|
|
html_parts = []
|
|
html_parts.append(_build_html_head(title))
|
|
|
|
# Titel
|
|
html_parts.append(f"<h1>{title}</h1>")
|
|
|
|
# Meta-Informationen
|
|
meta_parts = []
|
|
if subject:
|
|
meta_parts.append(f"<span><strong>Fach:</strong> {subject}</span>")
|
|
if grade_level:
|
|
meta_parts.append(f"<span><strong>Klasse:</strong> {grade_level}</span>")
|
|
if meta_parts:
|
|
html_parts.append(f"<div class='meta'>{''.join(meta_parts)}</div>")
|
|
|
|
# Arbeitsanweisung
|
|
if instructions:
|
|
html_parts.append("<div class='instructions'>")
|
|
html_parts.append("<div class='instructions-label'>Arbeitsanweisung:</div>")
|
|
html_parts.append(f"<div>{instructions}</div>")
|
|
html_parts.append("</div>")
|
|
|
|
# Haupttext / gedruckte Bloecke
|
|
_build_text_section(html_parts, printed_blocks, canonical_text)
|
|
|
|
# Aufgaben
|
|
_build_tasks_section(html_parts, tasks)
|
|
|
|
# Fusszeile
|
|
html_parts.append("<div class='footer'>")
|
|
html_parts.append("Dieses Arbeitsblatt wurde automatisch aus einem Scan rekonstruiert.")
|
|
html_parts.append("</div>")
|
|
|
|
html_parts.append("</body></html>")
|
|
|
|
return "\n".join(html_parts)
|
|
|
|
|
|
def _build_html_head(title: str) -> str:
|
|
"""Build the HTML head with print-optimized styles."""
|
|
return """<!DOCTYPE html>
|
|
<html lang="de">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>""" + title + """</title>
|
|
<style>
|
|
@page {
|
|
size: A4;
|
|
margin: 20mm;
|
|
}
|
|
@media print {
|
|
body {
|
|
font-size: 14pt !important;
|
|
-webkit-print-color-adjust: exact;
|
|
print-color-adjust: exact;
|
|
}
|
|
.no-print { display: none !important; }
|
|
.page-break { page-break-before: always; }
|
|
}
|
|
* { box-sizing: border-box; }
|
|
body {
|
|
font-family: Arial, "Helvetica Neue", sans-serif;
|
|
max-width: 800px;
|
|
margin: 0 auto;
|
|
padding: 30px;
|
|
line-height: 1.7;
|
|
font-size: 16px;
|
|
color: #000;
|
|
background: #fff;
|
|
}
|
|
h1 {
|
|
font-size: 28px;
|
|
margin: 0 0 8px 0;
|
|
padding-bottom: 8px;
|
|
border-bottom: 3px solid #000;
|
|
}
|
|
h2 {
|
|
font-size: 20px;
|
|
margin: 28px 0 12px 0;
|
|
padding-bottom: 4px;
|
|
border-bottom: 1px solid #666;
|
|
}
|
|
.meta {
|
|
font-size: 14px;
|
|
color: #333;
|
|
margin-bottom: 20px;
|
|
padding: 8px 0;
|
|
}
|
|
.meta span {
|
|
margin-right: 20px;
|
|
}
|
|
.instructions {
|
|
margin: 20px 0;
|
|
padding: 16px;
|
|
border: 2px solid #333;
|
|
background: #f5f5f5;
|
|
font-size: 15px;
|
|
}
|
|
.instructions-label {
|
|
font-weight: bold;
|
|
margin-bottom: 8px;
|
|
}
|
|
.text-section {
|
|
margin: 24px 0;
|
|
}
|
|
.text-block {
|
|
margin-bottom: 16px;
|
|
text-align: justify;
|
|
}
|
|
.text-block-title {
|
|
font-weight: bold;
|
|
font-size: 17px;
|
|
margin-bottom: 8px;
|
|
}
|
|
.task-section {
|
|
margin-top: 32px;
|
|
}
|
|
.task {
|
|
margin-bottom: 24px;
|
|
padding: 16px;
|
|
border: 1px solid #999;
|
|
background: #fafafa;
|
|
}
|
|
.task-header {
|
|
font-weight: bold;
|
|
font-size: 16px;
|
|
margin-bottom: 12px;
|
|
padding-bottom: 8px;
|
|
border-bottom: 1px dashed #666;
|
|
}
|
|
.task-content {
|
|
font-size: 15px;
|
|
}
|
|
.gap-line {
|
|
display: inline-block;
|
|
border-bottom: 2px solid #000;
|
|
min-width: 100px;
|
|
margin: 0 6px;
|
|
}
|
|
.answer-lines {
|
|
margin-top: 16px;
|
|
}
|
|
.answer-line {
|
|
border-bottom: 1px solid #333;
|
|
height: 36px;
|
|
margin-bottom: 4px;
|
|
}
|
|
.footer {
|
|
margin-top: 40px;
|
|
padding-top: 16px;
|
|
border-top: 1px solid #ccc;
|
|
font-size: 11px;
|
|
color: #666;
|
|
text-align: center;
|
|
}
|
|
/* Print Button - versteckt beim Drucken */
|
|
.print-button {
|
|
position: fixed;
|
|
top: 20px;
|
|
right: 20px;
|
|
padding: 12px 24px;
|
|
background: #333;
|
|
color: #fff;
|
|
border: none;
|
|
border-radius: 6px;
|
|
cursor: pointer;
|
|
font-size: 14px;
|
|
}
|
|
.print-button:hover {
|
|
background: #555;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<button class="print-button no-print" onclick="window.print()">Drucken</button>
|
|
"""
|
|
|
|
|
|
def _build_text_section(html_parts: list, printed_blocks: list, canonical_text: str):
|
|
"""Build the text section from printed blocks or canonical text."""
|
|
if printed_blocks:
|
|
html_parts.append("<section class='text-section'>")
|
|
for block in printed_blocks:
|
|
role = (block.get("role") or "body").lower()
|
|
text = (block.get("text") or "").strip()
|
|
if not text:
|
|
continue
|
|
if role == "title":
|
|
html_parts.append(f"<div class='text-block'><div class='text-block-title'>{text}</div></div>")
|
|
else:
|
|
html_parts.append(f"<div class='text-block'>{text}</div>")
|
|
html_parts.append("</section>")
|
|
elif canonical_text:
|
|
html_parts.append("<section class='text-section'>")
|
|
paragraphs = [
|
|
p.strip()
|
|
for p in canonical_text.replace("\r\n", "\n").split("\n\n")
|
|
if p.strip()
|
|
]
|
|
for p in paragraphs:
|
|
html_parts.append(f"<div class='text-block'>{p}</div>")
|
|
html_parts.append("</section>")
|
|
|
|
|
|
def _build_tasks_section(html_parts: list, tasks: list):
|
|
"""Build the tasks section."""
|
|
if not tasks:
|
|
return
|
|
|
|
html_parts.append("<section class='task-section'>")
|
|
html_parts.append("<h2>Aufgaben</h2>")
|
|
|
|
type_labels = {
|
|
"fill_in_blank": "Lueckentext",
|
|
"multiple_choice": "Multiple Choice",
|
|
"free_text": "Freitext",
|
|
"matching": "Zuordnung",
|
|
"labeling": "Beschriftung",
|
|
"calculation": "Rechnung",
|
|
"other": "Aufgabe"
|
|
}
|
|
|
|
for idx, task in enumerate(tasks, start=1):
|
|
t_type = task.get("type") or "Aufgabe"
|
|
desc = task.get("description") or ""
|
|
text_with_gaps = task.get("text_with_gaps")
|
|
|
|
html_parts.append("<div class='task'>")
|
|
|
|
type_label = type_labels.get(t_type, t_type)
|
|
html_parts.append(f"<div class='task-header'>Aufgabe {idx}: {type_label}</div>")
|
|
|
|
if desc:
|
|
html_parts.append(f"<div class='task-content'>{desc}</div>")
|
|
|
|
if text_with_gaps:
|
|
rendered = text_with_gaps.replace("___", "<span class='gap-line'> </span>")
|
|
html_parts.append(f"<div class='task-content' style='margin-top:12px;'>{rendered}</div>")
|
|
|
|
# Antwortlinien fuer Freitext-Aufgaben
|
|
if t_type in ["free_text", "other"] or (not text_with_gaps and not desc):
|
|
html_parts.append("<div class='answer-lines'>")
|
|
for _ in range(3):
|
|
html_parts.append("<div class='answer-line'></div>")
|
|
html_parts.append("</div>")
|
|
|
|
html_parts.append("</div>")
|
|
|
|
html_parts.append("</section>")
|