diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index 33360fa..d99fa6a 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -11,7 +11,12 @@ use gutenberg::Site; use console::warn; fn notify_site_size(site: &Site) { - println!("-> Creating {} pages and {} sections", site.pages.len(), site.sections.len()); + println!( + "-> Creating {} pages ({} orphan) and {} sections", + site.pages.len(), + site.get_all_orphan_pages().len(), + site.sections.len() + ); } fn warn_about_ignored_pages(site: &Site) { diff --git a/src/page.rs b/src/page.rs index 06d83e7..c63f63c 100644 --- a/src/page.rs +++ b/src/page.rs @@ -143,23 +143,27 @@ impl Page { // 4. Find sections // Pages with custom urls exists outside of sections + let mut path_set = false; if let Some(ref u) = page.meta.url { page.path = u.trim().to_string(); - } else if !page.components.is_empty() { + path_set = true; + } + + if !page.components.is_empty() { // If we have a folder with an asset, don't consider it as a component if page.file_name == "index" { page.components.pop(); // also set parent_path to grandparent instead page.parent_path = page.parent_path.parent().unwrap().to_path_buf(); } - - // Don't add a trailing slash to sections - page.path = format!("{}/{}", page.components.join("/"), page.slug); - } else { + if !path_set { + // Don't add a trailing slash to sections + page.path = format!("{}/{}", page.components.join("/"), page.slug); + } + } else if !path_set { page.path = page.slug.clone(); } - page.permalink = config.make_permalink(&page.path); Ok(page) diff --git a/src/site.rs b/src/site.rs index 5cd9127..27d2373 100644 --- a/src/site.rs +++ b/src/site.rs @@ -122,6 +122,7 @@ impl Site { self.live_reload = true; } + /// Gets the path of all ignored pages in the site pub fn get_ignored_pages(&self) -> Vec { self.sections .values() @@ -129,6 +130,24 @@ impl Site { .collect() } + /// Get all the orphan (== without section) pages in the site + pub fn get_all_orphan_pages(&self) -> Vec<&Page> { + let mut pages_in_sections = vec![]; + let mut orphans = vec![]; + + for s in self.sections.values() { + pages_in_sections.extend(s.all_pages_path()); + } + + for page in self.pages.values() { + if !pages_in_sections.contains(&page.file_path) { + orphans.push(page); + } + } + + orphans + } + /// Used by tests to change the output path to a tmp dir #[doc(hidden)] pub fn set_output_path>(&mut self, path: P) { @@ -589,15 +608,9 @@ impl Site { /// Renders all pages that do not belong to any sections fn render_orphan_pages(&self) -> Result<()> { self.ensure_public_directory_exists()?; - let mut pages_in_sections = vec![]; - for s in self.sections.values() { - pages_in_sections.extend(s.all_pages_path()); - } - for page in self.pages.values() { - if !pages_in_sections.contains(&page.file_path) { - self.render_page(page)?; - } + for page in self.get_all_orphan_pages() { + self.render_page(page)?; } Ok(()) diff --git a/tests/page.rs b/tests/page.rs index 29c5ee1..5833ce0 100644 --- a/tests/page.rs +++ b/tests/page.rs @@ -3,7 +3,7 @@ extern crate tera; extern crate tempdir; use std::collections::HashMap; -use std::fs::File; +use std::fs::{File, create_dir}; use std::path::Path; use tempdir::TempDir; @@ -252,6 +252,29 @@ Hey there assert!(page.content.starts_with("