8000 feat(autoindex): extract EPUB books, one record per chapter in reading order by thomas-villani · Pull Request #1159 · xerj-org/xerj · GitHub
[go: up one dir, main page]

Skip to content

feat(autoindex): extract EPUB books, one record per chapter in reading order - #1159

Merged
xerj-org merged 1 commit into
xerj-org:mainfrom
thomas-villani:feat/epub-extractor
Oct 5, 2026
Merged

xerj-org merged 1 commit into
xerj-org:mainfrom
thomas-villani:feat/epub-extractor

Conversation

@thomas-villani
Copy link
Copy Markdown
Contributor

What this changes, and why

Since #1114, autoindex recognizes an .epub but junks it with "unsupported document format (EPUB e-book) … convert it to PDF". Books, manuals and reports often ship as EPUB, and converting to PDF throws away the chapter structure the file already has. This PR adds an extractor (extract/epub.rs) and removes EPUB from the "export it as…" list.

Record shape. One record per chapter, in reading order:

  • Book fields: title, author, language, publisher, date, from the OPF metadata.
  • Chapter fields: chapter (position among the spine's XHTML documents), chapter_title (from the table of contents), part, section and body.
  • Locators: ch{n}-s{j}, or ch{n}.{k}-s{j} when a document is cut into parts.

How it works:

  • Package. META-INF/container.xml leads to the OPF. Its manifest and spine give the reading order. Hrefs are percent-decoded and resolved against the OPF's directory.

  • Chapter text. Each spine XHTML document goes through the HTML extractor's tokenizer, so it gets fix(autoindex): keep HTML table text, and break HTML sections between paragraphs #1154's table rows and paragraph breaks.

  • Chapter titles. The table of contents is the EPUB 3 nav document's toc nav, falling back to the EPUB 2 NCX, then to the document's first heading.

  • Several chapters per document. Project Gutenberg packs 7–15 chapters into one file (Moby-Dick has 135 chapters in 13 files). A document is therefore cut where each table-of-contents entry's anchor (file.xhtml#id) begins, so every chapter's text carries its own title. For this, html::parse now records each element's id (and <a name>) with its offset in the body.

    • Text before the first anchor becomes an untitled part.
    • An entry whose anchor is missing is ignored.
    • A piece that is only its own title, such as a subtitle listed as a separate entry, folds into the next piece with the titles joined: "CHAPTER 100. Leg and Arm. — The Pequod, …".
  • Skipped:

    • the nav document when it also appears in the spine;
    • spine items that are not XHTML;
    • parts listed in META-INF/encryption.xml under any algorithm other than font obfuscation (DRM ciphertext).

    A book with no readable text is junk.

  • Bounds, as in pptx.rs: 32 MiB per part, 256 MiB decompressed per container, 64 MiB of text, and MAX_RECORDS_PER_FILE. Hitting any of them sets truncated.

Wiring, following the PPTX/XLSX/man pattern:

  • Family::Epub (as_str "epub", is_document), sniffed from the mimetype member in zip_container_kind;
  • dispatch in extract/mod.rs;
  • the SourceAndDocs band in both order.rs functions;
  • the full-read family list in estimate::exact_scan_bytes;
  • the whole-file size cap in scan_file.

Evidence

< 8000 pre class="notranslate">cargo test -p xerj-autoindex --lib -> 1240 passed; 0 failed; 2 ignored cargo clippy -p xerj-autoindex --all-targets -- -D warnings -> clean

There are 10 new tests: 9 in extract::epub and one in extract::html for anchor recording. They cover:

  • spine order versus file names;
  • the toc nav versus landmarks;
  • metadata;
  • an EPUB 2 NCX with percent-encoded .. hrefs;
  • DRM versus font obfuscation;
  • junk cases and the title fallback;
  • sectioning and the decompression budget;
  • splitting at anchors: front matter, a missing anchor, duplicate entries, the heading-only fold, and <br/> inside a label.

Real books, run through a temporary probe test that is not committed. All three sniffed as epub, with 0 junk and 0 truncated:

book packaging records parts notes
Moby-Dick (Gutenberg) EPUB 3, nav 858 144 file 3 splits into CHAPTER 8 … CHAPTER 19, each with its own title
Alice (Gutenberg) EPUB 2, NCX 107 16 one part per chapter, titled from the NCX
28-language test book (AZARDI) EPUB 3, NCX 323 31 titles in each script, e.g. 『世界人権宣言』

Checks

  • cargo fmt --all, clippy -D warnings
  • Scoped release build: not run (see below)
  • cargo test -p xerj-autoindex --lib
  • ES-YAML conformance: N/A. This is an autoindex extractor; no engine or wire code changes.
  • YAML case: N/A
  • Docs: a follow-up, as for PPTX/XLSX/man. README and llms.txt format lists, plus a fact-check row, once this merges (only one PR edits llms.txt at a time).
  • Audit: a new extractor plus additive wiring. html::parse gains an anchors field that nothing else reads, and HTML output is unchanged.
  • Operational audit: N/A

Not run: the fat-LTO release build, and an end-to-end xerj autoindex run against a server. The build machine is shared with a long-running job.

Provenance

  • Written by: an AI coding agent (Claude Code, Claude Opus 5.5), run by @thomas-villani, who has reviewed it.
  • Verified: the test counts and clippy above, and the three real books (record and part counts, chapter titles and splits inspected in full dumps), all in a rust:latest Linux container.
  • Assumed (not tested):
    • real DRM-protected books. Only a synthetic encryption.xml was tested; such parts are skipped, never decrypted.
    • fixed-layout or image-only books. They come out as junk, since there is no OCR.
    • EPUBs from other producers (Calibre, InDesign, Pandoc), which may anchor their tables of contents differently. A book whose chapters all end up untitled or uncut would show this.

…g order

Motivation: since xerj-org#1114 autoindex recognizes an .epub but junks it with
"unsupported document format (EPUB e-book) ... convert it to PDF". Books,
manuals and reports ship as EPUB, and converting to PDF throws away the
chapter structure the file already carries.

What it does (extract/epub.rs, new):
- Package: META-INF/container.xml -> the OPF. Its metadata gives `title`,
  `author` (every dc:creator, "; "-joined), `language`, `publisher`,
  `date` (first of each); the manifest and spine give reading order.
  Hrefs are percent-decoded and resolved against the OPF's directory.
- Chapters: each spine XHTML document is read by the HTML extractor's
  tokenizer (so it gets xerj-org#1154's table rows and paragraph breaks).
  `chapter_title` comes from the table of contents: the EPUB 3 nav
  document's `toc` nav, else the EPUB 2 NCX; else the first heading.
- Several chapters per document: Project Gutenberg packs 7-15 chapters
  into one file (Moby-Dick: 135 chapters in 13 files), so a document is
  cut where each toc entry's anchor (file.xhtml#id) begins, and each
  piece gets its own `chapter_title` and a `part` number. Text before the
  first anchor is an untitled part; a missing anchor is ignored; a piece
  that is only its own title (a subtitle listed as its own entry) folds
  into the next with the titles joined ("CHAPTER 100. Leg and Arm. — The
  Pequod, ..."). html::parse now records each element's `id` (and
  `<a name>`) with its body offset for this (`Doc::anchors`).
- Locators: `ch{n}-s{j}`, or `ch{n}.{k}-s{j}` for a document cut into
  parts; `chapter` is the position among the spine's XHTML documents.
- Skipped: the nav document when it is also in the spine, non-XHTML spine
  items, and parts listed in META-INF/encryption.xml under anything but
  font obfuscation (DRM ciphertext). A book with no readable text is junk.
- Bounds as pptx.rs: 32 MiB per part, 256 MiB decompressed per container,
  64 MiB of text, MAX_RECORDS_PER_FILE; hitting any sets `truncated`.

Wiring: Family::Epub (as_str "epub", is_document), sniffed from the
`mimetype` member in zip_container_kind; dispatch in extract/mod.rs;
SourceAndDocs band in both order.rs functions; full-read family in
estimate::exact_scan_bytes; whole-file size cap in scan_file. "epub" is
removed from unsupported_document_advice and its sniff tests.

Evidence (rust:latest container, Linux):
- 10 new tests: 9 in extract::epub (spine order vs file names, nav vs
  landmarks, metadata, EPUB 2 NCX + percent-encoded `..` hrefs, DRM vs
  font obfuscation, junk cases, title fallback, sectioning, decompression
  budget, splitting at anchors with front matter / missing anchor /
  duplicate entries / heading-only fold / <br/> in a label) and
  anchors_record_ids_and_a_names_where_their_text_begins in extract::html.
- cargo test -p xerj-autoindex --lib: 1240 passed, 0 failed, 2 ignored.
- cargo clippy -p xerj-autoindex --all-targets -- -D warnings: clean.
- Three real books via a temporary probe test (not committed), all
  sniffed as epub, 0 junk, 0 truncated: Gutenberg Moby-Dick (EPUB 3, nav)
  -> 858 records in 144 parts, file 3 split into CHAPTER 8 ... CHAPTER 19
  with their own titles; Gutenberg Alice (EPUB 2, NCX) -> 107 records,
  one part per chapter; an EPUB 3 test book in 28 languages -> 323
  records with titles in each script.

Not covered: DRM-protected books (only synthetic encryption.xml tested;
they are skipped, not decrypted); fixed-layout or image-only books
(junk, no OCR); no end-to-end `xerj autoindex` run against a server.

Written by an AI coding agent (Claude Code, Claude Opus 5.5), run and
reviewed by Tom Villani (@thomas-villani).

Files: engine/crates/xerj-autoindex/src/extract/epub.rs (new),
extract/html.rs, extract/mod.rs, sniff.rs, order.rs, estimate.rs, lib.rs
@cla-bot cla-bot Bot added the cla-signed label Oct 5, 2026
@xerj-org
xerj-org merged commit 1959dff into xerj-org:main Oct 5, 2026
22 checks passed
Ravandevil25 pushed a commit to Ravandevil25/xerj that referenced this pull request Oct 6, 2026
- engine 1.0.0-rc.82 -> 1.0.0-rc.83 (Cargo.toml + cargo update -w, 17 lock entries)
- landing: data-latest-tag + all 91 footer brand spans -> RC.83 (the xerj-org#1155 guard now enforces this at cut)
- CHANGELOG: rc.83 section — xerj-org#1155 (version-roll guard + xxhash 0.8.19), xerj-org#1153 (xerj-org#1145/xerj-org#1148), xerj-org#1156 (xerj-org#1136), xerj-org#1154, xerj-org#1159 (EPUB), xerj-org#1160 (redundant-overlap section), xerj-org#1161 via xerj-org#1162 (notebooks + data-file naming)
- ROADMAP: cut-roll — shortlist re-verified live: 8 open (xerj-org#1091 xerj-org#1092 xerj-org#1094 xerj-org#1100 xerj-org#1146 xerj-org#1147 xerj-org#1158 xerj-org#1122); xerj-org#1136/xerj-org#1145/xerj-org#1148 closed in-window, de-linked here and moved to CHANGELOG; rc.83 paragraph in the GA window record
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

0