diff --git a/src/book/book.rs b/src/book/book.rs new file mode 100644 index 00000000..c5e46de8 --- /dev/null +++ b/src/book/book.rs @@ -0,0 +1,77 @@ +use book::metadata::BookMetadata; +use book::chapter::Chapter; + + +/// The `Book` struct contains the metadata and chapters for one language of the book. +/// Multiple `Book` structs are combined in the `MDBook` struct to support multi-language books. +pub struct Book { + metadata: BookMetadata, + + preface: Vec, + chapters: Vec, + appendix: Vec, +} + +impl Book { + /// Creates a new book with the given title, chapters are added with the `add_chapter` method + pub fn new(title: &str) -> Self { + Book { + metadata: BookMetadata::new(title), + + preface: Vec::new(), + chapters: Vec::new(), + appendix: Vec::new(), + } + } + + /// Adds a new chapter at the end of the book + pub fn add_chapter(&mut self, chapter: Chapter) -> &mut Self { + self.chapters.push(chapter); + self + } + + /// Adds a new preface chapter to the book, the preface chapters are in the order they were added + pub fn add_preface_chapter(&mut self, chapter: Chapter) -> &mut Self { + self.preface.push(chapter); + self + } + + /// Adds a new appendix chapter to the book, they are in de order they are added + pub fn add_appendix_chapter(&mut self, chapter: Chapter) -> &mut Self { + self.appendix.push(chapter); + self + } + + + /// This method takes a slice `&[x, y, z]` as parameter and returns the corresponding chapter. + /// For example, to retrieve chapter 2.3 we would use: + /// ``` + /// #extern crate mdbook; + /// #use mdbook::book::Book; + /// #fn main() { + /// #let book = Book::new("Test"); + /// let chapter_2_3 = book.get_chapter(&[2, 3]); + /// #} + /// ``` + pub fn get_chapter(&self, section: &[usize]) -> Option<&Chapter> { + match section.len() { + 0 => None, + 1 => self.chapters.get(section[0]), + _ => { + self.chapters + .get(section[0]) + .and_then(|ch| ch.get_sub_chapter(§ion[1..])) + }, + } + } + + /// Returns a mutable reference to the metadata for modification + pub fn mut_metadata(&mut self) -> &mut BookMetadata { + &mut self.metadata + } + + // Returns a reference to the metadata + pub fn metadata(&self) -> &BookMetadata { + &self.metadata + } +} diff --git a/src/book/chapter.rs b/src/book/chapter.rs index bd37ccac..23244cb5 100644 --- a/src/book/chapter.rs +++ b/src/book/chapter.rs @@ -1,5 +1,3 @@ -use book::metadata::BookMetadata; - use std::path; /// The Chapter struct holds the title of the chapter as written in the SUMMARY.md file, diff --git a/src/book/mod.rs b/src/book/mod.rs index 4e64092c..4ce695a1 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -1,5 +1,12 @@ pub mod bookitem; pub mod bookconfig; +pub mod metadata; +pub mod chapter; +pub mod book; + +pub use self::metadata::{Author, Language, BookMetadata}; +pub use self::chapter::Chapter; +pub use self::book::Book; pub use self::bookitem::{BookItem, BookItems}; pub use self::bookconfig::BookConfig;