mbook
Read, edit and create mbook files: books a quarter of the size that open in a tenth of a millisecond.
An mbook file holds a book as Markdown chapters and AVIF pictures in one indexed container, compressed with OpenZL. Measured on one illustrated manual, as described in the format's RFC:
- 4× smaller. A 7.0 MB EPUB became a 1.6 MB mbook file.
- 80 µs to open. Title, contents and the list of every resource come from five small typed tables: no XML, no JSON, nothing to unpack.
- 1 GB/s. Each chapter is its own frame and decodes on its own; the longest took 0.1 ms.
- No web engine. Chapters are GitHub Flavored Markdown without raw HTML; a reader is a Markdown view and an AVIF decoder.
- Safe Rust. This crate and its container,
mopenzl, forbidunsafe, and every length read from a file is bounded before memory is allocated.
Guide · API reference · Design · docs.rs · File format
Install
Requires Rust 1.89 or later.
The two types
BookReader<R> |
Book |
|
|---|---|---|
| What it is | A lazy, read-only view of a file | A whole book in memory that can be changed |
| Holds | The tables and the source | Every chapter, picture, diagram and formula, decoded |
| Memory | Kilobytes for a novel | The decoded size of the book |
| Addresses a resource by | A typed id: ChapterId, PictureId, DiagramId, FormulaId |
Its path |
| Typical user | A reading application | A converter or an editor |
A source that cannot seek is served by both: BookReader::buffered keeps
the compressed bytes in memory and still decodes on demand, and Book::read
decodes everything in one pass.
Examples
The examples leave out the surrounding function; each ? returns an
mbook::Error.
Show a book on a shelf
Opening reads the footer, the directory and five small tables; nothing else is decoded until it is asked for.
use BookReader;
let mut book = open?;
let meta = book.meta;
println!;
if let Some = book.read_cover?
Read a chapter and what it refers to
resolve reads nothing, so it can be called for every link and picture of
a chapter while it is laid out.
use ;
let mut book = open?;
for entry in book.toc
let Some = book.chapters.next.map else ;
let markdown = book.read_chapter?;
match book.resolve
Read from a stream
use ;
use io;
// The same lazy reader, over a buffer that holds the compressed file.
let mut book = buffered?;
println!;
use Book;
use io;
// Or everything decoded at once.
let book = read?;
for chapter in book.chapters
Create a book
use ;
use fs;
let mut book = new?;
book.meta_mut.add_author?;
book.set_cover?;
book.add_chapter?;
book.add_chapter?;
book.add_picture?;
// Optional: by default the contents are built from the headings.
book.set_toc;
book.save?;
Edit a book
Editing always goes through a Book; save writes a temporary file next
to the target and renames it into place.
use Book;
let mut book = open?;
book.meta_mut.set_title?;
if let Some = book.chapter_mut
book.remove_picture;
book.save?;
Diagrams, formulas and named lines
A diagram is stored as Mermaid source and a formula as KaTeX source; a chapter shows either the way it shows a picture. Any line gets a name from an anchor definition in front of it.
use Book;
let mut book = new?;
book.add_diagram?;
book.add_formula?;
book.add_chapter?;
// In another chapter: [the route](ch01.md#route)
When something is wrong
Rules about one argument are checked by the call that receives it. Rules that connect several parts of a book are checked when it is written, and reported together.
use ;
let book = open?;
match book.save
Book::check runs the same checks without writing.
What to know
- Lookups and reads are told apart by name. A call that only looks into
the tables takes
&selfand cannot fail with an I/O error. A call that touches the source starts withread_and takes&mut self. - Ids come from the reader. A
ChapterIdcannot be passed where aPictureIdis wanted, and neither has a public constructor. An id is a number inside one file; the stable name of a resource is its path. - Navigation is computed from the text. Heading anchors, named lines and the offsets of the contents are derived when a chapter is added or changed, so an edit cannot leave them stale.
- An invalid book cannot be written.
writeandsaveruncheckfirst and write nothing when it fails. - Text is normalised, not repaired. CRLF and CR become line feeds and a byte order mark is dropped. Raw HTML and control characters are reported.
- Rendering is the application's job. Chapters are returned as Markdown, pictures as AVIF bytes, diagrams and formulas as their source.
- No dependency type is public.
mopenzl,markdownanduuidstay behind the crate's own types and errors.
Limits
Every length and count read from a file is bounded before memory is
allocated. ReadOptions and WriteOptions carry the limits.
| Limit | Limits::default() |
|---|---|
max_resources |
1,000,000 |
max_resource_bytes, the decoded size of one resource |
64 MiB |
max_file_bytes |
16 GiB |
Cargo features
| Feature | Default | Enables |
|---|---|---|
read |
yes | BookReader, Book::read, Book::open |
write |
yes | Book::new and every call that changes, checks or saves a Book |
At least one of them must be enabled. A reading application that builds
with read alone gets no Markdown parser from this crate.
Status
Implements version 1.0 of the format as drafted: lazy reading, loading into memory, creating, editing, checking and writing. The format is still a draft and may change, and the API with it.
Documentation
- Guide: every task with its code
- API reference: each type and call, what it reads and when it fails
- Design: dependencies, costs and the alternatives that were rejected
- The file format
mbook-converterturns EPUB, FB2, PDF and five other formats into mbook, and back
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT license (LICENSE-MIT)
at your option.