diff --git a/components/config/src/config/mod.rs b/components/config/src/config/mod.rs index a23e3825..b33b8c79 100644 --- a/components/config/src/config/mod.rs +++ b/components/config/src/config/mod.rs @@ -96,6 +96,8 @@ pub struct Config { #[serde(skip_serializing, skip_deserializing)] // not a typo, 2 are need pub extra_syntax_set: Option, + pub output_dir: String, + pub link_checker: link_checker::LinkChecker, /// The setup for which slugification strategies to use for paths, taxonomies and anchors @@ -333,6 +335,7 @@ impl Default for Config { translations: HashMap::new(), extra_syntaxes: Vec::new(), extra_syntax_set: None, + output_dir: "public".to_string(), link_checker: link_checker::LinkChecker::default(), slugify: slugify::Slugify::default(), search: search::Search::default(), @@ -654,4 +657,27 @@ bar = "baz" // We expect an error here assert_eq!(false, config.add_theme_extra(&theme).is_ok()); } + + #[test] + fn default_output_dir() { + let config = r#" +title = "My site" +base_url = "https://replace-this-with-your-url.com" + "#; + + let config = Config::parse(config).unwrap(); + assert_eq!(config.output_dir, "public".to_string()); + } + + #[test] + fn can_add_output_dir() { + let config = r#" +title = "My site" +base_url = "https://replace-this-with-your-url.com" +output_dir = "docs" + "#; + + let config = Config::parse(config).unwrap(); + assert_eq!(config.output_dir, "docs".to_string()); + } } diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index 7d06b27a..d5bc5de5 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -85,7 +85,7 @@ impl Site { let static_path = path.join("static"); let imageproc = imageproc::Processor::new(content_path.clone(), &static_path, &config.base_url); - let output_path = path.join("public"); + let output_path = path.join(config.output_dir.clone()); let site = Site { base_path: path.to_path_buf(), diff --git a/docs/content/documentation/getting-started/configuration.md b/docs/content/documentation/getting-started/configuration.md index 96ad924f..588bb736 100644 --- a/docs/content/documentation/getting-started/configuration.md +++ b/docs/content/documentation/getting-started/configuration.md @@ -96,6 +96,9 @@ ignored_content = [] # A list of directories used to search for additional `.sublime-syntax` files. extra_syntaxes = [] +# You can override the default output directory `public` by setting an another value. +# output_dir = "docs" + # Configuration of the link checker. [link_checker] # Skip link checking for external URLs that start with these prefixes diff --git a/src/cli.rs b/src/cli.rs index bc14a5ab..c76f56dd 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -44,7 +44,6 @@ pub fn build_cli() -> App<'static, 'static> { Arg::with_name("output_dir") .short("o") .long("output-dir") - .default_value("public") .takes_value(true) .help("Outputs the generated site in the given path"), Arg::with_name("drafts") @@ -68,7 +67,6 @@ pub fn build_cli() -> App<'static, 'static> { Arg::with_name("output_dir") .short("o") .long("output-dir") - .default_value("public") .takes_value(true) .help("Outputs the generated site in the given path"), Arg::with_name("base_url") diff --git a/src/cmd/build.rs b/src/cmd/build.rs index 3a2c7634..7a0e85cb 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -9,11 +9,13 @@ pub fn build( root_dir: &Path, config_file: &Path, base_url: Option<&str>, - output_dir: &Path, + output_dir: Option<&Path>, include_drafts: bool, ) -> Result<()> { let mut site = Site::new(root_dir, config_file)?; - site.set_output_path(output_dir); + if let Some(output_dir) = output_dir { + site.set_output_path(output_dir); + } if let Some(b) = base_url { site.set_base_url(b.to_string()); } diff --git a/src/cmd/serve.rs b/src/cmd/serve.rs index 2f2f299a..899d2570 100644 --- a/src/cmd/serve.rs +++ b/src/cmd/serve.rs @@ -22,11 +22,11 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. use std::fs::{read_dir, remove_dir_all}; +use std::net::{SocketAddrV4, TcpListener}; use std::path::{Path, PathBuf}; use std::sync::mpsc::channel; use std::thread; use std::time::{Duration, Instant}; -use std::net::{SocketAddrV4, TcpListener}; use hyper::header; use hyper::service::{make_service_fn, service_fn}; @@ -173,7 +173,7 @@ fn create_new_site( root_dir: &Path, interface: &str, interface_port: u16, - output_dir: &Path, + output_dir: Option<&Path>, base_url: &str, config_file: &Path, include_drafts: bool, @@ -192,7 +192,9 @@ fn create_new_site( site.enable_serve_mode(); site.set_base_url(base_url); - site.set_output_path(output_dir); + if let Some(output_dir) = output_dir { + site.set_output_path(output_dir); + } if include_drafts { site.include_drafts(); } @@ -212,7 +214,7 @@ pub fn serve( root_dir: &Path, interface: &str, interface_port: u16, - output_dir: &Path, + output_dir: Option<&Path>, base_url: &str, config_file: &Path, watch_only: bool, @@ -277,7 +279,7 @@ pub fn serve( let ws_port = site.live_reload; let ws_address = format!("{}:{}", interface, ws_port.unwrap()); - let output_path = Path::new(output_dir).to_path_buf(); + let output_path = site.output_path.clone(); // output path is going to need to be moved later on, so clone it for the // http closure to avoid contention. diff --git a/src/main.rs b/src/main.rs index 3eec3ba3..2936354c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use std::env; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::time::Instant; use utils::net::{get_available_port, port_is_available}; @@ -37,12 +37,12 @@ fn main() { ("build", Some(matches)) => { console::info("Building site..."); let start = Instant::now(); - let output_dir = PathBuf::from(matches.value_of("output_dir").unwrap()); + let output_dir = matches.value_of("output_dir").map(|output_dir| Path::new(output_dir)); match cmd::build( &root_dir, &config_file, matches.value_of("base_url"), - &output_dir, + output_dir, matches.is_present("drafts"), ) { Ok(()) => console::report_elapsed_time(start), @@ -80,14 +80,14 @@ fn main() { ::std::process::exit(1); } } - let output_dir = PathBuf::from(matches.value_of("output_dir").unwrap()); + let output_dir = matches.value_of("output_dir").map(|output_dir| Path::new(output_dir)); let base_url = matches.value_of("base_url").unwrap(); console::info("Building site..."); match cmd::serve( &root_dir, interface, port, - &output_dir, + output_dir, base_url, &config_file, watch_only,