[bug] Preferred ebooklib parser ignores EPUB spine and reorders chapters
What happened
With ebooklib and beautifulsoup4 installed, an EPUB whose manifest order differs from its spine is extracted in manifest order. The same file through the stdlib parser is extracted in the correct spine order.
I reproduced this with real ebooklib 0.20 and an EPUB created by epub.write_epub(), then ran the unmodified CLI. The revised fixture includes the EPUB 3 navigation document. It is not a mocked import; no EPUBCheck conformance result is claimed:
Manifest document order: c2, c1, nav
Declared spine: c1, c2
ebooklib extraction: Chapter 2 / SECOND ... / Chapter 1 / FIRST ...
stdlib extraction: Chapter 1 / FIRST ... / Chapter 2 / SECOND ...
CLI exit code: 0
sources[0].extraction_method: ebooklib
chapters_detected: 2
full_text.txt: SECOND occurs before FIRSTInstalling the preferred dependency therefore changes correct reading order into incorrect reading order, while the chapter count still looks healthy.
What you expected
Both extractors should emit document content in the declared spine order. Non-spine content documents should follow it, consistent with the existing stdlib safety-net policy.
Source document
- Format: EPUB; entirely synthetic English content.
- Pages / size: 2 spine items and a non-spine navigation document; small synthetic archive, no images.
--check: EPUB reports ready with bothebooklibandbeautifulsoup4installed.
Repro
In a checkout with ebooklib and beautifulsoup4 available, run:
from ebooklib import epub
from book_to_skill.parsers.epub import extract_with_ebooklib, extract_with_zipfile
book = epub.EpubBook()
book.set_identifier("synthetic-spine-order")
book.set_title("Synthetic ordering fixture")
book.set_language("en")
first = epub.EpubHtml(title="Chapter 1", file_name="z-first.xhtml", uid="c1")
first.content = "<h1>Chapter 1</h1><p>FIRST: Define the calibration procedure.</p>"
second = epub.EpubHtml(title="Chapter 2", file_name="a-second.xhtml", uid="c2")
second.content = "<h1>Chapter 2</h1><p>SECOND: Apply the procedure defined earlier.</p>"
book.add_item(second) # manifest insertion order is not reading order
book.add_item(first)
book.add_item(epub.EpubNcx())
book.add_item(epub.EpubNav()) # EPUB 3 navigation document; not a spine chapter
book.spine = [first, second]
book.toc = [first, second]
epub.write_epub("spine-order.epub", book)
preferred = extract_with_ebooklib("spine-order.epub")
fallback = extract_with_zipfile("spine-order.epub")
print("preferred:", preferred.index("FIRST:"), preferred.index("SECOND:"))
print("fallback:", fallback.index("FIRST:"), fallback.index("SECOND:"))
assert fallback.index("FIRST:") < fallback.index("SECOND:")
assert preferred.index("FIRST:") < preferred.index("SECOND:")Observed: the assertion fails (92 < 20 is false). The fallback assertion passes.
The public CLI also selects the affected branch:
python scripts/extract.py spine-order.epub --mode text --install-missing noIts emitted full_text.txt begins with Chapter 2, despite a Chapter 1-first spine.
Cause and downstream impact
At epub.py:46-51, extract_with_ebooklib() iterates book.get_items_of_type(ITEM_DOCUMENT) without resolving book.spine. In this real fixture, that API yields c2, c1 in manifest order. utils.py:1063-1065 accepts that nonempty result, so the corrected stdlib parser never runs.
This affects more than display: Step 3 reads the beginning of full_text.txt to discover structure, and Step 7 reads chapter slices from that same corpus. A reversed book changes the front-matter/initial sample and prerequisite order, and a manifest-only nav document can appear between chapters. Those are risks to generation; I did not run an LLM conversion or measure a generated-answer error rate.
Relationship to previous fixes
#64 explicitly fixed only the stdlib extractor and left the ebooklib path untouched. #248/#249 fixed namespace-prefixed OPF handling in that same fallback. This report concerns the preferred, dependency-enabled branch and reproduces with ordinary unprefixed OPF, so those fixes do not cover it.
Smallest correction and acceptance plan
Resolve book.spine idrefs to document items, emit them in order with a seen set, then append remaining document items in their existing order. Keep the current best-effort handling and the stdlib fallback when the optional imports are absent; avoid changing the public parser return type or generation spec.
Regression coverage should include:
- A real two-document EPUB with manifest order opposite to spine order; test both parsers and CLI output.
- A non-spine nav/content item between chapters in the manifest; retain it after the spine content.
- Repeated or unresolved idrefs and a missing spine; document the tolerant fallback and avoid duplicated/lost content.
- Dependency-present and dependency-absent paths. Existing
TestEpubSpineOrderexercises only the stdlib function and cannot catch this discrepancy.
Environment and validation limits
- Windows 11; Python 3.12.14.
ebooklib0.20,beautifulsoup44.14.3.masteratc108d25b0cb58e1bdc361f3de02ed9f37075152f;pyproject.tomldeclares 1.4.0.- Unmodified full CLI reproduced the failure; a new expected-order regression fails.
- Existing EPUB-spine and DOCX-table focused tests: 17 passed on Python 3.14.0; the two new expected-order tests fail there. The complete CLI was separately reproduced on Python 3.12.14. No repository code changed. No full-suite or live-model result is claimed here.
Reproduction precision update
The initial minimal example omitted EpubNav() while ebooklib defaulted to EPUB 3.0. That example showed the ordering bug, but my original wording that it was a valid EPUB was too strong. The reproduction above now includes the navigation document. A separate rerun checks that the OPF contains properties="nav", that its referenced archive entry exists, and that the chapter spine remains c1, c2. Reordering still occurs in both the preferred parser and unmodified CLI. This narrows the evidence accurately; a successful library read is not a formal EPUBCheck certificate.
Source: virgiliojr94/book-to-skill