2018-02-18 15:10:47 +08:00
|
|
|
extern crate chrono;
|
2015-08-01 12:59:05 +08:00
|
|
|
#[macro_use]
|
|
|
|
extern crate clap;
|
2016-08-14 21:55:10 +08:00
|
|
|
extern crate env_logger;
|
2017-12-11 08:42:36 +08:00
|
|
|
extern crate error_chain;
|
2018-01-03 19:32:49 +08:00
|
|
|
#[macro_use]
|
2017-10-03 19:40:23 +08:00
|
|
|
extern crate log;
|
|
|
|
extern crate mdbook;
|
2017-01-02 01:42:47 +08:00
|
|
|
extern crate open;
|
2015-11-09 21:31:00 +08:00
|
|
|
|
2015-07-07 03:12:24 +08:00
|
|
|
use std::env;
|
2017-01-02 01:42:47 +08:00
|
|
|
use std::ffi::OsStr;
|
2015-08-01 12:59:05 +08:00
|
|
|
use std::path::{Path, PathBuf};
|
2017-12-30 18:43:46 +08:00
|
|
|
use std::io::Write;
|
2017-10-03 19:40:23 +08:00
|
|
|
use clap::{App, AppSettings, ArgMatches};
|
2017-12-30 18:43:46 +08:00
|
|
|
use chrono::Local;
|
|
|
|
use log::LevelFilter;
|
|
|
|
use env_logger::Builder;
|
2018-01-07 22:10:48 +08:00
|
|
|
use mdbook::utils;
|
2015-07-07 03:12:24 +08:00
|
|
|
|
2017-06-26 06:53:58 +08:00
|
|
|
pub mod build;
|
2018-02-04 21:00:29 +08:00
|
|
|
pub mod clean;
|
2017-06-26 07:02:32 +08:00
|
|
|
pub mod init;
|
2017-06-26 07:24:33 +08:00
|
|
|
pub mod test;
|
2017-06-26 03:41:23 +08:00
|
|
|
#[cfg(feature = "serve")]
|
|
|
|
pub mod serve;
|
2015-09-27 20:38:37 +08:00
|
|
|
#[cfg(feature = "watch")]
|
2017-06-26 05:44:28 +08:00
|
|
|
pub mod watch;
|
2015-11-09 21:31:00 +08:00
|
|
|
|
2015-07-07 03:12:24 +08:00
|
|
|
const NAME: &'static str = "mdbook";
|
|
|
|
|
|
|
|
fn main() {
|
2017-06-29 05:37:03 +08:00
|
|
|
init_logger();
|
2016-08-14 21:55:10 +08:00
|
|
|
|
2015-08-01 12:59:05 +08:00
|
|
|
// Create a list of valid arguments and sub-commands
|
2017-06-26 06:31:42 +08:00
|
|
|
let app = App::new(NAME)
|
2017-06-26 07:24:33 +08:00
|
|
|
.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(concat!("v",crate_version!()))
|
2018-02-18 15:10:47 +08:00
|
|
|
.setting(AppSettings::ArgRequiredElseHelp)
|
2017-10-03 19:40:23 +08:00
|
|
|
.after_help("For more information about a specific command, \
|
|
|
|
try `mdbook <command> --help`\n\
|
|
|
|
Source code for mdbook available \
|
2017-11-22 18:35:18 +08:00
|
|
|
at: https://github.com/rust-lang-nursery/mdBook")
|
2017-06-26 07:24:33 +08:00
|
|
|
.subcommand(init::make_subcommand())
|
|
|
|
.subcommand(build::make_subcommand())
|
2018-02-04 21:00:29 +08:00
|
|
|
.subcommand(test::make_subcommand())
|
|
|
|
.subcommand(clean::make_subcommand());
|
2017-06-26 06:31:42 +08:00
|
|
|
|
|
|
|
#[cfg(feature = "watch")]
|
|
|
|
let app = app.subcommand(watch::make_subcommand());
|
|
|
|
#[cfg(feature = "serve")]
|
|
|
|
let app = app.subcommand(serve::make_subcommand());
|
2015-08-01 12:59:05 +08:00
|
|
|
|
|
|
|
// Check which subcomamnd the user ran...
|
2017-06-26 06:31:42 +08:00
|
|
|
let res = match app.get_matches().subcommand() {
|
2017-06-27 13:59:50 +08:00
|
|
|
("init", Some(sub_matches)) => init::execute(sub_matches),
|
|
|
|
("build", Some(sub_matches)) => build::execute(sub_matches),
|
2018-02-04 21:00:29 +08:00
|
|
|
("clean", Some(sub_matches)) => clean::execute(sub_matches),
|
2015-09-27 20:38:37 +08:00
|
|
|
#[cfg(feature = "watch")]
|
2017-06-27 13:59:50 +08:00
|
|
|
("watch", Some(sub_matches)) => watch::execute(sub_matches),
|
2016-04-02 10:46:05 +08:00
|
|
|
#[cfg(feature = "serve")]
|
2017-06-27 13:59:50 +08:00
|
|
|
("serve", Some(sub_matches)) => serve::execute(sub_matches),
|
|
|
|
("test", Some(sub_matches)) => test::execute(sub_matches),
|
2016-03-18 05:31:28 +08:00
|
|
|
(_, _) => unreachable!(),
|
2015-07-07 03:12:24 +08:00
|
|
|
};
|
|
|
|
|
2015-08-01 12:59:05 +08:00
|
|
|
if let Err(e) = res {
|
2018-01-07 22:10:48 +08:00
|
|
|
utils::log_backtrace(&e);
|
2017-12-11 08:42:36 +08:00
|
|
|
|
2016-08-07 02:54:07 +08:00
|
|
|
::std::process::exit(101);
|
2015-07-07 03:12:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-29 05:37:03 +08:00
|
|
|
fn init_logger() {
|
2017-12-30 18:43:46 +08:00
|
|
|
let mut builder = Builder::new();
|
2017-06-29 05:37:03 +08:00
|
|
|
|
2017-12-30 18:43:46 +08:00
|
|
|
builder.format(|formatter, record| {
|
2018-02-18 15:10:47 +08:00
|
|
|
writeln!(
|
|
|
|
formatter,
|
|
|
|
"{} [{}] ({}): {}",
|
|
|
|
Local::now().format("%Y-%m-%d %H:%M:%S"),
|
|
|
|
record.level(),
|
|
|
|
record.target(),
|
|
|
|
record.args()
|
|
|
|
)
|
2017-12-30 18:43:46 +08:00
|
|
|
});
|
2017-06-29 05:37:03 +08:00
|
|
|
|
|
|
|
if let Ok(var) = env::var("RUST_LOG") {
|
2017-10-03 19:40:23 +08:00
|
|
|
builder.parse(&var);
|
2017-12-30 18:43:46 +08:00
|
|
|
} else {
|
|
|
|
// if no RUST_LOG provided, default to logging at the Info level
|
|
|
|
builder.filter(None, LevelFilter::Info);
|
2017-06-29 05:37:03 +08:00
|
|
|
}
|
|
|
|
|
2017-12-30 18:43:46 +08:00
|
|
|
builder.init();
|
2017-06-29 05:37:03 +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() {
|
2016-03-18 05:31:28 +08:00
|
|
|
env::current_dir().unwrap().join(dir)
|
2015-08-01 12:59:05 +08:00
|
|
|
} else {
|
2016-03-18 05:31:28 +08:00
|
|
|
p.to_path_buf()
|
2015-08-01 12:59:05 +08:00
|
|
|
}
|
2015-07-18 06:04:20 +08:00
|
|
|
} else {
|
2018-01-07 22:10:48 +08:00
|
|
|
env::current_dir().expect("Unable to determine the current directory")
|
2015-07-07 03:12:24 +08:00
|
|
|
}
|
|
|
|
}
|
2016-04-02 10:46:05 +08:00
|
|
|
|
2017-01-02 01:42:47 +08:00
|
|
|
fn open<P: AsRef<OsStr>>(path: P) {
|
|
|
|
if let Err(e) = open::that(path) {
|
2018-01-07 22:10:48 +08:00
|
|
|
error!("Error opening web browser: {}", e);
|
2017-01-02 01:42:47 +08:00
|
|
|
}
|
|
|
|
}
|