Add orphan in print notice and fix orphan with assets and url

This commit is contained in:
Vincent Prouillet 2017-05-09 21:47:02 +09:00
parent f3edef2640
commit c989ab607c
4 changed files with 61 additions and 16 deletions

View file

@ -11,7 +11,12 @@ use gutenberg::Site;
use console::warn; use console::warn;
fn notify_site_size(site: &Site) { 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) { fn warn_about_ignored_pages(site: &Site) {

View file

@ -143,23 +143,27 @@ impl Page {
// 4. Find sections // 4. Find sections
// Pages with custom urls exists outside of sections // Pages with custom urls exists outside of sections
let mut path_set = false;
if let Some(ref u) = page.meta.url { if let Some(ref u) = page.meta.url {
page.path = u.trim().to_string(); 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 we have a folder with an asset, don't consider it as a component
if page.file_name == "index" { if page.file_name == "index" {
page.components.pop(); page.components.pop();
// also set parent_path to grandparent instead // also set parent_path to grandparent instead
page.parent_path = page.parent_path.parent().unwrap().to_path_buf(); page.parent_path = page.parent_path.parent().unwrap().to_path_buf();
} }
if !path_set {
// Don't add a trailing slash to sections // Don't add a trailing slash to sections
page.path = format!("{}/{}", page.components.join("/"), page.slug); page.path = format!("{}/{}", page.components.join("/"), page.slug);
} else { }
} else if !path_set {
page.path = page.slug.clone(); page.path = page.slug.clone();
} }
page.permalink = config.make_permalink(&page.path); page.permalink = config.make_permalink(&page.path);
Ok(page) Ok(page)

View file

@ -122,6 +122,7 @@ impl Site {
self.live_reload = true; self.live_reload = true;
} }
/// Gets the path of all ignored pages in the site
pub fn get_ignored_pages(&self) -> Vec<PathBuf> { pub fn get_ignored_pages(&self) -> Vec<PathBuf> {
self.sections self.sections
.values() .values()
@ -129,6 +130,24 @@ impl Site {
.collect() .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 /// Used by tests to change the output path to a tmp dir
#[doc(hidden)] #[doc(hidden)]
pub fn set_output_path<P: AsRef<Path>>(&mut self, path: P) { pub fn set_output_path<P: AsRef<Path>>(&mut self, path: P) {
@ -589,16 +608,10 @@ impl Site {
/// Renders all pages that do not belong to any sections /// Renders all pages that do not belong to any sections
fn render_orphan_pages(&self) -> Result<()> { fn render_orphan_pages(&self) -> Result<()> {
self.ensure_public_directory_exists()?; 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() { for page in self.get_all_orphan_pages() {
if !pages_in_sections.contains(&page.file_path) {
self.render_page(page)?; self.render_page(page)?;
} }
}
Ok(()) Ok(())
} }

View file

@ -3,7 +3,7 @@ extern crate tera;
extern crate tempdir; extern crate tempdir;
use std::collections::HashMap; use std::collections::HashMap;
use std::fs::File; use std::fs::{File, create_dir};
use std::path::Path; use std::path::Path;
use tempdir::TempDir; use tempdir::TempDir;
@ -252,6 +252,29 @@ Hey there
assert!(page.content.starts_with("<pre")); assert!(page.content.starts_with("<pre"));
} }
#[test]
fn test_page_with_assets_gets_right_parent_path() {
let tmp_dir = TempDir::new("example").expect("create temp dir");
let path = tmp_dir.path();
create_dir(&path.join("content")).expect("create content temp dir");
create_dir(&path.join("content").join("posts")).expect("create posts temp dir");
let nested_path = path.join("content").join("posts").join("assets");
create_dir(&nested_path).expect("create nested temp dir");
File::create(nested_path.join("index.md")).unwrap();
File::create(nested_path.join("example.js")).unwrap();
File::create(nested_path.join("graph.jpg")).unwrap();
File::create(nested_path.join("fail.png")).unwrap();
let res = Page::parse(
&nested_path.join("index.md").as_path(),
"+++\nurl=\"hey\"+++\n",
&Config::default()
);
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.parent_path, path.join("content").join("posts"));
}
#[test] #[test]
fn test_file_not_named_index_with_assets() { fn test_file_not_named_index_with_assets() {
let tmp_dir = TempDir::new("example").expect("create temp dir"); let tmp_dir = TempDir::new("example").expect("create temp dir");