"""Regression tests for the remaining findings of the independent QA (QA2-09 … QA2-19).""" import re from conftest import PNG, SVG, Book, Doc M = 'xmlns="http://www.w3.org/1998/Math/MathML"' def html_table(text: str) -> str: return text[text.index("")] def test_qa2_09_math_and_notes_in_html_tables_are_html(convert): body = ('

T

' f'' '
Kopf
ab$$x^2$$' '1
' '') for mode in ("auto", "tex"): run = convert(Book(docs=[Doc("c.xhtml", body)], media={"f.png": PNG}), "--math", mode, "--footnotes", "inline") table = html_table(run.chapter("001")) visible = re.sub(r'"[^"]*"', '""', table) # ignore attribute values (alt texts) assert "1' in table assert '' in run.chapter("001") assert run.report["output_link_check"]["broken_count"] == 0 def test_qa2_10_book_readme_recommends_pdf_via_html(convert): """Pandoc drops raw HTML (tables, ) when going Markdown -> LaTeX; the HTML route keeps both.""" run = convert(Book(docs=[Doc("a.xhtml", "

A

x

")])) readme = (run.out / "README.md").read_text(encoding="utf-8") assert re.search(r"pandoc \S+\.md -s -o \S+\.html && pandoc \S+\.html --toc --pdf-engine=xelatex -o \S+\.pdf", readme) def test_qa2_11_code_language_hint(convert): py = "def f(x):\n return x\n\nclass A:\n pass" body = "

C

" + "".join(f"
{py}\n# {i}
print({i})
" for i in range(10)) run = convert(Book(docs=[Doc("c.xhtml", body)])) hints = [i for i in run.report["infos"] if i["code"] == "code-language-hint"] assert hints and "--code-language python" in hints[0]["message"] def test_qa2_12_strict_aborts_on_undecodable_xhtml(convert, tmp_path): import zipfile from conftest import build_epub path = build_epub(tmp_path / "g.epub", Book(docs=[Doc("a.xhtml", "

A

x

")])) with zipfile.ZipFile(path) as z: entries = {n: z.read(n) for n in z.namelist()} entries["OEBPS/a.xhtml"] = b'

\xff\xfe\x00\x81 kaputt

' with zipfile.ZipFile(path, "w") as z: for n, d in entries.items(): z.writestr(n, d) assert convert(path).rc == 0 assert convert(path, "--strict").rc == 3 def test_qa2_13_suspicious_media_is_skipped_not_fatal(convert, tmp_path): import zipfile from conftest import build_epub path = build_epub(tmp_path / "b.epub", Book(docs=[Doc("a.xhtml", '

A

Riesig Text

')], media={"big.png": PNG})) with zipfile.ZipFile(path) as z: entries = [(i, z.read(i)) for i in z.infolist()] with zipfile.ZipFile(path, "w", compression=zipfile.ZIP_DEFLATED) as z: for info, data in entries: z.writestr(info.filename, b"\0" * 60_000_000 if info.filename == "OEBPS/big.png" else data) run = convert(path) assert run.rc == 0 assert "Riesig" in run.chapter("001") and "big.png" not in run.chapter("001") assert any(e["code"] == "media-skipped" for e in run.report["errors"]) assert run.report["output_link_check"]["broken_count"] == 0 def test_qa2_14_formula_class_on_img_itself(convert): body = '

F

Wert Summe a plus b ist.

' run = convert(Book(docs=[Doc("c.xhtml", body)], media={"f.png": PNG})) assert [f["origin"] for f in run.report["formulas"]["items"]] == ["image"] assert any(w["code"] == "formula-image" for w in run.report["warnings"]) def test_qa2_15_diagnostics_count_repeats_and_html_table_formulas(convert): img = 'Summe' body = (f"

F

{img} und {img}

" f'
x
ab
') run = convert(Book(docs=[Doc("c.xhtml", body)], media={"f.png": PNG})) w = [w for w in run.report["warnings"] if w["code"] == "formula-image"] assert len(w) == 1 and w[0]["count"] == 2 assert run.report["formulas"]["count"] == 3 def test_qa2_16_non_ascii_ids_are_kept(convert): body = '

Über

Sommer

S Ü

' text = convert(Book(docs=[Doc("c.xhtml", body)])).chapter("001") assert '' in text and '' in text assert "[S](#été)" in text def test_qa2_17_no_empty_fragment_links_and_no_epub_type_attr(convert): body = ('

K

Text1

' '
1
ab
' '') text = convert(Book(docs=[Doc("c.xhtml", body)]), "--footnotes", "keep-links").chapter("001") assert "](#)" not in text assert ' type="noteref"' not in text def test_qa2_18_undefined_entities_are_reported(convert): body = "

E

A &unbekannt; B &auch;

" run = convert(Book(docs=[Doc("c.xhtml", body)])) w = [w for w in run.report["warnings"] if w["code"] == "undefined-entity"] assert w and "unbekannt" in w[0]["message"] and "auch" in w[0]["message"] def test_qa2_19_adjacent_placeholders_without_media(convert): body = '

B

EinsZwei

' text = convert(Book(docs=[Doc("c.xhtml", body)], media={"a.png": PNG, "b.svg": SVG}), "--no-extract-media").chapter("001") assert "**" not in text assert re.search(r"Bild: Eins.*Bild: Zwei", text)