Default to not generating tags and categories page

Also add a Site::render_section method
This commit is contained in:
Vincent Prouillet 2017-05-12 22:32:35 +09:00
parent ab3e04c9e5
commit 5ac5625de1
5 changed files with 54 additions and 51 deletions

View file

@ -58,6 +58,10 @@ impl Config {
set_default!(config.language_code, "en".to_string()); set_default!(config.language_code, "en".to_string());
set_default!(config.highlight_code, false); 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 { match config.highlight_theme {
Some(ref t) => { Some(ref t) => {
@ -68,11 +72,6 @@ impl Config {
None => config.highlight_theme = Some("base16-ocean-dark".to_string()) 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) Ok(config)
} }
@ -158,6 +157,7 @@ base_url = "https://replace-this-with-your-url.com"
#[test] #[test]
fn test_errors_when_missing_required_field() { fn test_errors_when_missing_required_field() {
// base_url is required
let config = r#" let config = r#"
title = "" title = ""
"#; "#;
@ -181,15 +181,4 @@ hello = "world"
assert_eq!(config.unwrap().extra.unwrap().get("hello").unwrap().as_str().unwrap(), "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");
}
} }

View file

@ -69,6 +69,7 @@ impl Section {
section.path = section.components.join("/"); section.path = section.components.join("/");
section.permalink = config.make_permalink(&section.path); section.permalink = config.make_permalink(&section.path);
if section.components.is_empty() { if section.components.is_empty() {
// the index one
section.relative_path = "_index.md".to_string(); section.relative_path = "_index.md".to_string();
} else { } else {
section.relative_path = format!("{}/_index.md", section.components.join("/")); 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 /// Renders the page using the default layout, unless specified in front-matter
pub fn render_html(&self, sections: &HashMap<String, Section>, tera: &Tera, config: &Config) -> Result<String> { pub fn render_html(&self, sections: HashMap<String, Section>, tera: &Tera, config: &Config) -> Result<String> {
let tpl_name = self.get_template_name(); let tpl_name = self.get_template_name();
let mut context = Context::new(); let mut context = Context::new();
@ -114,7 +115,7 @@ impl Section {
context.add("current_url", &self.permalink); context.add("current_url", &self.permalink);
context.add("current_path", &self.path); context.add("current_path", &self.path);
if self.is_index() { if self.is_index() {
context.add("sections", sections); context.add("sections", &sections);
} }
tera.render(&tpl_name, &context) tera.render(&tpl_name, &context)
@ -126,6 +127,7 @@ impl Section {
self.components.is_empty() self.components.is_empty()
} }
/// Returns all the paths for the pages belonging to that section
pub fn all_pages_path(&self) -> Vec<PathBuf> { pub fn all_pages_path(&self) -> Vec<PathBuf> {
let mut paths = vec![]; let mut paths = vec![];
paths.extend(self.pages.iter().map(|p| p.file_path.clone())); paths.extend(self.pages.iter().map(|p| p.file_path.clone()));

View file

@ -161,8 +161,6 @@ impl Site {
let base_path = self.base_path.to_string_lossy().replace("\\", "/"); let base_path = self.base_path.to_string_lossy().replace("\\", "/");
let content_glob = format!("{}/{}", base_path, "content/**/*.md"); 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()) { for entry in glob(&content_glob).unwrap().filter_map(|e| e.ok()) {
let path = entry.as_path(); let path = entry.as_path();
if path.file_name().unwrap() == "_index.md" { if path.file_name().unwrap() == "_index.md" {
@ -611,15 +609,20 @@ impl Site {
Ok(()) Ok(())
} }
fn render_sections(&self) -> Result<()> { /// Create a hashmap of paths to section
self.ensure_public_directory_exists()?; /// For example `content/posts/_index.md` key will be `posts`
let public = self.output_path.clone(); fn get_sections_map(&self) -> HashMap<String, Section> {
let sections: HashMap<String, Section> = self.sections self.sections
.values() .values()
.map(|s| (s.components.join("/"), s.clone())) .map(|s| (s.components.join("/"), s.clone()))
.collect(); .collect()
}
/// Renders a single section
fn render_section(&self, section: &Section) -> Result<()> {
self.ensure_public_directory_exists()?;
let public = self.output_path.clone();
for section in self.sections.values() {
let mut output_path = public.to_path_buf(); let mut output_path = public.to_path_buf();
for component in &section.components { for component in &section.components {
output_path.push(component); output_path.push(component);
@ -634,21 +637,28 @@ impl Site {
} }
if !section.meta.should_render() { if !section.meta.should_render() {
continue; return Ok(());
} }
if section.meta.is_paginated() { if section.meta.is_paginated() {
self.render_paginated(&output_path, section)?; self.render_paginated(&output_path, section)?;
} else { } else {
let output = section.render_html( let output = section.render_html(
&sections, if section.is_index() { self.get_sections_map() } else { HashMap::new() },
&self.tera, &self.tera,
&self.config, &self.config,
)?; )?;
create_file(output_path.join("index.html"), &self.inject_livereload(output))?; 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(()) Ok(())
} }

View file

@ -35,7 +35,7 @@ pub fn read_file<P: AsRef<Path>>(path: P) -> Result<String> {
} }
/// 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 /// Will not return the filename as last component
pub fn find_content_components<P: AsRef<Path>>(path: P) -> Vec<String> { pub fn find_content_components<P: AsRef<Path>>(path: P) -> Vec<String> {
let path = path.as_ref(); let path = path.as_ref();

View file

@ -173,6 +173,7 @@ fn test_can_build_site_with_categories() {
let mut path = env::current_dir().unwrap().to_path_buf(); let mut path = env::current_dir().unwrap().to_path_buf();
path.push("test_site"); path.push("test_site");
let mut site = Site::new(&path, "config.toml").unwrap(); let mut site = Site::new(&path, "config.toml").unwrap();
site.config.generate_categories_pages = Some(true);
site.load().unwrap(); site.load().unwrap();
for (i, page) in site.pages.values_mut().enumerate() { 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(); let mut path = env::current_dir().unwrap().to_path_buf();
path.push("test_site"); path.push("test_site");
let mut site = Site::new(&path, "config.toml").unwrap(); let mut site = Site::new(&path, "config.toml").unwrap();
site.config.generate_tags_pages = Some(true);
site.load().unwrap(); site.load().unwrap();
for (i, page) in site.pages.values_mut().enumerate() { for (i, page) in site.pages.values_mut().enumerate() {