From eb6fa3c3140f03dd29bb58954f8f9676326872da Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Mon, 20 Mar 2017 19:00:00 +0900 Subject: [PATCH] Override base_url in serve cmd --- src/cmd/build.rs | 4 +++- src/cmd/serve.rs | 10 +++++++++- src/site.rs | 3 +-- tests/site.rs | 7 ++++++- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/cmd/build.rs b/src/cmd/build.rs index ec581194..521ecd06 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -5,5 +5,7 @@ use gutenberg::Site; pub fn build() -> Result<()> { - Site::new(env::current_dir().unwrap())?.build() + let mut site = Site::new(env::current_dir().unwrap())?; + site.parse()?; + site.build() } diff --git a/src/cmd/serve.rs b/src/cmd/serve.rs index 0c83639e..49f1578d 100644 --- a/src/cmd/serve.rs +++ b/src/cmd/serve.rs @@ -62,11 +62,19 @@ pub fn serve(interface: &str, port: &str) -> Result<()> { println!("Building site..."); let start = Instant::now(); let mut site = Site::new(env::current_dir().unwrap())?; + 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('/') { + format!("http://{}/", address) + } else { + format!("http://{}", address) + }; + + site.parse()?; site.enable_live_reload(); site.build()?; report_elapsed_time(start); - let address = format!("{}:{}", interface, port); let ws_address = format!("{}:{}", interface, "1112"); // Start a webserver that serves the `public` directory diff --git a/src/site.rs b/src/site.rs index cdb09225..f2e4443e 100644 --- a/src/site.rs +++ b/src/site.rs @@ -74,7 +74,7 @@ impl Site { let mut tera = Tera::new(&tpl_glob).chain_err(|| "Error parsing templates")?; tera.extend(&GUTENBERG_TERA)?; - let mut site = Site { + let site = Site { base_path: path.to_path_buf(), config: get_config(path), pages: HashMap::new(), @@ -85,7 +85,6 @@ impl Site { tags: HashMap::new(), categories: HashMap::new(), }; - site.parse()?; Ok(site) } diff --git a/tests/site.rs b/tests/site.rs index 3df6926e..1c9d9d1b 100644 --- a/tests/site.rs +++ b/tests/site.rs @@ -16,7 +16,8 @@ use gutenberg::{Site}; fn test_can_parse_site() { let mut path = env::current_dir().unwrap().to_path_buf(); path.push("test_site"); - let site = Site::new(&path).unwrap(); + let mut site = Site::new(&path).unwrap(); + site.parse().unwrap(); // Correct number of pages (sections are pages too) assert_eq!(site.pages.len(), 10); @@ -88,6 +89,7 @@ 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(); + site.parse().unwrap(); let tmp_dir = TempDir::new("example").expect("create temp dir"); let public = &tmp_dir.path().join("public"); site.set_output_path(&public); @@ -127,6 +129,7 @@ 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(); + site.parse().unwrap(); let tmp_dir = TempDir::new("example").expect("create temp dir"); let public = &tmp_dir.path().join("public"); site.set_output_path(&public); @@ -163,6 +166,7 @@ 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(); + site.parse().unwrap(); for (i, page) in site.pages.values_mut().enumerate() { page.meta.category = if i % 2 == 0 { @@ -212,6 +216,7 @@ 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(); + site.parse().unwrap(); for (i, page) in site.pages.values_mut().enumerate() { page.meta.tags = if i % 2 == 0 {