epub2md/tools/qa/pyblocks.py
dschlueter 01f60a41c4 chore: QA-Skripte nach tools/qa übernommen
Die Werkzeuge der QA-Runden lagen nur im ignorierten .qa/: CLI-Matrix
(89 Fälle), Fixture-Generator, unabhängiger Markdown-Validator,
Wortvergleich gegen Datenverlust, Pandoc-Prüfung (HTML, EPUB, PDF).
Jetzt versioniert in tools/qa/ mit README und requirements.txt; erzeugte
Daten landen weiterhin in .qa/. Pfade auf die neue Lage angepasst
(Repo-Wurzel über parents[2], aufrufender Python-Interpreter).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 22:14:54 +02:00

36 lines
1.1 KiB
Python
Executable file

"""Listet Python-Codeblöcke mit Einrückungsfehlern in Markdown-Dateien (mit Inhalt, zum Vergleich mit der Quelle).
Aufruf: python tools/qa/pyblocks.py AUSGABE/chapters/*.md
"""
from __future__ import annotations
import ast
import re
import sys
def main() -> None:
for path in sys.argv[1:]:
with open(path, encoding="utf-8") as fh:
lines = fh.read().split("\n")
i = 0
while i < len(lines):
m = re.match(r"^(`{3,})python\s*$", lines[i])
if m:
j = i + 1
while j < len(lines) and not lines[j].startswith(m.group(1)):
j += 1
try:
ast.parse("\n".join(lines[i + 1:j]))
except IndentationError as exc:
print(f"== {path}:{i + 1} {exc.msg} (Blockzeile {exc.lineno})")
print("\n".join(f"{k + 1:3}|{line}" for k, line in enumerate(lines[i + 1:j])))
except SyntaxError:
pass # fragments and program output are no indentation defects
i = j
i += 1
if __name__ == "__main__":
main()