From 00c58faf1a83c683ede0f570d5c023d9f4f23faa Mon Sep 17 00:00:00 2001 From: Kenton Hamaluik Date: Thu, 28 Nov 2019 13:11:22 -0700 Subject: [PATCH] fleshed out init command --- assets/01-introduction.default.md | 7 +++++++ assets/mkbook.default.toml | 6 ++++++ src/cli.rs | 8 ++++---- src/main.rs | 25 +++++++++++++++++++++++-- 4 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 assets/01-introduction.default.md create mode 100644 assets/mkbook.default.toml diff --git a/assets/01-introduction.default.md b/assets/01-introduction.default.md new file mode 100644 index 0000000..6683295 --- /dev/null +++ b/assets/01-introduction.default.md @@ -0,0 +1,7 @@ +--- +title = "Introduction" +--- + +# Introduction + +This is my cool book, I made it all by myself (along with [mkbook](https://github.com/hamaluik/mkbook), that is!) diff --git a/assets/mkbook.default.toml b/assets/mkbook.default.toml new file mode 100644 index 0000000..15227c5 --- /dev/null +++ b/assets/mkbook.default.toml @@ -0,0 +1,6 @@ +title = "My Cool Book" +author = "Anonymous" +url = "" +description = """ +A **short** but sweet description of _My Cool Book_. +""" diff --git a/src/cli.rs b/src/cli.rs index a3a3122..6290a77 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -5,13 +5,13 @@ pub fn build_cli() -> App<'static, 'static> { .version(env!("CARGO_PKG_VERSION")) .author(env!("CARGO_PKG_AUTHORS")) .about(env!("CARGO_PKG_DESCRIPTION")) - .subcommand(SubCommand::with_name("init") - .about("initialized the directory structi") - ) .subcommand(SubCommand::with_name("init") .about("initialize a mkbook directory tree") .arg(Arg::with_name("directory") - .help("an optional directory to initialize into (defaults to the CWD)") + .short("d") + .long("directory") + .default_value("src") + .help("an optional directory to initialize into") ) ) .subcommand(SubCommand::with_name("build") diff --git a/src/main.rs b/src/main.rs index ae292ae..bc03be1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,8 @@ use askama::Template; pub const STYLESHEET: &'static str = include_str!(concat!(env!("OUT_DIR"), "/style.css")); pub const ASSET_FAVICON: &'static [u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/assets/favicon.ico")); pub const ASSET_ICONS: &'static [u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/assets/icons.svg")); +pub const ASSET_DEFAULT_MKBOOK: &'static [u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/assets/mkbook.default.toml")); +pub const ASSET_DEFAULT_INTRODUCTION: &'static [u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/assets/01-introduction.default.md")); pub const SYNTAX_TOML: &'static str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/assets/syntaxes/TOML.sublime-syntax")); pub const SYNTAX_HAXE: &'static str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/assets/syntaxes/haxe.sublime-syntax")); @@ -228,8 +230,27 @@ fn format_page(book: &Book, frontmatter: FrontMatter, chapters: &V fn main() -> Result<(), Box> { let matches = cli::build_cli().get_matches(); - if let Some(_submatches) = matches.subcommand_matches("init") { - unimplemented!() + if let Some(submatches) = matches.subcommand_matches("init") { + let dest = submatches.value_of("directory").expect("directory value"); + let dest = PathBuf::from(dest); + + println!("Initializing a book into {}...", dest.display()); + fs::create_dir_all(&dest)?; + let book_toml_path = dest.join("mkbook.toml"); + fs::write(&book_toml_path, ASSET_DEFAULT_MKBOOK)?; + let intro_path = dest.join("01-introduction.md"); + fs::write(&intro_path, ASSET_DEFAULT_INTRODUCTION)?; + println!("Done!"); + + println!("You can now build your book by running:"); + if dest.display().to_string() != "src" { + println!("mkbook build -i {}", dest.display()); + } + else { + println!("mkbook build"); + } + + Ok(()) } else if let Some(submatches) = matches.subcommand_matches("build") { let src = submatches.value_of("in").expect("in value");