pub struct EpubBuilder<Z: Zip> { /* private fields */ }
Expand description

Epub Builder

The main struct you’ll need to use in this library. It is first created using a wrapper to zip files; then you add content to it, and finally you generate the EPUB file by calling the generate method.

use epub_builder::EpubBuilder;
use epub_builder::ZipCommand;
use std::io;

// "Empty" EPUB file
let mut builder = EpubBuilder::new(ZipCommand::new().unwrap()).unwrap();
builder.metadata("title", "Empty EPUB").unwrap();
builder.metadata("author", "Ann 'Onymous").unwrap();
builder.generate(&mut io::stdout()).unwrap();

Implementations§

source§

impl<Z: Zip> EpubBuilder<Z>

source

pub fn new(zip: Z) -> Result<EpubBuilder<Z>>

Create a new default EPUB Builder

source

pub fn epub_version(&mut self, version: EpubVersion) -> &mut Self

Set EPUB version (default: V20)

Supported versions are:

  • V20: EPUB 2.0.1
  • ’V30`: EPUB 3.0.1
source

pub fn metadata<S1, S2>(&mut self, key: S1, value: S2) -> Result<&mut Self>where S1: AsRef<str>, S2: Into<String>,

Set some EPUB metadata

For most metadata, this function will replace the existing metadata, but for subject, cteator and identifier who can have multiple values, it will add data to the existing data, unless the empty string “” is passed, in which case it will delete existing data for this key.

Valid keys used by the EPUB builder
  • author: author(s) of the book;
  • title: title of the book;
  • lang: the language of the book, quite important as EPUB renderers rely on it for e.g. hyphenating words.
  • generator: generator of the book (should be your program name);
  • toc_name: the name to use for table of contents (by default, “Table of Contents”);
  • subject;
  • description;
  • license.
source

pub fn set_authors(&mut self, value: Vec<String>)

Sets the authors of the EPUB

source

pub fn add_author<S: Into<String>>(&mut self, value: S)

Add an author to the EPUB

source

pub fn clear_authors<S: Into<String>>(&mut self)

Remove all authors from EPUB

source

pub fn set_title<S: Into<String>>(&mut self, value: S)

Sets the title of the EPUB

source

pub fn escape_html(&mut self, val: bool)

Tells whether fields should be HTML-escaped.

  • true: fields such as titles, description, and so on will be HTML-escaped everywhere (default)
  • false: fields will be left as is (letting you in charge of making sure they do not contain anything illegal, e.g. < and > characters)
source

pub fn set_lang<S: Into<String>>(&mut self, value: S)

Sets the language of the EPUB

This is quite important as EPUB renderers rely on it for e.g. hyphenating words.

source

pub fn set_generator<S: Into<String>>(&mut self, value: S)

Sets the generator of the book (should be your program name)

source

pub fn set_toc_name<S: Into<String>>(&mut self, value: S)

Sets the name to use for table of contents. This is by default, “Table of Contents”

source

pub fn set_description(&mut self, value: Vec<String>)

Sets and replaces the description of the EPUB

source

pub fn add_description<S: Into<String>>(&mut self, value: S)

Adds a line to the EPUB description

source

pub fn clear_description(&mut self)

Remove all description paragraphs from EPUB

source

pub fn set_subjects(&mut self, value: Vec<String>)

Sets and replaces the subjects of the EPUB

source

pub fn add_subject<S: Into<String>>(&mut self, value: S)

Adds a value to the subjects

source

pub fn clear_subjects(&mut self)

Remove all the subjects from EPUB

source

pub fn set_license<S: Into<String>>(&mut self, value: S)

Sets the license under which this EPUB is distributed

source

pub fn set_publication_date(&mut self, date_published: DateTime<Utc>)

Sets the publication date of the EPUB

source

pub fn set_modified_date(&mut self, date_modified: DateTime<Utc>)

Sets the date on which the EPUB was last modified.

This value is part of the metadata. If this function is not called, the time at the moment of generation will be used instead.

source

pub fn set_uuid(&mut self, uuid: Uuid)

Sets the uuid used for the EPUB.

This is useful for reproducibly generating epubs.

source

pub fn stylesheet<R: Read>(&mut self, content: R) -> Result<&mut Self>

Sets stylesheet of the EPUB.

This content will be written in a stylesheet.css file; it is used by some pages (such as nav.xhtml), you don’t have use it in your documents though it makes sense to also do so.

source

pub fn inline_toc(&mut self) -> &mut Self

Adds an inline toc in the document.

If this method is called it adds a page that contains the table of contents that appears in the document.

The position where this table of contents will be inserted depends on when you call this method: if you call it before adding any content, it will be at the beginning, if you call it after, it will be at the end.

source

pub fn add_resource<R, P, S>( &mut self, path: P, content: R, mime_type: S ) -> Result<&mut Self>where R: Read, P: AsRef<Path>, S: Into<String>,

Add a resource to the EPUB file

This resource can be a picture, a font, some CSS file, …. Unlike add_content, files added this way won’t appear in the linear document.

Note that these files will automatically be inserted into an OEBPS directory, so you don’t need (and shouldn’t) prefix your path with OEBPS/.

Arguments
  • path: the path where this file will be written in the EPUB OEBPS structure, e.g. data/image_0.png
  • content: the resource to include
  • mime_type: the mime type of this file, e.g. “image/png”.
source

pub fn add_cover_image<R, P, S>( &mut self, path: P, content: R, mime_type: S ) -> Result<&mut Self>where R: Read, P: AsRef<Path>, S: Into<String>,

Add a cover image to the EPUB.

This works similarly to adding the image as a resource with the add_resource method, except, it signals it in the Manifest section so it is displayed as the cover by Ereaders

source

pub fn add_content<R: Read>( &mut self, content: EpubContent<R> ) -> Result<&mut Self>

Add a XHTML content file that will be added to the EPUB.

Examples
let content = "Some content";
let mut builder = EpubBuilder::new(ZipLibrary::new().unwrap()).unwrap();
// Add a chapter that won't be added to the Table of Contents
builder.add_content(EpubContent::new("intro.xhtml", content.as_bytes())).unwrap();
// Sets the title of a chapter so it is added to the Table of contents
// Also add information about its structure
builder.add_content(EpubContent::new("chapter_1.xhtml", content.as_bytes())
                     .title("Chapter 1")
                     .child(TocElement::new("chapter_1.xhtml#1", "1.1"))).unwrap();
// Add a section, by setting the level to 2 (instead of the default value 1)
builder.add_content(EpubContent::new("section.xhtml", content.as_bytes())
                     .title("Section 1")
                     .level(2)).unwrap();

Note that these files will automatically be inserted into an OEBPS directory, so you don’t need (and shouldn’t) prefix your path with OEBPS/.

See also
  • EpubContent
  • the add_resource method, to add other resources in the EPUB file.
source

pub fn generate<W: Write>(&mut self, to: W) -> Result<()>

Generate the EPUB file and write it to the writer

Example
let mut builder = EpubBuilder::new(ZipLibrary::new().unwrap()).unwrap();
// Write the EPUB file into a Vec<u8>
let mut epub: Vec<u8> = vec!();
builder.generate(&mut epub).unwrap();

Trait Implementations§

source§

impl<Z: Debug + Zip> Debug for EpubBuilder<Z>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<Z> RefUnwindSafe for EpubBuilder<Z>where Z: RefUnwindSafe,

§

impl<Z> Send for EpubBuilder<Z>where Z: Send,

§

impl<Z> Sync for EpubBuilder<Z>where Z: Sync,

§

impl<Z> Unpin for EpubBuilder<Z>where Z: Unpin,

§

impl<Z> UnwindSafe for EpubBuilder<Z>where Z: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.