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.
This builder can add simple interface styles via StyleOption or modify document styles by manually write css files.
Fields§
§id: StringThe 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
impl ContentBuilder
Sourcepub fn new(id: &str, language: &str) -> Result<Self, EpubError>
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
Sourcepub fn set_title(&mut self, title: &str) -> &mut Self
pub fn set_title(&mut self, title: &str) -> &mut Self
Sets the title displayed in the document’s head section.
Sourcepub fn set_styles(&mut self, styles: StyleOptions) -> &mut Self
pub fn set_styles(&mut self, styles: StyleOptions) -> &mut Self
Sets the styles for the document
Sourcepub fn add_css_file(
&mut self,
css_path: PathBuf,
) -> Result<&mut Self, EpubError>
pub fn add_css_file( &mut self, css_path: PathBuf, ) -> Result<&mut Self, EpubError>
Adds a CSS file to the document
Copies the CSS file to a temporary directory for inclusion in the EPUB package. The CSS file will be linked in the document’s head section when generating the output.
§Parameters
css_path: The path to the CSS file to add
§Return
Ok(&mut self): If the file exists and is accessibleErr(EpubError): If the file does not exist or is not accessible
Sourcepub fn add_block(&mut self, block: Block) -> Result<&mut Self, EpubError>
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
Sourcepub fn add_text_block(
&mut self,
content: &str,
footnotes: Vec<Footnote>,
) -> Result<&mut Self, EpubError>
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 paragraphfootnotes: A vector of footnotes associated with the text
Sourcepub fn add_quote_block(
&mut self,
content: &str,
footnotes: Vec<Footnote>,
) -> Result<&mut Self, EpubError>
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 textfootnotes: A vector of footnotes associated with the quote
Sourcepub fn add_title_block(
&mut self,
content: &str,
level: usize,
footnotes: Vec<Footnote>,
) -> Result<&mut Self, EpubError>
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 textlevel: The heading level (1-6), corresponding to h1-h6 HTML tagsfootnotes: A vector of footnotes associated with the heading
Sourcepub fn add_image_block(
&mut self,
url: PathBuf,
alt: Option<String>,
caption: Option<String>,
footnotes: Vec<Footnote>,
) -> Result<&mut Self, EpubError>
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 filealt: Optional alternative text for the image (displayed when image cannot load)caption: Optional caption text to display below the imagefootnotes: A vector of footnotes associated with the caption or image
Sourcepub fn add_audio_block(
&mut self,
url: PathBuf,
fallback: String,
caption: Option<String>,
footnotes: Vec<Footnote>,
) -> Result<&mut Self, EpubError>
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 filefallback: Fallback text displayed when the audio cannot be playedcaption: Optional caption text to display below the audio playerfootnotes: A vector of footnotes associated with the caption or audio
Sourcepub fn add_video_block(
&mut self,
url: PathBuf,
fallback: String,
caption: Option<String>,
footnotes: Vec<Footnote>,
) -> Result<&mut Self, EpubError>
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 filefallback: Fallback text displayed when the video cannot be playedcaption: Optional caption text to display below the video playerfootnotes: A vector of footnotes associated with the caption or video
Sourcepub fn add_mathml_block(
&mut self,
element_str: String,
fallback_image: Option<PathBuf>,
caption: Option<String>,
footnotes: Vec<Footnote>,
) -> Result<&mut Self, EpubError>
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 stringfallback_image: Optional path to a fallback image displayed when MathML cannot rendercaption: Optional caption text to display below the MathML elementfootnotes: A vector of footnotes associated with the caption or equation
Sourcepub fn make<P: AsRef<Path>>(
&mut self,
target: P,
) -> Result<Vec<PathBuf>, EpubError>
pub fn make<P: AsRef<Path>>( &mut self, target: P, ) -> Result<Vec<PathBuf>, EpubError>
Builds content document
The final constructed content document has the following structure:
<body>
<main>
<!-- The specific block structure can be queried in the Block docs. -->
</main>
<aside>
<ul class="footnote-list">
<!-- Each footnote has the same structure. -->
<li class="footnote-item" id="footnote-{{ index }}">
<p>
<a herf="ref-{{ index }}">[{{ index }}]</a>
{{ footnote.content }}
</p>
</li>
</ul>
</aside>
</body>§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 documentErr(EpubError): Error occurred during the making process