From b777a318f7c4b0e74246be917d7ccdf7c25ba98c Mon Sep 17 00:00:00 2001 From: Michael Bryan Date: Fri, 26 Jan 2018 01:11:48 +0800 Subject: [PATCH] Expose functionality for creating core types (#578) * You can now add chapters to a Book * Made the RenderContext::new() constructor public --- src/book/book.rs | 12 ++++++++++++ src/renderer/mod.rs | 6 ++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/book/book.rs b/src/book/book.rs index ff7758e6..adb49ade 100644 --- a/src/book/book.rs +++ b/src/book/book.rs @@ -101,6 +101,12 @@ impl Book { { for_each_mut(&mut func, &mut self.sections); } + + /// Append a `BookItem` to the `Book`. + pub fn push_item>(&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) @@ -126,6 +132,12 @@ pub enum BookItem { Separator, } +impl From for BookItem { + fn from(other: Chapter) -> BookItem { + BookItem::Chapter(other) + } +} + /// The representation of a "chapter", usually mapping to a single file on /// disk however it may contain multiple sub-chapters. #[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)] diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index fb8d40cd..ad3a66a0 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -26,6 +26,8 @@ use errors::*; use config::Config; use book::Book; +const MDBOOK_VERSION: &str = env!("CARGO_PKG_VERSION"); + /// An arbitrary `mdbook` backend. /// /// Although it's quite possible for you to import `mdbook` as a library and @@ -68,7 +70,7 @@ pub struct RenderContext { impl RenderContext { /// Create a new `RenderContext`. - pub(crate) fn new(root: P, book: Book, config: Config, destination: Q) -> RenderContext + pub fn new(root: P, book: Book, config: Config, destination: Q) -> RenderContext where P: Into, Q: Into, @@ -76,7 +78,7 @@ impl RenderContext { RenderContext { book: book, config: config, - version: env!("CARGO_PKG_VERSION").to_string(), + version: MDBOOK_VERSION.to_string(), root: root.into(), destination: destination.into(), }