From 5ac5625de1585a7265a0b03454b9e96574cab87c Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Fri, 12 May 2017 22:32:35 +0900 Subject: [PATCH] Default to not generating tags and categories page Also add a Site::render_section method --- src/config.rs | 21 ++++---------- src/section.rs | 6 ++-- src/site.rs | 74 ++++++++++++++++++++++++++++---------------------- src/utils.rs | 2 +- tests/site.rs | 2 ++ 5 files changed, 54 insertions(+), 51 deletions(-) diff --git a/src/config.rs b/src/config.rs index 2d8a3bfd..fbcffb27 100644 --- a/src/config.rs +++ b/src/config.rs @@ -58,6 +58,10 @@ impl Config { set_default!(config.language_code, "en".to_string()); set_default!(config.highlight_code, false); + set_default!(config.generate_rss, false); + set_default!(config.generate_tags_pages, false); + set_default!(config.generate_categories_pages, false); + set_default!(config.insert_anchor_links, false); match config.highlight_theme { Some(ref t) => { @@ -68,11 +72,6 @@ impl Config { None => config.highlight_theme = Some("base16-ocean-dark".to_string()) }; - set_default!(config.generate_rss, false); - set_default!(config.generate_tags_pages, true); - set_default!(config.generate_categories_pages, true); - set_default!(config.insert_anchor_links, false); - Ok(config) } @@ -158,6 +157,7 @@ base_url = "https://replace-this-with-your-url.com" #[test] fn test_errors_when_missing_required_field() { + // base_url is required let config = r#" title = "" "#; @@ -181,15 +181,4 @@ hello = "world" assert_eq!(config.unwrap().extra.unwrap().get("hello").unwrap().as_str().unwrap(), "world"); } - #[test] - fn test_language_defaults_to_en() { - let config = r#" -title = "My site" -base_url = "https://replace-this-with-your-url.com""#; - - let config = Config::parse(config); - assert!(config.is_ok()); - let config = config.unwrap(); - assert_eq!(config.language_code.unwrap(), "en"); - } } diff --git a/src/section.rs b/src/section.rs index 82c4495d..1d261c3f 100644 --- a/src/section.rs +++ b/src/section.rs @@ -69,6 +69,7 @@ impl Section { section.path = section.components.join("/"); section.permalink = config.make_permalink(§ion.path); if section.components.is_empty() { + // the index one section.relative_path = "_index.md".to_string(); } else { section.relative_path = format!("{}/_index.md", section.components.join("/")); @@ -105,7 +106,7 @@ impl Section { } /// Renders the page using the default layout, unless specified in front-matter - pub fn render_html(&self, sections: &HashMap, tera: &Tera, config: &Config) -> Result { + pub fn render_html(&self, sections: HashMap, tera: &Tera, config: &Config) -> Result { let tpl_name = self.get_template_name(); let mut context = Context::new(); @@ -114,7 +115,7 @@ impl Section { context.add("current_url", &self.permalink); context.add("current_path", &self.path); if self.is_index() { - context.add("sections", sections); + context.add("sections", §ions); } tera.render(&tpl_name, &context) @@ -126,6 +127,7 @@ impl Section { self.components.is_empty() } + /// Returns all the paths for the pages belonging to that section pub fn all_pages_path(&self) -> Vec { let mut paths = vec![]; paths.extend(self.pages.iter().map(|p| p.file_path.clone())); diff --git a/src/site.rs b/src/site.rs index 11978bd2..46c20f7a 100644 --- a/src/site.rs +++ b/src/site.rs @@ -161,8 +161,6 @@ impl Site { let base_path = self.base_path.to_string_lossy().replace("\\", "/"); let content_glob = format!("{}/{}", base_path, "content/**/*.md"); - // TODO: make that parallel, that's the main bottleneck - // `add_section` and `add_page` can't be used in the parallel version afaik for entry in glob(&content_glob).unwrap().filter_map(|e| e.ok()) { let path = entry.as_path(); if path.file_name().unwrap() == "_index.md" { @@ -611,44 +609,56 @@ impl Site { Ok(()) } - fn render_sections(&self) -> Result<()> { - self.ensure_public_directory_exists()?; - let public = self.output_path.clone(); - let sections: HashMap = self.sections + /// Create a hashmap of paths to section + /// For example `content/posts/_index.md` key will be `posts` + fn get_sections_map(&self) -> HashMap { + self.sections .values() .map(|s| (s.components.join("/"), s.clone())) - .collect(); + .collect() + } - for section in self.sections.values() { - let mut output_path = public.to_path_buf(); - for component in §ion.components { - output_path.push(component); + /// Renders a single section + fn render_section(&self, section: &Section) -> Result<()> { + self.ensure_public_directory_exists()?; + let public = self.output_path.clone(); - if !output_path.exists() { - create_directory(&output_path)?; - } - } + let mut output_path = public.to_path_buf(); + for component in §ion.components { + output_path.push(component); - for page in §ion.pages { - self.render_page(page)?; - } - - if !section.meta.should_render() { - continue; - } - - if section.meta.is_paginated() { - self.render_paginated(&output_path, section)?; - } else { - let output = section.render_html( - §ions, - &self.tera, - &self.config, - )?; - create_file(output_path.join("index.html"), &self.inject_livereload(output))?; + if !output_path.exists() { + create_directory(&output_path)?; } } + for page in §ion.pages { + self.render_page(page)?; + } + + if !section.meta.should_render() { + return Ok(()); + } + + if section.meta.is_paginated() { + self.render_paginated(&output_path, section)?; + } else { + let output = section.render_html( + if section.is_index() { self.get_sections_map() } else { HashMap::new() }, + &self.tera, + &self.config, + )?; + create_file(output_path.join("index.html"), &self.inject_livereload(output))?; + } + + Ok(()) + } + + /// Renders all sections + fn render_sections(&self) -> Result<()> { + for section in self.sections.values() { + self.render_section(section)?; + } Ok(()) } diff --git a/src/utils.rs b/src/utils.rs index ed374173..38a4eb9c 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -35,7 +35,7 @@ pub fn read_file>(path: P) -> Result { } -/// Takes a full path to a .md and returns only the components after the `content` directory +/// Takes a full path to a .md and returns only the components after the first `content` directory /// Will not return the filename as last component pub fn find_content_components>(path: P) -> Vec { let path = path.as_ref(); diff --git a/tests/site.rs b/tests/site.rs index 2c463863..2166eee9 100644 --- a/tests/site.rs +++ b/tests/site.rs @@ -173,6 +173,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, "config.toml").unwrap(); + site.config.generate_categories_pages = Some(true); site.load().unwrap(); for (i, page) in site.pages.values_mut().enumerate() { @@ -224,6 +225,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, "config.toml").unwrap(); + site.config.generate_tags_pages = Some(true); site.load().unwrap(); for (i, page) in site.pages.values_mut().enumerate() {