From 25f10b87cdb00ac6ab43f2659a434ab79c7db343 Mon Sep 17 00:00:00 2001 From: Dmitry Luschan Date: Tue, 29 Oct 2024 17:57:46 +0100 Subject: [PATCH] Add support for epub-version parameter in mdbook-epub plugin --- README.md | 6 ++++++ src/config.rs | 3 +++ src/generator.rs | 15 +++++++++++++-- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2c33c86ef..cedecad5a 100644 --- a/README.md +++ b/README.md @@ -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"] @@ -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 diff --git a/src/config.rs b/src/config.rs index 1e63f8402..328d141b2 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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, } impl Config { @@ -68,6 +70,7 @@ impl Default for Config { additional_resources: Vec::new(), no_section_label: false, curly_quotes: false, + epub_version: None, } } } diff --git a/src/generator.rs b/src/generator.rs index 4c681e149..73604c08f 100644 --- a/src/generator.rs +++ b/src/generator.rs @@ -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}; @@ -44,9 +44,20 @@ impl<'a> Generator<'a> { handler: impl ContentRetriever + 'static, ) -> Result, 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)?;