2015-07-07 03:12:24 +08:00
|
|
|
extern crate mdbook;
|
2015-08-01 12:59:05 +08:00
|
|
|
#[macro_use]
|
|
|
|
extern crate clap;
|
|
|
|
|
2015-07-07 03:12:24 +08:00
|
|
|
use std::env;
|
2015-08-01 12:59:05 +08:00
|
|
|
use std::error::Error;
|
|
|
|
use std::io::{self, Write};
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
|
|
|
use clap::{App, ArgMatches, SubCommand};
|
2015-07-07 03:12:24 +08:00
|
|
|
|
2015-07-07 08:56:19 +08:00
|
|
|
use mdbook::MDBook;
|
2015-07-07 03:12:24 +08:00
|
|
|
|
|
|
|
const NAME: &'static str = "mdbook";
|
|
|
|
|
|
|
|
fn main() {
|
2015-08-01 12:59:05 +08:00
|
|
|
// Create a list of valid arguments and sub-commands
|
|
|
|
let matches = App::new(NAME)
|
|
|
|
.about("Create a book in form of a static website from markdown files")
|
|
|
|
.author("Mathieu David <mathieudavid@mathieudavid.org>")
|
|
|
|
// Get the version from our Cargo.toml using clap's crate_version!() macro
|
|
|
|
.version(&*format!("v{}", crate_version!()))
|
|
|
|
.subcommand_required(true)
|
|
|
|
.after_help("For more information about a specific command, try `mdbook <command> --help`")
|
|
|
|
.subcommand(SubCommand::with_name("init")
|
|
|
|
.about("Create boilerplate structure and files in the directory")
|
|
|
|
// the {n} denotes a newline which will properly aligned in all help
|
|
|
|
// messages
|
|
|
|
.arg_from_usage("[dir] 'A directory for your book{n}(Defaults to Current Directory when ommitted)'"))
|
|
|
|
.subcommand(SubCommand::with_name("build")
|
|
|
|
.about("Build the book from the markdown files")
|
|
|
|
.arg_from_usage("[dir] 'A directory for your book{n}(Defaults to Current Directory when ommitted)'"))
|
|
|
|
.subcommand(SubCommand::with_name("watch")
|
|
|
|
.about("Watch the files for changes"))
|
|
|
|
.get_matches();
|
|
|
|
|
|
|
|
// Check which subcomamnd the user ran...
|
|
|
|
let res = match matches.subcommand() {
|
|
|
|
("init", Some(sub_matches)) => init(sub_matches),
|
|
|
|
("build", Some(sub_matches)) => build(sub_matches),
|
|
|
|
("watch", _) => unimplemented!(),
|
|
|
|
(_, _) => unreachable!()
|
2015-07-07 03:12:24 +08:00
|
|
|
};
|
|
|
|
|
2015-08-01 12:59:05 +08:00
|
|
|
if let Err(e) = res {
|
|
|
|
writeln!(&mut io::stderr(), "Error: {}", e).ok();
|
2015-07-07 03:12:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-01 12:59:05 +08:00
|
|
|
fn init(args: &ArgMatches) -> Result<(), Box<Error>> {
|
|
|
|
let book_dir = get_book_dir(args);
|
|
|
|
let book = MDBook::new(&book_dir);
|
2015-07-07 03:12:24 +08:00
|
|
|
|
2015-08-01 12:59:05 +08:00
|
|
|
book.init()
|
2015-07-07 03:12:24 +08:00
|
|
|
}
|
|
|
|
|
2015-08-01 12:59:05 +08:00
|
|
|
fn build(args: &ArgMatches) -> Result<(), Box<Error>> {
|
|
|
|
let book_dir = get_book_dir(args);
|
2015-08-03 07:37:13 +08:00
|
|
|
let mut book = MDBook::new(&book_dir).read_config();
|
2015-07-07 08:56:19 +08:00
|
|
|
|
2015-08-01 12:59:05 +08:00
|
|
|
book.build()
|
2015-07-07 03:12:24 +08:00
|
|
|
}
|
|
|
|
|
2015-08-01 12:59:05 +08:00
|
|
|
fn get_book_dir(args: &ArgMatches) -> PathBuf {
|
|
|
|
if let Some(dir) = args.value_of("dir") {
|
|
|
|
// Check if path is relative from current dir, or absolute...
|
|
|
|
let p = Path::new(dir);
|
|
|
|
if p.is_relative() {
|
|
|
|
env::current_dir().unwrap().join(dir)
|
|
|
|
} else {
|
|
|
|
p.to_path_buf()
|
|
|
|
}
|
2015-07-18 06:04:20 +08:00
|
|
|
} else {
|
2015-08-01 12:59:05 +08:00
|
|
|
env::current_dir().unwrap()
|
2015-07-07 03:12:24 +08:00
|
|
|
}
|
|
|
|
}
|