From 870fbbcd0f9b28e96d6d80b2c2f54ce955218a92 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Sat, 25 Mar 2017 13:18:15 +0900 Subject: [PATCH] Allow selecting a config file other than config.toml --- Cargo.lock | 29 ++++++++++++++--------------- src/cmd/build.rs | 4 ++-- src/cmd/serve.rs | 4 ++-- src/config.rs | 6 +++--- src/lib.rs | 2 +- src/main.rs | 7 +++++-- src/site.rs | 4 ++-- test_site/config.staging.toml | 7 +++++++ tests/site.rs | 10 +++++----- 9 files changed, 41 insertions(+), 32 deletions(-) create mode 100644 test_site/config.staging.toml diff --git a/Cargo.lock b/Cargo.lock index 7b48605..78eab5e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,14 +3,14 @@ name = "gutenberg" version = "0.0.2" dependencies = [ "chrono 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "clap 2.22.0 (registry+https://github.com/rust-lang/crates.io-index)", + "clap 2.22.1 (registry+https://github.com/rust-lang/crates.io-index)", "error-chain 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "iron 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", "mount 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "notify 4.0.0 (registry+https://github.com/rust-lang/crates.io-index)", - "pulldown-cmark 0.0.12 (registry+https://github.com/rust-lang/crates.io-index)", + "notify 4.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "pulldown-cmark 0.0.14 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "serde 0.9.11 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 0.9.11 (registry+https://github.com/rust-lang/crates.io-index)", @@ -93,7 +93,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "bitflags" -version = "0.8.0" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -145,12 +145,12 @@ dependencies = [ [[package]] name = "clap" -version = "2.22.0" +version = "2.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "atty 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "bitflags 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", "strsim 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "term_size 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "unicode-segmentation 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -493,7 +493,7 @@ dependencies = [ [[package]] name = "notify" -version = "4.0.0" +version = "4.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -504,7 +504,6 @@ dependencies = [ "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)", "mio 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", - "time 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)", "walkdir 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -602,10 +601,10 @@ dependencies = [ [[package]] name = "pulldown-cmark" -version = "0.0.12" +version = "0.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "bitflags 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", "getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -783,7 +782,7 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bincode 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", - "bitflags 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", "flate2 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1043,7 +1042,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum bincode 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "55eb0b7fd108527b0c77860f75eca70214e11a8b4c6ef05148c54c05a25d48ad" "checksum bitflags 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8dead7461c1127cf637931a1e50934eb6eee8bff2f74433ac7909e9afcee04a3" "checksum bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aad18937a628ec6abcd26d1489012cc0e18c21798210f491af69ded9b881106d" -"checksum bitflags 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e1ab483fc81a8143faa7203c4a3c02888ebd1a782e37e41fa34753ba9a162" +"checksum bitflags 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1370e9fc2a6ae53aea8b7a5110edbd08836ed87c88736dfabccade1c2b44bff4" "checksum byteorder 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0fc10e8cc6b2580fda3f36eb6dc5316657f812a3df879a44a66fc9f0fdbc4855" "checksum byteorder 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c40977b0ee6b9885c9013cd41d9feffdd22deb3bb4dc3a71d901cc7a77de18c8" "checksum bytes 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c129aff112dcc562970abb69e2508b40850dd24c274761bb50fb8a0067ba6c27" @@ -1051,7 +1050,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "de1e760d7b6535af4241fca8bd8adf68e2e7edacc6b29f5d399050c5e48cf88c" "checksum chrono 0.2.25 (registry+https://github.com/rust-lang/crates.io-index)" = "9213f7cd7c27e95c2b57c49f0e69b1ea65b27138da84a170133fd21b07659c00" "checksum chrono 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "158b0bd7d75cbb6bf9c25967a48a2e9f77da95876b858eadfabaa99cd069de6e" -"checksum clap 2.22.0 (registry+https://github.com/rust-lang/crates.io-index)" = "875f4f161166a71c4b7bf2c05f46d26c30d8b9052c16a6bdd3171ddf03848cbe" +"checksum clap 2.22.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e17a4a72ffea176f77d6e2db609c6c919ef221f23862c9915e687fb54d833485" "checksum cmake 0.1.22 (registry+https://github.com/rust-lang/crates.io-index)" = "d18d68987ed4c516dcc3e7913659bfa4076f5182eea4a7e0038bb060953e76ac" "checksum conduit-mime-types 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "95ca30253581af809925ef68c2641cc140d6183f43e12e0af4992d53768bd7b8" "checksum dbghelp-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "97590ba53bcb8ac28279161ca943a924d1fd4a8fb3fa63302591647c4fc5b850" @@ -1092,7 +1091,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum mount 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "32245731923cd096899502fc4c4317cfd09f121e80e73f7f576cf3777a824256" "checksum net2 0.2.27 (registry+https://github.com/rust-lang/crates.io-index)" = "18b9642ad6222faf5ce46f6966f59b71b9775ad5758c9e09fcf0a6c8061972b4" "checksum nix 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "bfb3ddedaa14746434a02041940495bf11325c22f6d36125d3bdd56090d50a79" -"checksum notify 4.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "87c628262cc8a99209596ec610dcf199d6876a10cfa22e355acc5936d6f1fa63" +"checksum notify 4.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "298d4401ff2c6cebb7f8944c90288647c89ce59029d43b439444cf1067df55e1" "checksum num 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "98b15ba84e910ea7a1973bccd3df7b31ae282bf9d8bd2897779950c9b8303d40" "checksum num-integer 0.1.33 (registry+https://github.com/rust-lang/crates.io-index)" = "21e4df1098d1d797d27ef0c69c178c3fab64941559b290fcae198e0825c9c8b5" "checksum num-iter 0.1.33 (registry+https://github.com/rust-lang/crates.io-index)" = "f7d1891bd7b936f12349b7d1403761c8a0b85a18b148e9da4429d5d102c1a41e" @@ -1104,7 +1103,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "3a8b4c6b8165cd1a1cd4b9b120978131389f64bdaf456435caa41e630edba903" "checksum plist 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5d6ab9bef2781bcdac1baf3e29eb297344cd24263e22fd9436d3a21215b7d8aa" "checksum plugin 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "1a6a0dc3910bc8db877ffed8e457763b317cf880df4ae19109b9f77d277cf6e0" -"checksum pulldown-cmark 0.0.12 (registry+https://github.com/rust-lang/crates.io-index)" = "151e9b7f560c0c483b8d29e9628fd6837f28795e8bc8a5ac465843e5cd108c43" +"checksum pulldown-cmark 0.0.14 (registry+https://github.com/rust-lang/crates.io-index)" = "d9ab1e588ef8efd702c7ed9d2bd774db5e6f4d878bb5a1a9f371828fbdff6973" "checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" "checksum rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d" "checksum redox_syscall 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)" = "29dbdfd4b9df8ab31dec47c6087b7b13cbf4a776f335e4de8efba8288dda075b" diff --git a/src/cmd/build.rs b/src/cmd/build.rs index 9893a18..bb3aaeb 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -4,8 +4,8 @@ use gutenberg::errors::Result; use gutenberg::Site; -pub fn build() -> Result<()> { - let mut site = Site::new(env::current_dir().unwrap())?; +pub fn build(config_file: &str) -> Result<()> { + let mut site = Site::new(env::current_dir().unwrap(), config_file)?; site.load()?; site.build() } diff --git a/src/cmd/serve.rs b/src/cmd/serve.rs index 8370175..4fa2111 100644 --- a/src/cmd/serve.rs +++ b/src/cmd/serve.rs @@ -58,10 +58,10 @@ fn rebuild_done_handling(broadcaster: &Sender, res: Result<()>, reload_path: &st // Most of it taken from mdbook -pub fn serve(interface: &str, port: &str) -> Result<()> { +pub fn serve(interface: &str, port: &str, config_file: &str) -> Result<()> { println!("Building site..."); let start = Instant::now(); - let mut site = Site::new(env::current_dir().unwrap())?; + let mut site = Site::new(env::current_dir().unwrap(), config_file)?; let address = format!("{}:{}", interface, port); // Override the base url so links work in localhost site.config.base_url = if site.config.base_url.ends_with('/') { diff --git a/src/config.rs b/src/config.rs index 5f24bae..0f9a737 100644 --- a/src/config.rs +++ b/src/config.rs @@ -112,11 +112,11 @@ impl Default for Config { /// Get and parse the config. /// If it doesn't succeed, exit -pub fn get_config(path: &Path) -> Config { - match Config::from_file(path.join("config.toml")) { +pub fn get_config(path: &Path, filename: &str) -> Config { + match Config::from_file(path.join(filename)) { Ok(c) => c, Err(e) => { - println!("Failed to load config.toml"); + println!("Failed to load {}", filename); println!("Error: {}", e); ::std::process::exit(1); } diff --git a/src/lib.rs b/src/lib.rs index 75f1ecc..2217399 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,7 +27,7 @@ mod markdown; mod section; pub use site::Site; -pub use config::Config; +pub use config::{Config, get_config}; pub use front_matter::{FrontMatter, split_content}; pub use page::{Page, populate_previous_and_next_pages}; pub use section::{Section}; diff --git a/src/main.rs b/src/main.rs index b0237a0..185ad5b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,6 +37,7 @@ fn main() { (author: "Vincent Prouillet") (about: "Static site generator") (@setting SubcommandRequiredElseHelp) + (@arg config: -c --config +takes_value "Path to a config file other than config.toml") (@subcommand init => (about: "Create a new Gutenberg project") (@arg name: +required "Name of the project. Will create a directory with that name in the current directory") @@ -51,6 +52,8 @@ fn main() { ) ).get_matches(); + let config_file = matches.value_of("config").unwrap_or("config.toml"); + match matches.subcommand() { ("init", Some(matches)) => { match cmd::create_new_project(matches.value_of("name").unwrap()) { @@ -66,7 +69,7 @@ fn main() { ("build", Some(_)) => { println!("Building site"); let start = Instant::now(); - match cmd::build() { + match cmd::build(&config_file) { Ok(()) => { report_elapsed_time(start); }, @@ -83,7 +86,7 @@ fn main() { ("serve", Some(matches)) => { let interface = matches.value_of("interface").unwrap_or("127.0.0.1"); let port = matches.value_of("port").unwrap_or("1111"); - match cmd::serve(interface, port) { + match cmd::serve(interface, port, &config_file) { Ok(()) => (), Err(e) => { println!("Error: {}", e); diff --git a/src/site.rs b/src/site.rs index 52bb76e..638e2e9 100644 --- a/src/site.rs +++ b/src/site.rs @@ -68,7 +68,7 @@ pub struct Site { impl Site { /// Parse a site at the given path. Defaults to the current dir /// Passing in a path is only used in tests - pub fn new>(path: P) -> Result { + pub fn new>(path: P, config_file: &str) -> Result { let path = path.as_ref(); let tpl_glob = format!("{}/{}", path.to_string_lossy().replace("\\", "/"), "templates/**/*"); @@ -77,7 +77,7 @@ impl Site { let site = Site { base_path: path.to_path_buf(), - config: get_config(path), + config: get_config(path, config_file), pages: HashMap::new(), sections: BTreeMap::new(), templates: tera, diff --git a/test_site/config.staging.toml b/test_site/config.staging.toml new file mode 100644 index 0000000..123bc89 --- /dev/null +++ b/test_site/config.staging.toml @@ -0,0 +1,7 @@ +title = "My site" +base_url = "https://staging.com" +highlight_code = true + + +[extra.author] +name = "Vincent Prouillet" diff --git a/tests/site.rs b/tests/site.rs index 963592f..9f2cbff 100644 --- a/tests/site.rs +++ b/tests/site.rs @@ -15,7 +15,7 @@ use gutenberg::{Site}; fn test_can_parse_site() { let mut path = env::current_dir().unwrap().to_path_buf(); path.push("test_site"); - let mut site = Site::new(&path).unwrap(); + let mut site = Site::new(&path, "config.toml").unwrap(); site.load().unwrap(); // Correct number of pages (sections are pages too) @@ -88,7 +88,7 @@ macro_rules! file_contains { fn test_can_build_site_without_live_reload() { let mut path = env::current_dir().unwrap().to_path_buf(); path.push("test_site"); - let mut site = Site::new(&path).unwrap(); + let mut site = Site::new(&path, "config.toml").unwrap(); site.load().unwrap(); let tmp_dir = TempDir::new("example").expect("create temp dir"); let public = &tmp_dir.path().join("public"); @@ -129,7 +129,7 @@ fn test_can_build_site_without_live_reload() { fn test_can_build_site_with_live_reload() { let mut path = env::current_dir().unwrap().to_path_buf(); path.push("test_site"); - let mut site = Site::new(&path).unwrap(); + let mut site = Site::new(&path, "config.toml").unwrap(); site.load().unwrap(); let tmp_dir = TempDir::new("example").expect("create temp dir"); let public = &tmp_dir.path().join("public"); @@ -167,7 +167,7 @@ fn test_can_build_site_with_live_reload() { fn test_can_build_site_with_categories() { let mut path = env::current_dir().unwrap().to_path_buf(); path.push("test_site"); - let mut site = Site::new(&path).unwrap(); + let mut site = Site::new(&path, "config.toml").unwrap(); site.load().unwrap(); for (i, page) in site.pages.values_mut().enumerate() { @@ -218,7 +218,7 @@ fn test_can_build_site_with_categories() { fn test_can_build_site_with_tags() { let mut path = env::current_dir().unwrap().to_path_buf(); path.push("test_site"); - let mut site = Site::new(&path).unwrap(); + let mut site = Site::new(&path, "config.toml").unwrap(); site.load().unwrap(); for (i, page) in site.pages.values_mut().enumerate() {