Skip to main content

mbook_converter/
lib.rs

1//! Convert books between mbook and other formats.
2//!
3//! One type does the work: [`Converter`]. It takes a source as any
4//! [`Read`](std::io::Read) or as a byte slice, and gives an mbook file
5//! written to any [`Write`](std::io::Write), or a [`Book`](mbook::Book)
6//! that can still be changed.
7//! The optional `export` feature writes an existing book in another format.
8//!
9//! ```no_run
10//! use mbook_converter::Converter;
11//! use std::fs::File;
12//! use std::io::BufWriter;
13//!
14//! let source = File::open("book.fb2")?;
15//! let sink = BufWriter::new(File::create("book.mbook")?);
16//! let report = Converter::new().convert(source, sink)?;
17//!
18//! println!("converted from {}", report.format);
19//! for warning in &report.warnings {
20//!     eprintln!("{warning}");
21//! }
22//! # Ok::<(), Box<dyn std::error::Error>>(())
23//! ```
24//!
25//! The design is described in the `docs/src/mbook-converter` folder of the
26//! repository.
27//!
28//! # Features
29//!
30//! Each import format has a feature enabled by default.
31//!
32//! | Feature | Format |
33//! | --- | --- |
34//! | `epub` | EPUB 2 and 3 |
35//! | `fb2` | `FictionBook` 2, plain or inside a ZIP |
36//! | `pdf` | PDF |
37//! | `cbz` | Comic book archive |
38//! | `docx` | Word documents |
39//! | `html` | One HTML document |
40//! | `md` | One Markdown document |
41//! | `txt` | Plain text |
42//!
43//! A source in a format whose feature is disabled is still recognised and
44//! refused with [`Error::FormatDisabled`].
45//!
46//! The opt-in `rayon` feature encodes the pictures of a source on all cores
47//! at once instead of one after another. The book is the same.
48//!
49//! The opt-in `export` feature enables writing all eight formats, including
50//! their picture codecs, and [`figure`], which draws the diagrams and
51//! formulas of a book. It currently requires Rust 1.98 or later.
52//!
53// Without `export`, the link to `figure` leads to the table above instead.
54// The empty doc line above is what lets the definition start a new block.
55#![cfg_attr(not(feature = "export"), doc = "[`figure`]: #features")]
56
57#[cfg(not(any(
58    feature = "epub",
59    feature = "fb2",
60    feature = "pdf",
61    feature = "cbz",
62    feature = "docx",
63    feature = "html",
64    feature = "md",
65    feature = "txt",
66    feature = "export"
67)))]
68compile_error!("mbook-converter needs at least one import format or the export feature");
69
70// Without a format that carries pictures, `rayon` has nothing to encode.
71#[cfg(all(feature = "rayon", not(parallel_pictures)))]
72use rayon as _;
73
74mod assemble;
75mod converter;
76mod draft;
77// Text in any encoding, for the formats that are text and nothing else.
78#[cfg(any(feature = "md", feature = "txt"))]
79mod encoding;
80mod error;
81#[cfg(feature = "export")]
82mod export;
83#[cfg(feature = "export")]
84pub mod figure;
85mod format;
86mod formats;
87mod markdown;
88mod options;
89#[cfg(pictures)]
90mod picture;
91mod report;
92// HTML to Markdown, shared by the formats whose text is HTML.
93#[cfg(any(feature = "epub", feature = "html"))]
94mod xhtml;
95
96pub use converter::Converter;
97pub use error::{Error, Result};
98pub use format::Format;
99pub use mbook;
100pub use options::{ConvertLimits, ConvertOptions, PictureOptions};
101pub use report::{Converted, Report, Warning, WarningKind};