Ebooks and notebooks¶
Package ezmd_converters.ebooks, converter family documents (spec: docs/internal/spec/part2.md section 4).
Markdown and plain text are handled by the text family.
| Converter | Mime types | Status | Engine |
|---|---|---|---|
documents.epub |
application/epub+zip; a zip named .epub or whose first member is the mimetype entry |
Stable | stdlib zipfile + lxml (BSD-3-Clause), default install |
documents.ipynb |
application/x-ipynb+json; JSON that Magika labels ipynb, or a .ipynb file detected as JSON/text |
Stable | stdlib json, the text family's Markdown parser |
Fallback chain: application/epub+zip tries documents.epub, then archives.archive (which lists the zip
when the EPUB converter rejects a file that detection called EPUB).
EPUB 2 and 3¶
What is preserved:
- Reading order from the OPF spine.
linear="no"items (endnotes, answer keys) follow at the end under a "Non-linear content" heading; when they hold only footnote bodies (an endnotes file) the headings are dropped and only the notes remain, so no hollow sections appear. Settext.epub_nonlinear=falseto leave them out. - Chapter headings from the TOC (EPUB 3
nav.xhtmlwithepub:type="toc", else the EPUB 2 NCX). A TOC title is inserted before a chapter only when the chapter does not already start with a heading of the same text, so titles are never duplicated. Chapters with no TOC entry get no synthetic heading (fragmented books with one file per paragraph read as one chapter). Synthetic headings carryattrs.origin = "toc". - Headings, paragraphs, bold/italic/code/strike/super/subscript, links, lists (nested, ordered with
start), tables (colspan/rowspan,<thead>/<th>header rows, captions),<pre>code (withlanguage-*classes), block quotes, definition lists, MathML as an Equation with itsalttext. - Footnotes: EPUB 3
epub:type="noteref"links (androle="doc-noteref") become footnote references that point at Footnote blocks built fromepub:type="footnote|endnote|rearnote"(orrole="doc-footnote") elements, including notes in another file of the book. The note body is removed from the text flow. - Images become Image blocks with their
alttext andfigcaption. The cover (properties="cover-image"or<meta name="cover">) is recorded asmetadata.extra.cover_image, not as an Image block, so it is not a numbered figure, and it is not repeated where a cover page shows it. Image references areimages/<basename>; the bytes (cover included) are written there whenConvertOptions.image_diris set andextract_imagesis on. - Print page numbers from the EPUB 3
page-listnav (or the NCXpageList): a PageBreak at each anchor,provenance.source_page(orpage_labelfor non-numeric labels) on the blocks that follow,metadata.pages= the number of distinct page labels, andmetadata.extra.has_print_pages = true. - Metadata:
dc:title, authors (dc:creatorwith roleautor no role); editors go tometadata.extra.editorsand other roles tometadata.extra.contributors("Name (role)");dc:language(language_source = "declared"),dc:date,dc:description;dc:identifier,dc:publisher, and the EPUB version inmetadata.extra. - Provenance: every block has
path= the chapter's zip path plus the nearest ancestorid(OEBPS/text/chapter3.xhtml#sec2).
Security: the container is read under the archive limits (500 MB total, 10,000 entries, 100:1 ratio per
entry; see archives); a book that breaks them is refused with archive_bomb_suspected.
XHTML, OPF, NCX, and container XML are parsed with lxml with resolve_entities=False, no_network=True,
load_dtd=False, huge_tree=False (no XXE, no entity expansion). Chapters that are not well-formed XML
(HTML entities such as ) are re-parsed with lxml's HTML parser, also without network access.
Scripts, styles, <nav>, and hidden elements (hidden, aria-hidden="true", display:none) are dropped
and counted in removed_hidden_elements.
Jupyter notebooks¶
- Markdown cells go through the text family's Markdown parser (
parse_markdown); footnote ids are made unique per cell. TeX math is recovered afterwards:$...$becomes an inline math span and a paragraph that is only$$...$$becomes an Equation.attachment:images resolve toimages/cell<i>-<name>(written whenimage_diris set). - Code cells become CodeBlocks in the kernel language, with the source verbatim (only a trailing newline
is trimmed) (
metadata.kernelspec.language, thenlanguage_info.name, defaultpython), withattrs.execution_count. - Outputs (
text.notebook_outputs:alldefault,text,off): stream output andtext/plainresults are CodeBlocks fencedoutput(attrs.role = "output",attrs.stream); error tracebacks are ANSI-stripped CodeBlocks fencedoutputwithattrs.role = "error"; withall, rich results prefertext/markdown(Markdown parser), thentext/html(tables such as pandas DataFrames through the XHTML walker), then PNG/JPEG/GIF/SVG images (Image blocks withattrs.role = "output"), thentext/plain. - Each output is capped at
text.notebook_max_output_chars(5,000) characters with a truncation marker and atruncatedwarning (detail.reason = "notebook_output"). - Raw cells become Raw blocks fenced with a language name derived from their
format/raw_mimetype(text/x-rstbecomesrst,text/latexbecomeslatex, unknown becomestext). - Cells tagged
hide-cell/remove-cellare left out only withtext.notebook_honor_tags=true, and the count is reported inremoved_hidden_elements. - Notebooks older than nbformat 4 (or with no
cellslist) are read best-effort withnotebook_invalid. - Provenance:
path=cells[i]orcells[i].outputs[j],source_id= the cell id, andline_start/line_endwithin the cell for code and Markdown.
Options¶
ConvertOptions.extra keys: text.epub_nonlinear (true), text.notebook_outputs (all),
text.notebook_max_output_chars (5000), text.notebook_honor_tags (false), and the archive limits
specialized.archive_max_total / archive_max_entry / archive_max_entries that also bound EPUB containers.
Warnings¶
epub_mimetype_missing (info), drm_protected (error; DRM-encrypted chapters are skipped, and a stub
explains an entirely locked book; font obfuscation in encryption.xml is not DRM), archive_bomb_suspected
(error), missing_resource (a spine chapter is missing), removed_hidden_elements (info),
extraction_empty, notebook_invalid, truncated (notebook output cap), image_too_large (a notebook
image over 20 MB of base64).
Known limitations¶
- The XHTML walker is local to this family until the shared HTML-to-IR module from the web family (P1-T02)
lands; CSS-based hiding beyond inline
display:none/visibility:hiddenis not detected. - EPUB 2 anchor-based footnotes (plain
#fn1links to a short paragraph) are not yet detected; they stay as normal paragraphs and links. - Only the first TOC entry of each file becomes a synthetic heading; deeper TOC entries rely on the chapter's own headings.
- HTML tables whose header row has an empty cell (a pandas index column) get
Table.attrs.header_synthesized = "true"; the renderer names such columns itself. - Notebook
text/latexoutputs and widget outputs are not rendered. Math recovery runs after Markdown parsing, so a backslash escape or an underscore pair inside$...$may already have been consumed. ruff format .also formats```pythonfences inside committedexpected.full.mdgoldens, so the notebook fixture uses ruff-style code; verbatim code is asserted in the unit tests instead.- Notebook detection depends on the extension until core detection maps Magika's
ipynblabel toapplication/x-ipynb+json(requested); until then every notebook also carriesmisnamed_file.
Planned (not in this release)¶
MOBI/AZW/AZW3/KFX/CHM via Calibre ebook-convert (calibre_missing when absent), FB2, LaTeX (Pandoc or
the builtin parser), reStructuredText (docutils), AsciiDoc, Org, Textile, and MediaWiki (Pandoc when
present, markup_partial otherwise), HTML files/MHTML/webarchive (web family module in file mode).