From 005990a92821e2bade0c45c5a29f456ce79d9982 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Fri, 12 May 2017 16:30:01 +0900 Subject: [PATCH] Create a default index section if there is none --- src/front_matter.rs | 2 +- src/section.rs | 18 ++++++++++++++++++ src/site.rs | 11 +++++++++-- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/front_matter.rs b/src/front_matter.rs index c53e2f54..78715306 100644 --- a/src/front_matter.rs +++ b/src/front_matter.rs @@ -150,7 +150,7 @@ impl Default for FrontMatter { template: None, paginate_by: None, paginate_path: None, - render: None, + render: Some(true), extra: None, } } diff --git a/src/section.rs b/src/section.rs index 678b1caf..b6650e70 100644 --- a/src/section.rs +++ b/src/section.rs @@ -131,3 +131,21 @@ impl ser::Serialize for Section { state.end() } } + +impl Default for Section { + /// Used to create a default index section if there is no _index.md + fn default() -> Section { + Section { + file_path: PathBuf::new(), + relative_path: "".to_string(), + parent_path: PathBuf::new(), + components: vec![], + path: "".to_string(), + permalink: "".to_string(), + meta: FrontMatter::default(), + pages: vec![], + ignored_pages: vec![], + subsections: vec![], + } + } +} diff --git a/src/site.rs b/src/site.rs index 18cf8512..67bd478f 100644 --- a/src/site.rs +++ b/src/site.rs @@ -171,6 +171,12 @@ impl Site { self.add_page(path)?; } } + // Insert a default index section so we don't need to create a _index.md to render + // the index page + let index_path = self.base_path.join("content"); + if !self.sections.contains_key(&index_path) { + self.sections.insert(index_path, Section::default()); + } // A map of all .md files (section and pages) and their permalink // We need that if there are relative links in the content that need to be resolved @@ -234,8 +240,9 @@ impl Site { let mut grandparent_paths = HashMap::new(); for section in self.sections.values() { - let grand_parent = section.parent_path.parent().unwrap().to_path_buf(); - grandparent_paths.entry(grand_parent).or_insert_with(|| vec![]).push(section.clone()); + if let Some(grand_parent) = section.parent_path.parent() { + grandparent_paths.entry(grand_parent.to_path_buf()).or_insert_with(|| vec![]).push(section.clone()); + } } for (parent_path, section) in &mut self.sections {