The library not compiles (probably completely broken)

This commit is contained in:
Michael Bryan 2017-11-18 20:01:50 +08:00
parent 4c6c696c87
commit cafb8b75e7
No known key found for this signature in database
GPG Key ID: E9C602B0D9A998DC
3 changed files with 187 additions and 211 deletions

View File

@ -4,12 +4,12 @@ use std::collections::VecDeque;
use std::fs::File; use std::fs::File;
use std::io::{Read, Write}; use std::io::{Read, Write};
use super::summary::{parse_summary, Summary, Link, SummaryItem, SectionNumber}; use super::summary::{parse_summary, Link, SectionNumber, Summary, SummaryItem};
use errors::*; use errors::*;
/// Load a book into memory from its `src/` directory. /// Load a book into memory from its `src/` directory.
pub fn load_book<P: AsRef<Path>>(src_dir: P, create_if_not_present: bool) -> Result<Book> { pub fn load_book<P: AsRef<Path>>(src_dir: P) -> Result<Book> {
let src_dir = src_dir.as_ref(); let src_dir = src_dir.as_ref();
let summary_md = src_dir.join("SUMMARY.md"); let summary_md = src_dir.join("SUMMARY.md");
@ -20,7 +20,7 @@ pub fn load_book<P: AsRef<Path>>(src_dir: P, create_if_not_present: bool) -> Res
let summary = parse_summary(&summary_content).chain_err(|| "Summary parsing failed")?; let summary = parse_summary(&summary_content).chain_err(|| "Summary parsing failed")?;
load_book_from_disk(&summary, src_dir, create_if_not_present) load_book_from_disk(&summary, src_dir)
} }
@ -41,7 +41,9 @@ impl Book {
/// Get a depth-first iterator over the items in the book. /// Get a depth-first iterator over the items in the book.
pub fn iter(&self) -> BookItems { pub fn iter(&self) -> BookItems {
BookItems { items: self.sections.iter().collect() } BookItems {
items: self.sections.iter().collect(),
}
} }
} }
@ -86,7 +88,7 @@ impl Chapter {
/// ///
/// You need to pass in the book's source directory because all the links in /// You need to pass in the book's source directory because all the links in
/// `SUMMARY.md` give the chapter locations relative to it. /// `SUMMARY.md` give the chapter locations relative to it.
fn load_book_from_disk<P: AsRef<Path>>(summary: &Summary, src_dir: P, create_if_not_present: bool) -> Result<Book> { fn load_book_from_disk<P: AsRef<Path>>(summary: &Summary, src_dir: P) -> Result<Book> {
debug!("[*] Loading the book from disk"); debug!("[*] Loading the book from disk");
let src_dir = src_dir.as_ref(); let src_dir = src_dir.as_ref();
@ -99,24 +101,18 @@ fn load_book_from_disk<P: AsRef<Path>>(summary: &Summary, src_dir: P, create_if_
let mut chapters = Vec::new(); let mut chapters = Vec::new();
for summary_item in summary_items { for summary_item in summary_items {
let chapter = load_summary_item(summary_item, src_dir, create_if_not_present)?; let chapter = load_summary_item(summary_item, src_dir)?;
chapters.push(chapter); chapters.push(chapter);
} }
Ok(Book { sections: chapters }) Ok(Book { sections: chapters })
} }
fn load_summary_item<P: AsRef<Path>>(item: &SummaryItem, src_dir: P, create_if_not_present: bool) -> Result<BookItem> { fn load_summary_item<P: AsRef<Path>>(item: &SummaryItem, src_dir: P) -> Result<BookItem> {
match *item { match *item {
SummaryItem::Separator => Ok(BookItem::Separator), SummaryItem::Separator => Ok(BookItem::Separator),
SummaryItem::Link(ref link) => { SummaryItem::Link(ref link) => {
let file = src_dir.as_ref().join(&link.location); let file = src_dir.as_ref().join(&link.location);
if create_if_not_present && !file.exists() {
let text = format!("# {}", link.name);
File::create(&file)?.write_all(text.as_bytes())?;
}
load_chapter(link, src_dir).map(|c| BookItem::Chapter(c)) load_chapter(link, src_dir).map(|c| BookItem::Chapter(c))
}, },
} }
@ -132,21 +128,21 @@ fn load_chapter<P: AsRef<Path>>(link: &Link, src_dir: P) -> Result<Chapter> {
src_dir.join(&link.location) src_dir.join(&link.location)
}; };
let mut f = File::open(&location).chain_err(|| { let mut f = File::open(&location).chain_err(|| format!("Chapter file not found, {}", link.location.display()))?;
format!("Chapter file not found, {}", link.location.display())
})?;
let mut content = String::new(); let mut content = String::new();
f.read_to_string(&mut content)?; f.read_to_string(&mut content)?;
let stripped = location.strip_prefix(&src_dir).expect("Chapters are always inside a book"); let stripped = location
.strip_prefix(&src_dir)
.expect("Chapters are always inside a book");
let mut ch = Chapter::new(&link.name, content, stripped); let mut ch = Chapter::new(&link.name, content, stripped);
ch.number = link.number.clone(); ch.number = link.number.clone();
let sub_items = link.nested_items let sub_items = link.nested_items
.iter() .iter()
.map(|i| load_summary_item(i, src_dir, false)) .map(|i| load_summary_item(i, src_dir))
.collect::<Result<Vec<_>>>()?; .collect::<Result<Vec<_>>>()?;
ch.sub_items = sub_items; ch.sub_items = sub_items;
@ -286,7 +282,7 @@ And here is some more text.
], ],
}); });
let got = load_summary_item(&SummaryItem::Link(root), temp.path(), false).unwrap(); let got = load_summary_item(&SummaryItem::Link(root), temp.path()).unwrap();
assert_eq!(got, should_be); assert_eq!(got, should_be);
} }
@ -308,7 +304,7 @@ And here is some more text.
], ],
}; };
let got = load_book_from_disk(&summary, temp.path(), false).unwrap(); let got = load_book_from_disk(&summary, temp.path()).unwrap();
assert_eq!(got, should_be); assert_eq!(got, should_be);
} }
@ -372,21 +368,4 @@ And here is some more text.
assert_eq!(chapter_names, should_be); assert_eq!(chapter_names, should_be);
} }
#[test]
fn create_missing_book_items() {
let (link, temp) = dummy_link();
let summary = Summary {
numbered_chapters: vec![SummaryItem::Link(link)],
..Default::default()
};
let chapter_1 = temp.path().join("chapter_1.md");
fs::remove_file(&chapter_1).unwrap();
assert!(!chapter_1.exists());
load_book_from_disk(&summary, temp.path(), true).unwrap();
assert!(chapter_1.exists());
}
} }

