This commit is contained in:
team3
2026-07-13 06:07:09 +02:00
parent 8926f87185
commit 32d7ad9ea1
15 changed files with 552 additions and 115 deletions

View File

@@ -83,8 +83,25 @@ def _snapshot_schreiben(topic: str, inhalt: str) -> tuple[str, str] | None:
return str(pfad), h
# LaTeX-Quellen: Umlaut-Escapes am Import auflösen — Modelle zitieren „ü", nie \"u,
# sonst scheitert der verbatim-Anker. Mathe und Makros bleiben unangetastet.
_TEX_UMLAUT = re.compile(r'\\"\{?([AOUaou])\}?')
_TEX_UMLAUTE = {"a": "ä", "o": "ö", "u": "ü", "A": "Ä", "O": "Ö", "U": "Ü"}
# \ss ist ein Kontrollwort und frisst das Folge-Leerzeichen: hei\ss t → heißt
_TEX_SZ = re.compile(r"\\ss(?![a-zA-Z])(?:\{\})?[ \t]?")
def _tex_normalisieren(inhalt: str) -> str:
_, sep, rumpf = inhalt.partition(r"\begin{document}")
if sep:
inhalt = rumpf
inhalt = inhalt.replace(r"\end{document}", "")
inhalt = _TEX_UMLAUT.sub(lambda m: _TEX_UMLAUTE[m.group(1)], inhalt)
return _TEX_SZ.sub("ß", inhalt)
def _datei_lesen(pfad: Path) -> str:
"""txt/md direkt; PDF via pdftotext (treu, strukturarm — Lektion 38)."""
"""txt/md direkt; tex normalisiert; PDF via pdftotext (treu, strukturarm — Lektion 38)."""
if pfad.suffix.lower() == ".pdf":
if shutil.which("pdftotext") is None:
log.warning("pdftotext fehlt — %s übersprungen", pfad.name)
@@ -93,9 +110,10 @@ def _datei_lesen(pfad: Path) -> str:
capture_output=True, text=True, timeout=120)
return res.stdout if res.returncode == 0 else ""
try:
return pfad.read_text(encoding="utf-8", errors="replace")
text = pfad.read_text(encoding="utf-8", errors="replace")
except OSError:
return ""
return _tex_normalisieren(text) if pfad.suffix.lower() == ".tex" else text
async def _uni_quellen(ctx: llm.Kontext) -> int:
@@ -103,10 +121,14 @@ async def _uni_quellen(ctx: llm.Kontext) -> int:
ordner = TOPICS_DIR / ctx.topic
neu = 0
for pfad in sorted(ordner.glob("*")) if ordner.is_dir() else []:
if pfad.suffix.lower() not in (".txt", ".md", ".pdf"):
suffix = pfad.suffix.lower()
if suffix not in (".tex", ".txt", ".md", ".pdf"):
continue
# .pdf überspringen, wenn eine gleichnamige .txt daneben liegt (vorkonvertiert)
if pfad.suffix.lower() == ".pdf" and pfad.with_suffix(".txt").exists():
# Vorrang bei gleichem Basename: tex > txt > pdf (treueste Fassung gewinnt)
if suffix == ".pdf" and (pfad.with_suffix(".tex").exists()
or pfad.with_suffix(".txt").exists()):
continue
if suffix == ".txt" and pfad.with_suffix(".tex").exists():
continue
inhalt = _datei_lesen(pfad)
if not inhalt.strip():