Skip to content

Fix EPUB conversion with namespace prefixes - #2480

Open
Gali (Jokasa7) wants to merge 1 commit into
microsoft:mainfrom
Jokasa7:JoyCx/fix-epub-namespace-prefixes
Open

Fix EPUB conversion with namespace prefixes#2480
Gali (Jokasa7) wants to merge 1 commit into
microsoft:mainfrom
Jokasa7:JoyCx/fix-epub-namespace-prefixes

Conversation

@Jokasa7

Copy link
Copy Markdown

EPUB conversion currently depends on XML prefix spelling. With ocf:rootfile in container.xml, conversion raises IndexError (wrapped in FileConversionException by MarkItDown.convert_stream). With prefixed opf:item / opf:itemref, conversion succeeds but silently omits every chapter. Binding Dublin Core to a different prefix drops the book metadata.

Match structural elements by their namespace URI and local name, retaining compatibility with unqualified structural elements. Match Dublin Core metadata by its URI while preserving the existing text helpers. Elements in unrelated namespaces cannot replace a rootfile, override a manifest entry, duplicate a chapter, or supply book metadata.

The EPUB specification defines the OCF and package namespaces, and XML Namespaces permits equivalent namespace prefixes. This change is separate from #1728, which addresses relative paths and prefixes inside chapter XHTML. Chapter HTML conversion and href resolution are unchanged here.

Validation:

  • New prefix regressions against the original implementation: 8 failed; the existing four tests and two default-prefix controls passed.
  • EPUB tests, including the existing metadata/kwargs regressions: 20 passed. Cases cover direct conversion and MarkItDown.convert_stream, independent OCF/OPF/DC aliases, default-namespace Dublin Core, multiple authors, reversed spine order, foreign-namespace collisions, and legacy unqualified structural elements.
  • pre-commit run --all-files: passed.
  • Full hatch test on Windows / Python 3.12: 912 passed, 5 skipped, 1 failed. The only failure is the existing speech-transcription test because ffprobe is not installed (AudioConverter raises FileNotFoundError). The same test fails identically when importing an untouched git archive HEAD copy of all runtime sources in the same Hatch environment. No tests were disabled or modified to suppress it.

The tests build minimal in-memory EPUB fixtures; they do not claim EPUBCheck validation of a complete publication. The fix and tests were generated and independently reviewed with Codex. Validation uses real ZIP/XML/HTML conversion without model services.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant