diff --git a/Cargo.toml b/Cargo.toml index f2ee4e67..e118bb13 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ lazy_static = "0.2" log = "0.3" env_logger = "0.4.0" toml = "0.4" +memchr = "2.0.1" open = "1.1" regex = "0.2.1" tempdir = "0.3.4" @@ -60,3 +61,6 @@ serve = ["iron", "staticfile", "ws"] doc = false name = "mdbook" path = "src/bin/mdbook.rs" + +[patch.crates-io] +pulldown-cmark = { git = "https://github.com/google/pulldown-cmark" } diff --git a/book-example/book.toml b/book-example/book.toml index 4dbc659c..e730b43e 100644 --- a/book-example/book.toml +++ b/book-example/book.toml @@ -1,6 +1,7 @@ +[book] title = "mdBook Documentation" description = "Create book from markdown files. Like Gitbook but implemented in Rust" author = "Mathieu David" [output.html] -mathjax-support = true \ No newline at end of file +mathjax-support = true diff --git a/src/bin/build.rs b/src/bin/build.rs index 54dffbd1..bc784ea9 100644 --- a/src/bin/build.rs +++ b/src/bin/build.rs @@ -1,5 +1,5 @@ use std::path::PathBuf; -use clap::{ArgMatches, SubCommand, App}; +use clap::{App, ArgMatches, SubCommand}; use mdbook::MDBook; use mdbook::errors::Result; use {get_book_dir, open}; @@ -10,32 +10,23 @@ pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> { .about("Build the book from the markdown files") .arg_from_usage("-o, --open 'Open the compiled book in a web browser'") .arg_from_usage( - "-d, --dest-dir=[dest-dir] 'The output directory for your \ - book{n}(Defaults to ./book when omitted)'", - ) - .arg_from_usage( - "--no-create 'Will not create non-existent files linked from SUMMARY.md (deprecated: use book.toml instead)'", - ) - .arg_from_usage( - "[dir] 'A directory for your book{n}(Defaults to Current Directory \ + "-d, --dest-dir=[dest-dir] 'The output directory for your book{n}(Defaults to ./book \ when omitted)'", ) + .arg_from_usage( + "[dir] 'A directory for your book{n}(Defaults to Current Directory when omitted)'", + ) } // Build command implementation pub fn execute(args: &ArgMatches) -> Result<()> { let book_dir = get_book_dir(args); - let mut book = MDBook::new(&book_dir).read_config()?; + let mut book = MDBook::load(&book_dir)?; if let Some(dest_dir) = args.value_of("dest-dir") { book.config.build.build_dir = PathBuf::from(dest_dir); } - // This flag is deprecated in favor of being set via `book.toml`. - if args.is_present("no-create") { - book.config.build.create_missing = false; - } - book.build()?; if args.is_present("open") { diff --git a/src/bin/init.rs b/src/bin/init.rs index 83ac54be..3a14d9c0 100644 --- a/src/bin/init.rs +++ b/src/bin/init.rs @@ -19,45 +19,31 @@ pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> { // Init command implementation pub fn execute(args: &ArgMatches) -> Result<()> { let book_dir = get_book_dir(args); - let mut book = MDBook::new(&book_dir); - - // Call the function that does the initialization - book.init()?; + let mut builder = MDBook::init(&book_dir); // If flag `--theme` is present, copy theme to src if args.is_present("theme") { // Skip this if `--force` is present if !args.is_present("force") { // Print warning - print!("\nCopying the default theme to {:?}", book.get_source()); - println!("could potentially overwrite files already present in that directory."); + print!("\nCopying the default theme to {}", builder.config().book.src.display()); + println!("This could potentially overwrite files already present in that directory."); print!("\nAre you sure you want to continue? (y/n) "); // Read answer from user and exit if it's not 'yes' - if !confirm() { - println!("\nSkipping...\n"); - println!("All done, no errors..."); - ::std::process::exit(0); + if confirm() { + builder.copy_theme(true); } } - - // Call the function that copies the theme - book.copy_theme()?; - println!("\nTheme copied."); } - // Because of `src/book/mdbook.rs#L37-L39`, `dest` will always start with `root` - let is_dest_inside_root = book.get_destination().starts_with(&book.root); + println!("\nDo you want a .gitignore to be created? (y/n)"); - if !args.is_present("force") && is_dest_inside_root { - println!("\nDo you want a .gitignore to be created? (y/n)"); - - if confirm() { - book.create_gitignore(); - println!("\n.gitignore created."); - } + if confirm() { + builder.create_gitignore(true); } + builder.build()?; println!("\nAll done, no errors..."); Ok(()) diff --git a/src/bin/mdbook.rs b/src/bin/mdbook.rs index 53de436c..4d7fbd6b 100644 --- a/src/bin/mdbook.rs +++ b/src/bin/mdbook.rs @@ -1,17 +1,18 @@ #[macro_use] extern crate clap; extern crate env_logger; +extern crate error_chain; extern crate log; extern crate mdbook; extern crate open; use std::env; use std::ffi::OsStr; -use std::io::{self, Write}; use std::path::{Path, PathBuf}; use clap::{App, AppSettings, ArgMatches}; use log::{LogLevelFilter, LogRecord}; use env_logger::LogBuilder; +use error_chain::ChainedError; pub mod build; pub mod init; @@ -59,7 +60,8 @@ fn main() { }; if let Err(e) = res { - writeln!(&mut io::stderr(), "An error occured:\n{}", e).ok(); + eprintln!("{}", e.display_chain()); + ::std::process::exit(101); } } diff --git a/src/bin/serve.rs b/src/bin/serve.rs index ac1ba144..2768e466 100644 --- a/src/bin/serve.rs +++ b/src/bin/serve.rs @@ -49,7 +49,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> { const RELOAD_COMMAND: &'static str = "reload"; let book_dir = get_book_dir(args); - let mut book = MDBook::new(&book_dir).read_config()?; + let mut book = MDBook::load(&book_dir)?; if let Some(dest_dir) = args.value_of("dest-dir") { book.config.build.build_dir = PathBuf::from(dest_dir); @@ -64,7 +64,8 @@ pub fn execute(args: &ArgMatches) -> Result<()> { let address = format!("{}:{}", interface, port); let ws_address = format!("{}:{}", interface, ws_port); - book.livereload = Some(format!(r#" + book.livereload = Some(format!( + r#"