Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for epub-version parameter in mdbook-epub plugin #122

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ its section number.
`curly-quotes`: Enable converting straight quotes `'x'` and `"x"` to `‘x’` and
`“x”` (aka *smart quotes*).

`epub-version`: Specifies the EPUB version to use. If omitted, the epub-builder
default version is used.
- `2` — EPUB 2.0.1
- `3` — EPUB 3.0.1

```toml
[output.epub]
additional-css = ["./path/to/main.css"]
Expand All @@ -83,6 +88,7 @@ cover-image = "ebook-cover.png"
additional-resources = ["./assets/Open-Sans-Regular.ttf"]
no-section-label = true
curly-quotes = true
epub-version = 3
```

## Logging, seeing progress
Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub struct Config {
pub no_section_label: bool,
/// Use "smart quotes" instead of the usual `"` character.
pub curly_quotes: bool,
/// EPUB version to use if specified, otherwise defaults to the epub-builder default.
pub epub_version: Option<u8>,
}

impl Config {
Expand Down Expand Up @@ -68,6 +70,7 @@ impl Default for Config {
additional_resources: Vec::new(),
no_section_label: false,
curly_quotes: false,
epub_version: None,
}
}
}
Expand Down
15 changes: 13 additions & 2 deletions src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
path::{Path, PathBuf},
};

use epub_builder::{EpubBuilder, EpubContent, ZipLibrary};
use epub_builder::{EpubBuilder, EpubContent, EpubVersion, ZipLibrary};
use handlebars::{Handlebars, RenderError, RenderErrorReason};
use html_parser::{Dom, Node};
use mdbook::book::{BookItem, Chapter};
Expand Down Expand Up @@ -44,9 +44,20 @@ impl<'a> Generator<'a> {
handler: impl ContentRetriever + 'static,
) -> Result<Generator<'a>, Error> {
let handler = Box::new(handler);
let builder = EpubBuilder::new(ZipLibrary::new()?)?;
let config = Config::from_render_context(ctx)?;

let epub_version = match config.epub_version {
Some(2) => Some(EpubVersion::V20),
Some(3) => Some(EpubVersion::V30),
Some(v) => return Err(Error::EpubDocCreate(format!("Unsupported epub version specified in book.toml: {}", v))),
None => None,
};

let mut builder = EpubBuilder::new(ZipLibrary::new()?)?;
if let Some(version) = epub_version {
builder.epub_version(version);
}

let mut hbs = Handlebars::new();
hbs.register_template_string("index", config.template()?)
.map_err(|_| Error::TemplateParse)?;
Expand Down