Skip to content

Commit b777a31

Browse files
Expose functionality for creating core types (#578)
* You can now add chapters to a Book * Made the RenderContext::new() constructor public
1 parent 30e3b83 commit b777a31

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/book/book.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ impl Book {
101101
{
102102
for_each_mut(&mut func, &mut self.sections);
103103
}
104+
105+
/// Append a `BookItem` to the `Book`.
106+
pub fn push_item<I: Into<BookItem>>(&mut self, item: I) -> &mut Self {
107+
self.sections.push(item.into());
108+
self
109+
}
104110
}
105111

106112
pub fn for_each_mut<'a, F, I>(func: &mut F, items: I)
@@ -126,6 +132,12 @@ pub enum BookItem {
126132
Separator,
127133
}
128134

135+
impl From<Chapter> for BookItem {
136+
fn from(other: Chapter) -> BookItem {
137+
BookItem::Chapter(other)
138+
}
139+
}
140+
129141
/// The representation of a "chapter", usually mapping to a single file on
130142
/// disk however it may contain multiple sub-chapters.
131143
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]

src/renderer/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ use errors::*;
2626
use config::Config;
2727
use book::Book;
2828

29+
const MDBOOK_VERSION: &str = env!("CARGO_PKG_VERSION");
30+
2931
/// An arbitrary `mdbook` backend.
3032
///
3133
/// Although it's quite possible for you to import `mdbook` as a library and
@@ -68,15 +70,15 @@ pub struct RenderContext {
6870

6971
impl RenderContext {
7072
/// Create a new `RenderContext`.
71-
pub(crate) fn new<P, Q>(root: P, book: Book, config: Config, destination: Q) -> RenderContext
73+
pub fn new<P, Q>(root: P, book: Book, config: Config, destination: Q) -> RenderContext
7274
where
7375
P: Into<PathBuf>,
7476
Q: Into<PathBuf>,
7577
{
7678
RenderContext {
7779
book: book,
7880
config: config,
79-
version: env!("CARGO_PKG_VERSION").to_string(),
81+
version: MDBOOK_VERSION.to_string(),
8082
root: root.into(),
8183
destination: destination.into(),
8284
}

0 commit comments

Comments
 (0)