From c989ab607c4aac084c9fe4cbc19aae5a904a439d Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Tue, 9 May 2017 21:47:02 +0900 Subject: [PATCH] Add orphan in print notice and fix orphan with assets and url --- src/cmd/mod.rs | 7 ++++++- src/page.rs | 16 ++++++++++------ src/site.rs | 29 +++++++++++++++++++++-------- tests/page.rs | 25 ++++++++++++++++++++++++- 4 files changed, 61 insertions(+), 16 deletions(-) diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index 33360fa5..d99fa6a3 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 06d83e72..c63f63ca 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 5cd91272..27d2373d 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 29c5ee13..5833ce0e 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("