import re import yaml from conftest import PNG, Book, Doc from epub2md.linkcheck import anchors_in, check_links from epub2md.merge import main as merge_main A = ('

Kapitel A

Siehe B, Kapitel B, ' 'hier und Anmerkung1.

' '

Bild

' '

Bildlink

' '
[kein](link.md) id="sec" `code`
') B = ('

Kapitel B

Zurück zu A, nochmal Anmerkung' '1.

' '') def book() -> Book: return Book(title="Merge-Test", docs=[Doc("a.xhtml", A), Doc("b.xhtml", B)], media={"img/a.png": PNG}) def test_merge_complete_book(convert, tmp_path): run = convert(book()) assert merge_main([str(run.out)]) == 0 target = run.out / f"{run.report['input']['name'][:-5]}.md" text = target.read_text(encoding="utf-8") # one book-level frontmatter, no chapter frontmatter fm = yaml.safe_load(text.split("---\n")[1]) assert fm["title"] == "Merge-Test" and fm["author"] == ["Erika Musterfrau"] and fm["lang"] == "de" assert text.count("chapter_number") == 0 # reading order assert text.index("# Kapitel A") < text.index("# Kapitel B") # links between chapters point into the merged file; colliding id "sec" renamed in chapter B assert '' in text and '' in text assert "[B](#c002-sec)" in text and "[A](#sec)" in text and "[hier](#sec)" in text assert "[Kapitel B](#002-kapitel-b)" in text and '' in text # media paths relative to the merged file, also inside image links assert "![Bild](media/images/0001-bild.png)" in text assert "[![Bildlink](media/images/0001-bild.png)](#002-kapitel-b)" in text # code untouched assert '[kein](link.md) id="sec" `code`' in text # footnote defined in both chapters -> only once in the merged file assert text.count("[^fn-001]:") == 1 assert check_links({target.as_posix(): text}, {p.as_posix() for p in run.out.rglob("*") if p.is_file()}) == [] prose = re.sub(r"^```.*?^```$", "", text, flags=re.M | re.S) ids = re.findall(r'id="([^"]+)"', prose) assert len(ids) == len(set(ids)) def test_merge_refuses_overwrite_without_force(convert): run = convert(book()) assert merge_main([str(run.out)]) == 0 assert merge_main([str(run.out)]) == 1 assert merge_main([str(run.out), "--force"]) == 0 def test_merge_to_other_location_rewrites_media_paths(convert, tmp_path): run = convert(book()) (tmp_path / "ziel").mkdir() target = tmp_path / "ziel" / "buch.md" assert merge_main([str(run.out), "-o", str(target), "--toc", "--no-frontmatter"]) == 0 text = target.read_text(encoding="utf-8") assert not text.startswith("---") assert f"![Bild](../{run.out.name}/media/images/0001-bild.png)" in text assert "1. [Kapitel A](#001-kapitel-a)" in text assert "001-kapitel-a" in anchors_in(text) def test_merge_invalid_directory(tmp_path): assert merge_main([str(tmp_path / "gibt-es-nicht")]) == 1 (tmp_path / "leer").mkdir() assert merge_main([str(tmp_path / "leer")]) == 1