Repository navigation
feat(autoindex): extract EPUB books, one record per chapter in reading order - #1159
Merged
Merged
Conversation
…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
Merged
4 tasks done
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this changes, and why
Since #1114, autoindex recognizes an
.epubbut 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:
title,author,language,publisher,date, from the OPF metadata.chapter(position among the spine's XHTML documents),chapter_title(from the table of contents),part,sectionandbody.ch{n}-s{j}, orch{n}.{k}-s{j}when a document is cut into parts.How it works:
Package.
META-INF/container.xmlleads 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
tocnav, 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::parsenow records each element'sid(and<a name>) with its offset in the body.Skipped:
META-INF/encryption.xmlunder 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, andMAX_RECORDS_PER_FILE. Hitting any of them setstruncated.Wiring, following the PPTX/XLSX/man pattern:
Family::Epub(as_str"epub",is_document), sniffed from themimetypemember inzip_container_kind;extract/mod.rs;SourceAndDocsband in bothorder.rsfunctions;estimate::exact_scan_bytes;scan_file.Evidence
cargo test -p xerj-autoindex --lib -> 1240 passed; 0 failed; 2 ignored cargo clippy -p xerj-autoindex --all-targets -- -D warnings -> cleanThere are 10 new tests: 9 in
extract::epuband one inextract::htmlfor anchor recording. They cover:tocnav versuslandmarks;..hrefs;<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:Checks
cargo fmt --all, clippy-D warningscargo test -p xerj-autoindex --libllms.txtformat lists, plus a fact-check row, once this merges (only one PR editsllms.txtat a time).html::parsegains ananchorsfield that nothing else reads, and HTML output is unchanged.Not run: the fat-LTO release build, and an end-to-end
xerj autoindexrun against a server. The build machine is shared with a long-running job.Provenance
rust:latestLinux container.encryption.xmlwas tested; such parts are skipped, never decrypted.