Skip to main content

ContentBuilder

Struct ContentBuilder 

Source
pub struct ContentBuilder {
    pub id: String,
    /* private fields */
}
Expand description

Content Builder

A builder for constructing EPUB content documents with various block types. This builder manages the creation and organization of content blocks including text, quotes, headings, images, audio, video, and MathML content.

Fields§

§id: String

The unique identifier for the content document

This identifier is used to uniquely identify the content document within the EPUB container. If the identifier is not unique, only one content document will be included in the EPUB container; and the other content document will be ignored.

Implementations§

Source§

impl ContentBuilder

Source

pub fn new(id: &str, language: &str) -> Result<Self, EpubError>

Creates a new ContentBuilder instance

Initializes a ContentBuilder with the specified language code. A temporary directory is automatically created to store media files during construction.

§Parameters
  • language: The language code for the document
Source

pub fn set_title(&mut self, title: &str) -> &mut Self

Sets the title of the document

Sets the title that will be displayed in the document’s head section.

§Parameters
  • title: The title text for the document
Source

pub fn add_block(&mut self, block: Block) -> Result<&mut Self, EpubError>

Adds a block to the document

Adds a constructed Block to the document.

§Parameters
  • block: The Block to add to the document
Source

pub fn add_text_block( &mut self, content: &str, footnotes: Vec<Footnote>, ) -> Result<&mut Self, EpubError>

Adds a text block to the document

Convenience method that creates and adds a Text block using the provided content and footnotes.

§Parameters
  • content: The text content of the paragraph
  • footnotes: A vector of footnotes associated with the text
Source

pub fn add_quote_block( &mut self, content: &str, footnotes: Vec<Footnote>, ) -> Result<&mut Self, EpubError>

Adds a quote block to the document

Convenience method that creates and adds a Quote block using the provided content and footnotes.

§Parameters
  • content: The quoted text
  • footnotes: A vector of footnotes associated with the quote
Source

pub fn add_title_block( &mut self, content: &str, level: usize, footnotes: Vec<Footnote>, ) -> Result<&mut Self, EpubError>

Adds a heading block to the document

Convenience method that creates and adds a Title block with the specified level.

§Parameters
  • content: The heading text
  • level: The heading level (1-6), corresponding to h1-h6 HTML tags
  • footnotes: A vector of footnotes associated with the heading
Source

pub fn add_image_block( &mut self, url: PathBuf, alt: Option<String>, caption: Option<String>, footnotes: Vec<Footnote>, ) -> Result<&mut Self, EpubError>

Adds an image block to the document

Convenience method that creates and adds an Image block with optional alt text, caption, and footnotes.

§Parameters
  • url: The path to the image file
  • alt: Optional alternative text for the image (displayed when image cannot load)
  • caption: Optional caption text to display below the image
  • footnotes: A vector of footnotes associated with the caption or image
Source

pub fn add_audio_block( &mut self, url: PathBuf, fallback: String, caption: Option<String>, footnotes: Vec<Footnote>, ) -> Result<&mut Self, EpubError>

Adds an audio block to the document

Convenience method that creates and adds an Audio block with fallback text, optional caption, and footnotes.

§Parameters
  • url: The path to the audio file
  • fallback: Fallback text displayed when the audio cannot be played
  • caption: Optional caption text to display below the audio player
  • footnotes: A vector of footnotes associated with the caption or audio
Source

pub fn add_video_block( &mut self, url: PathBuf, fallback: String, caption: Option<String>, footnotes: Vec<Footnote>, ) -> Result<&mut Self, EpubError>

Adds a video block to the document

Convenience method that creates and adds a Video block with fallback text, optional caption, and footnotes.

§Parameters
  • url: The path to the video file
  • fallback: Fallback text displayed when the video cannot be played
  • caption: Optional caption text to display below the video player
  • footnotes: A vector of footnotes associated with the caption or video
Source

pub fn add_mathml_block( &mut self, element_str: String, fallback_image: Option<PathBuf>, caption: Option<String>, footnotes: Vec<Footnote>, ) -> Result<&mut Self, EpubError>

Adds a MathML block to the document

Convenience method that creates and adds a MathML block with optional fallback image, caption, and footnotes.

§Parameters
  • element_str: The raw MathML markup string
  • fallback_image: Optional path to a fallback image displayed when MathML cannot render
  • caption: Optional caption text to display below the MathML element
  • footnotes: A vector of footnotes associated with the caption or equation
Source

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

Removes the last block from the document

Discards the most recently added block. If no blocks exist, this method has no effect.

Source

pub fn take_last_block(&mut self) -> Option<Block>

Takes ownership of the last block

Removes and returns the most recently added block without consuming the builder. This allows you to extract a block while keeping the builder alive.

§Return
  • Some(Block): If a block exists
  • None: If the blocks collection is empty
Source

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

Clears all blocks from the document

Removes all blocks from the document while keeping the language and title settings intact.

Source

pub fn make<P: AsRef<Path>>( &mut self, target: P, ) -> Result<Vec<PathBuf>, EpubError>

Builds content document

§Parameters
  • target: The file path where the document should be written
§Return
  • Ok(Vec<PathBuf>): A vector of paths to all resources used in the document
  • Err(EpubError): Error occurred during the making process

Trait Implementations§

Source§

impl Debug for ContentBuilder

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Drop for ContentBuilder

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

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 T
where U: TryFrom<T>,

Source§

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.