import yaml from conftest import Book, Doc def frontmatter(text: str) -> dict: assert text.startswith("---\n") return yaml.safe_load(text.split("---\n")[1]) def test_files_are_numbered_and_slugged(convert): docs = [Doc("c1.xhtml", "
a
"), Doc("c2.xhtml", "b
")] run = convert(Book(docs=docs)) assert [p.name for p in run.chapters()] == ["001-uebersicht-groessen.md", "002-uebersicht-groessen.md"] run_en = convert(Book(docs=docs), "--slug-language", "en") assert run_en.chapters()[0].name == "001-ubersicht-grossen.md" run_none = convert(Book(docs=docs), "--slug-language", "none") assert run_none.chapters()[0].name == "001-übersicht-größen.md" def test_h1_split_inside_one_file(convert): body = "a
b
c
" run = convert(Book(docs=[Doc("all.xhtml", body)])) assert [p.name for p in run.chapters()] == ["001-eins.md", "002-zwei.md"] assert "## Eins.Eins" in run.chapter("001") def test_h2_split_with_chapter_level_2(convert): body = "Intro
a
b
" run = convert(Book(docs=[Doc("all.xhtml", body)]), "--chapter-level", "2") names = [p.name for p in run.chapters()] assert names == ["001-teil-i.md", "002-kapitel-1.md", "003-kapitel-2.md"] assert run.chapter("002").count("\n# ") == 1 # exactly one H1 per file def test_section_files_without_h1_continue_previous_chapter(convert): docs = [ Doc("01.xhtml", "a
"), Doc("01b.xhtml", 'b
c
"), ] run = convert(Book(docs=docs)) assert len(run.chapters()) == 2 first = run.chapter("001") assert "## 1.1 Abschnitt" in first assert frontmatter(first)["source_hrefs"] == ["01.xhtml", "01b.xhtml"] def test_headingless_documents_become_own_chapters_with_nav_title(convert): docs = [Doc("titel.xhtml", "Widmung für alle
"), Doc("c.xhtml", "x
")] book = Book(docs=docs, nav=[("Widmung", "titel.xhtml"), ("Start", "c.xhtml")]) run = convert(book) assert [p.name for p in run.chapters()] == ["001-widmung.md", "002-start.md"] fm = frontmatter(run.chapter("001")) assert fm["chapter_title"] == "Widmung" assert "dedication" in fm["epub_type"] def test_semantic_sections_are_split_and_classified(convert): body = ( 'Text
A
Knuth
Text
a
"), Doc("b.xhtml", 'b
')] run = convert(Book(docs=docs, nav=[("A", "a.xhtml"), ("B", "b.xhtml")])) assert [p.name for p in run.chapters()] == ["001-a.md", "002-b.md"] assert run.report["skipped_documents"] == [{"href": "OEBPS/toc.xhtml", "reason": "html-toc"}] def test_short_title_page_is_kept(convert): docs = [Doc("t.xhtml", "Für M.
", "Widmung"), Doc("c.xhtml", "x
")] run = convert(Book(docs=docs, nav=[])) assert len(run.chapters()) == 2 assert "Für M." in run.chapter("001") def test_layout_table_headings_are_recognized(convert): body = ( '| 3 | Grundlagen |
Text
" '| 3.1 | Einführung |
mehr
' ) run = convert(Book(docs=[Doc("c.xhtml", body)])) text = run.chapter("001") assert "# 3 Grundlagen" in text assert "## 3.1 Einführung" in text assert "