epub2md/tests/test_pandoc.py
dschlueter 64d0cb0357 fix: Pandoc-Weg – EPUB-Links, Formeln im EPUB, portables TeX (QA3-04 bis QA3-06)
Prüfung aller 5 Beispielbücher über die Befehle der Buch-README (HTML,
EPUB, PDF mit weasyprint und xelatex):

- QA3-04: Der EPUB-Befehl der Buch-README hatte kein --mathml; im
  Mathebuch blieben 1072 Formeln als TeX-Text. Jetzt 4904 als MathML.
- QA3-05: Kapitel-Startanker von epub2md_merge.py begannen mit einer
  Ziffer (039-…). Pandocs EPUB-Writer löst Links darauf nicht auf (C-Buch
  32, Mathebuch 20 tote Links). Jetzt kap-039-…; book.lua erkennt beide.
- QA3-06: portable_tex() passt Verlags-TeX für texmath an (\"u, \O,
  @{…} und \arraycolsep in array, leeres \displaystyle vor Zellenende).
  Mathebuch: 1 statt 9 nicht umsetzbare Formeln.
- Cover ohne Seitenzahl (print.css, book.lua).

Neue Tests: tests/test_pandoc.py (nur mit installiertem pandoc),
Parametertests für portable_tex. Andere Bücher byte-identisch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 21:41:13 +02:00

60 lines
2.5 KiB
Python

"""Pandoc path: merged file → EPUB/HTML with the filters from pandoc/ (skipped without pandoc)."""
import re
import shutil
import subprocess
import zipfile
from pathlib import Path
import pytest
from conftest import Book, Doc
from epub2md.merge import main as merge_main
pytestmark = pytest.mark.skipif(shutil.which("pandoc") is None, reason="pandoc nicht installiert")
FILTERS = Path(__file__).resolve().parent.parent / "pandoc"
def merged(convert, book: Book) -> Path:
run = convert(book)
assert merge_main([str(run.out)]) == 0
return run.out / f"{run.report['input']['name'][:-5]}.md"
def pandoc(md: Path, *args: str) -> subprocess.CompletedProcess:
return subprocess.run(["pandoc", md.name, *args], cwd=md.parent, capture_output=True, text=True, check=True)
def links_and_ids(epub: Path) -> tuple[list[tuple[str, str]], dict[str, set[str]]]:
with zipfile.ZipFile(epub) as z:
docs = {n: z.read(n).decode("utf-8") for n in z.namelist() if n.endswith(".xhtml")}
ids = {n: set(re.findall(r'\bid="([^"]+)"', t)) for n, t in docs.items()}
links = [(n, h) for n, t in docs.items() for h in re.findall(r'href="([^"]+)"', t)
if not re.match(r"^[a-z]+:", h) and not h.endswith(".css")]
return links, ids
def test_epub_links_to_chapter_starts_resolve(convert):
"""Chapter start anchors must begin with a letter: Pandoc's EPUB writer breaks '#039-…' (QA3-05)."""
docs = [Doc("a.xhtml", '<h1>Kapitel A</h1><p>Siehe <a href="b.xhtml">Kapitel B</a> und <a href="c.xhtml">Bild</a>.</p>'),
Doc("b.xhtml", "<h1>Kapitel B</h1><p>Text B.</p>"),
Doc("c.xhtml", "<p>Nur eine Bildseite ohne Überschrift.</p>")]
md = merged(convert, Book(docs=docs))
pandoc(md, "--toc", "-o", "buch.epub")
links, ids = links_and_ids(md.parent / "buch.epub")
broken = []
for doc, href in links:
file, _, frag = href.partition("#")
target = str(Path(doc).parent / file).replace("\\", "/") if file else doc
target = re.sub(r"[^/]+/\.\./", "", target)
if target not in ids or (frag and frag not in ids[target]):
broken.append(f"{doc}: {href}")
assert not broken
def test_book_filter_turns_chapter_anchors_into_chapter_starts(convert):
docs = [Doc("a.xhtml", "<h1>Kapitel A</h1><p>a</p>"), Doc("b.xhtml", "<h1>Kapitel B</h1><p>b</p>")]
md = merged(convert, Book(docs=docs))
html = pandoc(md, "-s", "--lua-filter", str(FILTERS / "book.lua"), "-t", "html").stdout
assert html.count('class="chapter-start"') == 2
assert 'id="TOC"' in html and "Kapitel B" in html