Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

mbook-converter

Cratembook-converter
API documentationdocs.rs/mbook-converter
Sourcecrates/mbook-converter
Builds onmbook; RFC 0001, format version 1.0
Minimum Rust1.89; 1.98 with the export feature
StatusDraft: the API may still change

mbook-converter imports books into mbook and exports existing books to other formats. Its central type, Converter, has four import entry points:

Result: an mbook file written to any WriteResult: a Book in memory
Source: any Readconvertload
Source: a byte sliceconvert_sliceload_slice

Each import format has a Cargo feature enabled by default. The optional export feature enables all writers. A format is read by an established crate (unpdf for PDF, rbook for EPUB, and so on); this crate adds what is a rule of mbook: which chapter a piece of text belongs to, what a link or a picture is called after conversion, what the Markdown dialect allows, and that every picture becomes AVIF.

What is implemented lists, format by format, what the crate does not do yet.

[dependencies]
mbook-converter = "0.3"
PageWhat it answers
GuideHow is a book converted, corrected and exported? Each task with its code.
API referenceWhich types, features, options and errors exist, and what each export format keeps.
Conversion rulesWhat happens to chapters, links, pictures and metadata, format by format, and what each format cannot do yet.
DesignWhich crates read each format, what the design costs, and why it looks this way.

How a conversion runs

flowchart LR
    src["Source<br/>any Read,<br/>or a byte slice"] --> det["Detect<br/>the format"]
    det --> fe["Front end<br/>one established<br/>crate per format"]
    fe --> draft["Draft<br/>metadata, Markdown,<br/>pictures, outline"]
    draft --> asm["Assembler<br/>chapters, paths, links,<br/>dialect, AVIF"]
    asm --> book["Book<br/>of mbook"]
    book --> out["Any Write<br/>an .mbook file"]
  1. The format is detected from the content, unless the caller names it.
  2. The front end of that format parses the source with its crate and hands over a draft: metadata, Markdown in source order with the source's own names in links and pictures, the pictures, and an outline when there is one.
  3. The assembler is the same for every format. It cuts the text into chapters, gives chapters and pictures their paths, re-targets links, brings the Markdown into the dialect of RFC 0001, and encodes pictures as AVIF.
  4. The result is a Book of mbook. convert writes it; load returns it.

What it is for

RFC 0001 describes a packer that starts from FB2, EPUB or a folder, and mbook leaves conversion to "crates built on this one". Without such a crate every application that imports books writes the same code: parse the source, produce Markdown without raw HTML, re-encode pictures, rename every reference, build the contents.

TaskWho does it
Convert a file on diskA command-line tool, a library manager
Convert an upload or a download without a temporary fileA server, a reading application
Convert, then correct the metadata before savingA library manager
Ship only the formats an application needsAn embedded or mobile reader

Goals

GoalHow the design meets it
No file paths in the APISources are Read or &[u8]; the result goes to Write or stays a Book.
A sink that cannot seek is enoughThe file is written in one forward pass by Book::write.
Pay only for the formats usedOne feature per format; a disabled format costs no dependency.
The rules of mbook are written onceEvery front end hands over the same draft; one assembler builds the book.
A converted book is always validThe book goes through Book::check before a byte is written.
Losses are visibleWhatever cannot be represented is reported as a Warning, not dropped silently.
Parsers are not written hereEach format is read by an established crate.

Non-goals

  • Keeping the layout. Fonts, colours, CSS, page geometry and fixed positions are dropped; RFC 0001 excludes them from the format.
  • Removing protection. A protected source is refused.
  • Recognising text in pictures. The pages of a scanned PDF are stored as pictures.
  • Fetching anything. A picture or a stylesheet named by a network address is never downloaded.
  • Asynchronous I/O, as in mbook.