Expose functionality for creating core types (#578)

* You can now add chapters to a Book

* Made the RenderContext::new() constructor public
This commit is contained in:
Michael Bryan 2018-01-26 01:11:48 +08:00 committed by GitHub
parent 30e3b83167
commit b777a318f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 2 deletions

View File

@ -101,6 +101,12 @@ impl Book {
{ {
for_each_mut(&mut func, &mut self.sections); for_each_mut(&mut func, &mut self.sections);
} }
/// Append a `BookItem` to the `Book`.
pub fn push_item<I: Into<BookItem>>(&mut self, item: I) -> &mut Self {
self.sections.push(item.into());
self
}
} }
pub fn for_each_mut<'a, F, I>(func: &mut F, items: I) pub fn for_each_mut<'a, F, I>(func: &mut F, items: I)
@ -126,6 +132,12 @@ pub enum BookItem {
Separator, Separator,
} }
impl From<Chapter> for BookItem {
fn from(other: Chapter) -> BookItem {
BookItem::Chapter(other)
}
}
/// The representation of a "chapter", usually mapping to a single file on /// The representation of a "chapter", usually mapping to a single file on
/// disk however it may contain multiple sub-chapters. /// disk however it may contain multiple sub-chapters.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)] #[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]

View File

@ -26,6 +26,8 @@ use errors::*;
use config::Config; use config::Config;
use book::Book; use book::Book;
const MDBOOK_VERSION: &str = env!("CARGO_PKG_VERSION");
/// An arbitrary `mdbook` backend. /// An arbitrary `mdbook` backend.
/// ///
/// Although it's quite possible for you to import `mdbook` as a library and /// Although it's quite possible for you to import `mdbook` as a library and
@ -68,7 +70,7 @@ pub struct RenderContext {
impl RenderContext { impl RenderContext {
/// Create a new `RenderContext`. /// Create a new `RenderContext`.
pub(crate) fn new<P, Q>(root: P, book: Book, config: Config, destination: Q) -> RenderContext pub fn new<P, Q>(root: P, book: Book, config: Config, destination: Q) -> RenderContext
where where
P: Into<PathBuf>, P: Into<PathBuf>,
Q: Into<PathBuf>, Q: Into<PathBuf>,
@ -76,7 +78,7 @@ impl RenderContext {
RenderContext { RenderContext {
book: book, book: book,
config: config, config: config,
version: env!("CARGO_PKG_VERSION").to_string(), version: MDBOOK_VERSION.to_string(),
root: root.into(), root: root.into(),
destination: destination.into(), destination: destination.into(),
} }