Struct epub_builder::Toc

source ·
pub struct Toc {
    pub elements: Vec<TocElement>,
}
Expand description

A Table Of Contents

It basically contains a list of TocElements.

Example

Creates a Toc, fills it, and render it to HTML:

use epub_builder::{Toc, TocElement};
Toc::new()
   // add a level-1 element
   .add(TocElement::new("intro.xhtml", "Introduction"))
   // add a level-1 element with children
   .add(TocElement::new("chapter_1.xhtml", "Chapter 1")
           .child(TocElement::new("chapter_1.xhtml#section1", "1.1: Some section"))
           .child(TocElement::new("chapter_1.xhtml#section2", "1.2: another section")))
   // add a level-2 element, which will thus get "attached" to previous level-1 element
   .add(TocElement::new("chapter_1.xhtml#section3", "1.3: yet another section")
           .level(2))
   // render the toc (non-numbered list, escape html) and returns a string
   .render(false, true);

Fields§

§elements: Vec<TocElement>

The elements composing the TOC

Implementations§

source§

impl Toc

source

pub fn new() -> Toc

Creates a new, empty, Toc

source

pub fn is_empty(&self) -> bool

Returns true if the toc is empty, false else.

Note that empty here means that the the toc has zero or one element, since it’s still not worth displaying it in this case.

source

pub fn add(&mut self, element: TocElement) -> &mut Self

Adds a TocElement to the Toc.

This will look at the element’s level and will insert it as a child of the last element of the Toc that has an inferior level.

Example
let mut toc = Toc::new();
// Insert an element at default level (1)
toc.add(TocElement::new("chapter_1.xhtml", "Chapter 1"));

// Insert an element at level 2
toc.add(TocElement::new("section_1.xhtml", "Section 1")
          .level(2));
// "Section 1" is now a child of "Chapter 1"

There are some cases where this behaviour might not be what you want; however, it makes sure that the TOC can still be renderer correctly for HTML and EPUB.

source

pub fn render_epub(&mut self, escape_html: bool) -> String

Render the Toc in a toc.ncx compatible way, for EPUB.

  • escape_html: whether titles should be HTML-encoded or not (only applies to titles)
source

pub fn render(&mut self, numbered: bool, escape_html: bool) -> String

Render the Toc in either

    or
      form (according to numbered)

Trait Implementations§

source§

impl Debug for Toc

source§

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

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

impl Default for Toc

source§

fn default() -> Toc

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for Toc

§

impl Send for Toc

§

impl Sync for Toc

§

impl Unpin for Toc

§

impl UnwindSafe for Toc

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.