Moved most of MDBook over to using the new Book format

This commit is contained in:
Michael Bryan 2017-08-21 10:23:52 +08:00
parent ce6dbd6736
commit 7ca198a700
3 changed files with 14 additions and 18 deletions

View File

@ -3,7 +3,7 @@ use std::collections::VecDeque;
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
use super::summary::{Summary, Link, SummaryItem, SectionNumber}; use super::summary::{parse_summary, Summary, Link, SummaryItem, SectionNumber};
use errors::*; use errors::*;

View File

@ -2,7 +2,7 @@ pub mod bookitem;
pub mod book; pub mod book;
pub mod summary; pub mod summary;
use self::book::{parse_book, Book, BookItem, BookItems}; use self::book::{load_book, Book, BookItem, BookItems};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::fs::{self, File}; use std::fs::{self, File};
@ -10,7 +10,7 @@ use std::io::{Read, Write};
use std::process::Command; use std::process::Command;
use tempdir::TempDir; use tempdir::TempDir;
use {theme, parse, utils}; use {theme, utils};
use renderer::{Renderer, HtmlHandlebars}; use renderer::{Renderer, HtmlHandlebars};
use preprocess; use preprocess;
use errors::*; use errors::*;
@ -23,7 +23,7 @@ use config::jsonconfig::JsonConfig;
pub struct MDBook { pub struct MDBook {
config: BookConfig, config: BookConfig,
pub content: Vec<BookItem>, pub content: Option<Book>,
renderer: Box<Renderer>, renderer: Box<Renderer>,
livereload: Option<String>, livereload: Option<String>,
@ -71,7 +71,7 @@ impl MDBook {
MDBook { MDBook {
config: BookConfig::new(root), config: BookConfig::new(root),
content: vec![], content: None,
renderer: Box::new(HtmlHandlebars::new()), renderer: Box::new(HtmlHandlebars::new()),
livereload: None, livereload: None,
@ -109,11 +109,8 @@ impl MDBook {
/// ``` /// ```
pub fn iter(&self) -> BookItems { pub fn iter(&self) -> BookItems {
BookItems { self.content.expect("Trying to iterate over a book before it is loaded. This is a bug")
items: &self.content[..], .iter()
current_index: 0,
stack: Vec::new(),
}
} }
/// `init()` creates some boilerplate files and directories /// `init()` creates some boilerplate files and directories
@ -176,9 +173,8 @@ impl MDBook {
for item in self.iter() { for item in self.iter() {
debug!("[*]: item: {:?}", item); debug!("[*]: item: {:?}", item);
let ch = match *item { let ch = match *item {
BookItem::Spacer => continue, BookItem::Separator => continue,
BookItem::Chapter(_, ref ch) | BookItem::Chapter(ref ch) => ch,
BookItem::Affix(ref ch) => ch,
}; };
if !ch.path.as_os_str().is_empty() { if !ch.path.as_os_str().is_empty() {
let path = self.config.get_source().join(&ch.path); let path = self.config.get_source().join(&ch.path);
@ -365,8 +361,8 @@ impl MDBook {
let temp_dir = TempDir::new("mdbook")?; let temp_dir = TempDir::new("mdbook")?;
for item in self.iter() { for item in self.iter() {
if let BookItem::Chapter(_, ref ch) = *item { if let BookItem::Chapter(ref ch) = *item {
if !ch.path.as_os_str().is_empty() { if ch.path != PathBuf::new() {
let path = self.get_source().join(&ch.path); let path = self.get_source().join(&ch.path);
let base = path.parent().ok_or_else( let base = path.parent().ok_or_else(
@ -517,8 +513,8 @@ impl MDBook {
// Construct book // Construct book
fn parse_summary(&mut self) -> Result<()> { fn parse_summary(&mut self) -> Result<()> {
// When append becomes stable, use self.content.append() ... let book = load_book(self.get_source().join("SUMMARY.md"))?;
self.content = parse::construct_bookitems(&self.get_source().join("SUMMARY.md"))?; self.content = Some(book);
Ok(()) Ok(())
} }
} }

View File

@ -98,7 +98,7 @@ pub mod theme;
pub mod utils; pub mod utils;
pub use book::MDBook; pub use book::MDBook;
pub use book::Book; pub use book::book::Book;
pub use renderer::Renderer; pub use renderer::Renderer;
/// The error types used through out this crate. /// The error types used through out this crate.