View File

@ -1,6 +1,7 @@
pub mod bookitem; mod summary;
mod book;
pub use self::bookitem::{BookItem, BookItems}; pub use self::book::{Book, BookItem, BookItems, Chapter};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::fs::{self, File}; use std::fs::{self, File};
@ -8,7 +9,7 @@ use std::io::Write;
use std::process::Command; use std::process::Command;
use tempdir::TempDir; use tempdir::TempDir;
use {parse, theme, utils}; use {theme, utils};
use renderer::{HtmlHandlebars, Renderer}; use renderer::{HtmlHandlebars, Renderer};
use preprocess; use preprocess;
use errors::*; use errors::*;
@ -19,57 +20,81 @@ pub struct MDBook {
pub root: PathBuf, pub root: PathBuf,
pub config: Config, pub config: Config,
pub content: Vec<BookItem>, book: Book,
renderer: Box<Renderer>, renderer: Box<Renderer>,
pub livereload: Option<String>, pub livereload: Option<String>,
} }
impl MDBook { impl MDBook {
/// Create a new `MDBook` struct with root directory `root` /// Load a book from its root directory on disk.
/// pub fn load<P: Into<PathBuf>>(book_root: P) -> Result<MDBook> {
/// # Examples let book_root = book_root.into();
/// let config_location = book_root.join("book.toml");
/// ```no_run
/// # extern crate mdbook;
/// # use mdbook::MDBook;
/// # #[allow(unused_variables)]
/// # fn main() {
/// let book = MDBook::new("root_dir");
/// # }
/// ```
///
/// In this example, `root_dir` will be the root directory of our book
/// and is specified in function of the current working directory
/// by using a relative path instead of an
/// absolute path.
///
/// Default directory paths:
///
/// - source: `root/src`
/// - output: `root/book`
/// - theme: `root/theme`
///
/// They can both be changed by using [`set_src()`](#method.set_src) and
/// [`set_dest()`](#method.set_dest)
pub fn new<P: Into<PathBuf>>(root: P) -> MDBook { let config = if config_location.exists() {
let root = root.into(); Config::from_disk(&config_location)?
if !root.exists() || !root.is_dir() { } else {
warn!("{:?} No directory with that name", root); Config::default()
} };
MDBook { let src_dir = book_root.join(&config.book.src);
root: root, let book = book::load_book(&src_dir)?;
config: Config::default(),
content: vec![], let md = MDBook {
root: book_root,
config: config,
book: book,
renderer: Box::new(HtmlHandlebars::new()), renderer: Box::new(HtmlHandlebars::new()),
livereload: None, livereload: None,
} }
} }
// /// Create a new `MDBook` struct with root directory `root`
// ///
// /// # Examples
// ///
// /// ```no_run
// /// # extern crate mdbook;
// /// # use mdbook::MDBook;
// /// # #[allow(unused_variables)]
// /// # fn main() {
// /// let book = MDBook::new("root_dir");
// /// # }
// /// ```
// ///
// /// In this example, `root_dir` will be the root directory of our book
// /// and is specified in function of the current working directory
// /// by using a relative path instead of an
// /// absolute path.
// ///
// /// Default directory paths:
// ///
// /// - source: `root/src`
// /// - output: `root/book`
// /// - theme: `root/theme`
// ///
// /// They can both be changed by using [`set_src()`](#method.set_src) and
// /// [`set_dest()`](#method.set_dest)
// pub fn new<P: Into<PathBuf>>(root: P) -> MDBook {
// let root = root.into();
// if !root.exists() || !root.is_dir() {
// warn!("{:?} No directory with that name", root);
// }
// MDBook {
// root: root,
// config: Config::default(),
// content: vec![],
// renderer: Box::new(HtmlHandlebars::new()),
// livereload: None,
// create_missing: true,
// }
// }
/// Returns a flat depth-first iterator over the elements of the book, /// Returns a flat depth-first iterator over the elements of the book,
/// it returns an [BookItem enum](bookitem.html): /// it returns an [BookItem enum](bookitem.html):
/// `(section: String, bookitem: &BookItem)` /// `(section: String, bookitem: &BookItem)`
@ -77,15 +102,14 @@ impl MDBook {
/// ```no_run /// ```no_run
/// # extern crate mdbook; /// # extern crate mdbook;
/// # use mdbook::MDBook; /// # use mdbook::MDBook;
/// # use mdbook::BookItem; /// # use mdbook::book::BookItem;
/// # #[allow(unused_variables)] /// # #[allow(unused_variables)]
/// # fn main() { /// # fn main() {
/// # let book = MDBook::new("mybook"); /// # let book = MDBook::new("mybook");
/// for item in book.iter() { /// for item in book.iter() {
/// match item { /// match *item {
/// &BookItem::Chapter(ref section, ref chapter) => {}, /// BookItem::Chapter(ref chapter) => {},
/// &BookItem::Affix(ref chapter) => {}, /// BookItem::Spacer => {},
/// &BookItem::Spacer => {},
/// } /// }
/// } /// }
/// ///
@ -100,11 +124,7 @@ impl MDBook {
/// ``` /// ```
pub fn iter(&self) -> BookItems { pub fn iter(&self) -> BookItems {
BookItems { self.book.iter()
items: &self.content[..],
current_index: 0,
stack: Vec::new(),
}
} }
/// `init()` creates some boilerplate files and directories /// `init()` creates some boilerplate files and directories
@ -122,104 +142,106 @@ impl MDBook {
/// and adds a `SUMMARY.md` and a /// and adds a `SUMMARY.md` and a
/// `chapter_1.md` to the source directory. /// `chapter_1.md` to the source directory.
pub fn init(&mut self) -> Result<()> { pub fn init<P: AsRef<Path>>(book_root: P) -> Result<MDBook> {
debug!("[fn]: init"); unimplemented!()
// debug!("[fn]: init");
if !self.root.exists() { // if !self.root.exists() {
fs::create_dir_all(&self.root).unwrap(); // fs::create_dir_all(&self.root).unwrap();
info!("{:?} created", self.root.display()); // info!("{:?} created", self.root.display());
// }
// {
// let dest = self.get_destination();
// if !dest.exists() {
// debug!("[*]: {} does not exist, trying to create directory",
// dest.display()); fs::create_dir_all(dest)?;
// }
// let src = self.get_source();
// if !src.exists() {
// debug!("[*]: {} does not exist, trying to create directory",
// src.display()); fs::create_dir_all(&src)?;
// }
// let summary = src.join("SUMMARY.md");
// if !summary.exists() {
// // Summary does not exist, create it
// debug!("[*]: {:?} does not exist, trying to create SUMMARY.md",
// &summary); let mut f = File::create(&summary)?;
// debug!("[*]: Writing to SUMMARY.md");
// writeln!(f, "# Summary")?;
// writeln!(f, "")?;
// writeln!(f, "- [Chapter 1](./chapter_1.md)")?;
// }
// }
// // parse SUMMARY.md, and create the missing item related file
// self.parse_summary()?;
// debug!("[*]: constructing paths for missing files");
// for item in self.iter() {
// debug!("[*]: item: {:?}", item);
// let ch = match *item {
// BookItem::Spacer => continue,
// BookItem::Chapter(_, ref ch) | BookItem::Affix(ref ch) => ch,
// };
// if !ch.path.as_os_str().is_empty() {
// let path = self.get_source().join(&ch.path);
// if !path.exists() {
// if !self.create_missing {
// return Err(
// format!("'{}' referenced from SUMMARY.md does not
// exist.", path.to_string_lossy()).into(), );
// }
// debug!("[*]: {:?} does not exist, trying to create file", path);
// ::std::fs::create_dir_all(path.parent().unwrap())?;
// let mut f = File::create(path)?;
// // debug!("[*]: Writing to {:?}", path);
// writeln!(f, "# {}", ch.name)?;
// }
// }
// }
// debug!("[*]: init done");
// Ok(())
} }
{ // pub fn create_gitignore(&self) {
let dest = self.get_destination(); // let gitignore = self.get_gitignore();
if !dest.exists() {
debug!("[*]: {} does not exist, trying to create directory", dest.display());
fs::create_dir_all(dest)?;
}
// let destination = self.get_destination();
let src = self.get_source(); // // Check that the gitignore does not extist and that the destination path
if !src.exists() { // // begins with the root path
debug!("[*]: {} does not exist, trying to create directory", src.display()); // // We assume tha if it does begin with the root path it is contained
fs::create_dir_all(&src)?; // within. // This assumption
} // // will not hold true for paths containing double dots to go back up e.g.
// // `root/../destination`
// if !gitignore.exists() && destination.starts_with(&self.root) {
// let relative = destination
// .strip_prefix(&self.root)
// .expect("Could not strip the root prefix, path is not relative
// to root") .to_str()
// .expect("Could not convert to &str");
let summary = src.join("SUMMARY.md"); // debug!("[*]: {:?} does not exist, trying to create .gitignore",
// gitignore);
if !summary.exists() { // let mut f = File::create(&gitignore).expect("Could not create
// Summary does not exist, create it // file.");
debug!("[*]: {:?} does not exist, trying to create SUMMARY.md",
&summary);
let mut f = File::create(&summary)?;
debug!("[*]: Writing to SUMMARY.md"); // debug!("[*]: Writing to .gitignore");
writeln!(f, "# Summary")?; // writeln!(f, "/{}", relative).expect("Could not write to file.");
writeln!(f, "")?; // }
writeln!(f, "- [Chapter 1](./chapter_1.md)")?; // }
}
}
// parse SUMMARY.md, and create the missing item related file
self.parse_summary()?;
debug!("[*]: constructing paths for missing files");
for item in self.iter() {
debug!("[*]: item: {:?}", item);
let ch = match *item {
BookItem::Spacer => continue,
BookItem::Chapter(_, ref ch) | BookItem::Affix(ref ch) => ch,
};
if !ch.path.as_os_str().is_empty() {
let path = self.get_source().join(&ch.path);
if !path.exists() {
if !self.config.build.create_missing {
return Err(
format!("'{}' referenced from SUMMARY.md does not exist.", path.to_string_lossy()).into(),
);
}
debug!("[*]: {:?} does not exist, trying to create file", path);
::std::fs::create_dir_all(path.parent().unwrap())?;
let mut f = File::create(path)?;
// debug!("[*]: Writing to {:?}", path);
writeln!(f, "# {}", ch.name)?;
}
}
}
debug!("[*]: init done");
Ok(())
}
pub fn create_gitignore(&self) {
let gitignore = self.get_gitignore();
let destination = self.get_destination();
// Check that the gitignore does not extist and that the destination path
// begins with the root path
// We assume tha if it does begin with the root path it is contained within.
// This assumption
// will not hold true for paths containing double dots to go back up e.g.
// `root/../destination`
if !gitignore.exists() && destination.starts_with(&self.root) {
let relative = destination
.strip_prefix(&self.root)
.expect("Could not strip the root prefix, path is not relative to root")
.to_str()
.expect("Could not convert to &str");
debug!("[*]: {:?} does not exist, trying to create .gitignore", gitignore);
let mut f = File::create(&gitignore).expect("Could not create file.");
debug!("[*]: Writing to .gitignore");
writeln!(f, "/{}", relative).expect("Could not write to file.");
}
}
/// The `build()` method is the one where everything happens. /// The `build()` method is the one where everything happens.
/// First it parses `SUMMARY.md` to construct the book's structure /// First it parses `SUMMARY.md` to construct the book's structure
@ -230,8 +252,6 @@ impl MDBook {
pub fn build(&mut self) -> Result<()> { pub fn build(&mut self) -> Result<()> {
debug!("[fn]: build"); debug!("[fn]: build");
self.init()?;
// Clean output directory // Clean output directory
utils::fs::remove_dir_content(&self.get_destination())?; utils::fs::remove_dir_content(&self.get_destination())?;
@ -239,18 +259,13 @@ impl MDBook {
} }
pub fn get_gitignore(&self) -> PathBuf {
self.root.join(".gitignore")
}
pub fn copy_theme(&self) -> Result<()> { pub fn copy_theme(&self) -> Result<()> {
debug!("[fn]: copy_theme"); debug!("[fn]: copy_theme");
let themedir = self.theme_dir(); let themedir = self.theme_dir();
if !themedir.exists() { if !themedir.exists() {
debug!("[*]: {:?} does not exist, trying to create directory", debug!("[*]: {:?} does not exist, trying to create directory", themedir);
themedir);
fs::create_dir(&themedir)?; fs::create_dir(&themedir)?;
} }
@ -288,7 +303,8 @@ impl MDBook {
pub fn write_file<P: AsRef<Path>>(&self, filename: P, content: &[u8]) -> Result<()> { pub fn write_file<P: AsRef<Path>>(&self, filename: P, content: &[u8]) -> Result<()> {
let path = self.get_destination().join(filename); let path = self.get_destination().join(filename);
utils::fs::create_file(&path)?.write_all(content) utils::fs::create_file(&path)?
.write_all(content)
.map_err(|e| e.into()) .map_err(|e| e.into())
} }
@ -340,8 +356,6 @@ impl MDBook {
} }
pub fn test(&mut self, library_paths: Vec<&str>) -> Result<()> { pub fn test(&mut self, library_paths: Vec<&str>) -> Result<()> {
// read in the chapters
self.parse_summary().chain_err(|| "Couldn't parse summary")?;
let library_args: Vec<&str> = (0..library_paths.len()) let library_args: Vec<&str> = (0..library_paths.len())
.map(|_| "-L") .map(|_| "-L")
.zip(library_paths.into_iter()) .zip(library_paths.into_iter())
@ -349,7 +363,7 @@ impl MDBook {
.collect(); .collect();
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.as_os_str().is_empty() {
let path = self.get_source().join(&ch.path); let path = self.get_source().join(&ch.path);
let base = path.parent() let base = path.parent()
@ -364,14 +378,14 @@ impl MDBook {
let mut tmpf = utils::fs::create_file(&path)?; let mut tmpf = utils::fs::create_file(&path)?;
tmpf.write_all(content.as_bytes())?; tmpf.write_all(content.as_bytes())?;
let output = Command::new("rustdoc").arg(&path) let output = Command::new("rustdoc")
.arg(&path)
.arg("--test") .arg("--test")
.args(&library_args) .args(&library_args)
.output()?; .output()?;
if !output.status.success() { if !output.status.success() {
bail!(ErrorKind::Subprocess("Rustdoc returned an error".to_string(), bail!(ErrorKind::Subprocess("Rustdoc returned an error".to_string(), output));
output));
} }
} }
} }
@ -379,14 +393,6 @@ impl MDBook {
Ok(()) Ok(())
} }
// Construct book
fn parse_summary(&mut self) -> Result<()> {
// When append becomes stable, use self.content.append() ...
let summary = self.get_source().join("SUMMARY.md");
self.content = parse::construct_bookitems(&summary)?;
Ok(())
}
pub fn get_destination(&self) -> PathBuf { pub fn get_destination(&self) -> PathBuf {
self.root.join(&self.config.build.build_dir) self.root.join(&self.config.build.build_dir)
} }

View File

@ -2,7 +2,7 @@ use renderer::html_handlebars::helpers;
use preprocess; use preprocess;
use renderer::Renderer; use renderer::Renderer;
use book::MDBook; use book::MDBook;
use book::bookitem::{BookItem, Chapter}; use book::{BookItem, Chapter};
use config::{Config, Playpen, HtmlConfig}; use config::{Config, Playpen, HtmlConfig};
use {utils, theme}; use {utils, theme};
use theme::{Theme, playpen_editor}; use theme::{Theme, playpen_editor};
@ -35,13 +35,10 @@ impl HtmlHandlebars {
-> Result<()> { -> Result<()> {
// FIXME: This should be made DRY-er and rely less on mutable state // FIXME: This should be made DRY-er and rely less on mutable state
match *item { match *item {
BookItem::Chapter(_, ref ch) | BookItem::Affix(ref ch) BookItem::Chapter(ref ch) =>
if !ch.path.as_os_str().is_empty() =>
{ {
let path = ctx.book.get_source().join(&ch.path); let content = ch.content.clone();
let content = utils::fs::file_to_string(&path)?; let base = ch.path.parent().expect("All chapters must have a parent directory");
let base = path.parent()
.ok_or_else(|| String::from("Invalid bookitem path!"))?;
// Parse and expand links // Parse and expand links
let content = preprocess::links::replace_all(&content, base)?; let content = preprocess::links::replace_all(&content, base)?;
@ -397,7 +394,11 @@ fn make_data(book: &MDBook, config: &Config) -> Result<serde_json::Map<String, s
let mut chapter = BTreeMap::new(); let mut chapter = BTreeMap::new();
match *item { match *item {
BookItem::Affix(ref ch) => { BookItem::Chapter(ref ch) => {
if let Some(ref section) = ch.number {
chapter.insert("section".to_owned(), json!(section.to_string()));
}
chapter.insert("name".to_owned(), json!(ch.name)); chapter.insert("name".to_owned(), json!(ch.name));
let path = ch.path.to_str().ok_or_else(|| { let path = ch.path.to_str().ok_or_else(|| {
io::Error::new(io::ErrorKind::Other, io::Error::new(io::ErrorKind::Other,
@ -406,17 +407,7 @@ fn make_data(book: &MDBook, config: &Config) -> Result<serde_json::Map<String, s
})?; })?;
chapter.insert("path".to_owned(), json!(path)); chapter.insert("path".to_owned(), json!(path));
} }
BookItem::Chapter(ref s, ref ch) => { BookItem::Separator => {
chapter.insert("section".to_owned(), json!(s));
chapter.insert("name".to_owned(), json!(ch.name));
let path = ch.path.to_str().ok_or_else(|| {
io::Error::new(io::ErrorKind::Other,
"Could not convert path \
to str")
})?;
chapter.insert("path".to_owned(), json!(path));
}
BookItem::Spacer => {
chapter.insert("spacer".to_owned(), json!("_spacer_")); chapter.insert("spacer".to_owned(), json!("_spacer_"));
} }
} }