Add flags in config to disable tags/categories generation

This commit is contained in:
Vincent Prouillet 2017-03-20 18:36:24 +09:00
parent 9af85ba3e4
commit 540bbcc1b6
2 changed files with 41 additions and 24 deletions

View file

@ -31,11 +31,23 @@ pub struct Config {
pub language_code: Option<String>, pub language_code: Option<String>,
/// Whether to generate RSS, defaults to false /// Whether to generate RSS, defaults to false
pub generate_rss: Option<bool>, pub generate_rss: Option<bool>,
/// Whether to generate tags and individual tag pages if some pages have them. Defaults to true
pub generate_tags_pages: Option<bool>,
/// Whether to generate categories and individual tag categories if some pages have them. Defaults to true
pub generate_categories_pages: Option<bool>,
/// All user params set in [extra] in the config /// All user params set in [extra] in the config
pub extra: Option<HashMap<String, Toml>>, pub extra: Option<HashMap<String, Toml>>,
} }
macro_rules! set_default {
($key: expr, $default: expr) => {
if $key.is_none() {
$key = Some($default);
}
}
}
impl Config { impl Config {
/// Parses a string containing TOML to our Config struct /// Parses a string containing TOML to our Config struct
/// Any extra parameter will end up in the extra field /// Any extra parameter will end up in the extra field
@ -45,13 +57,8 @@ impl Config {
Err(e) => bail!(e) Err(e) => bail!(e)
}; };
if config.language_code.is_none() { set_default!(config.language_code, "en".to_string());
config.language_code = Some("en".to_string()); set_default!(config.highlight_code, false);
}
if config.highlight_code.is_none() {
config.highlight_code = Some(false);
}
match config.highlight_theme { match config.highlight_theme {
Some(ref t) => { Some(ref t) => {
@ -62,9 +69,9 @@ impl Config {
None => config.highlight_theme = Some("base16-ocean-dark".to_string()) None => config.highlight_theme = Some("base16-ocean-dark".to_string())
}; };
if config.generate_rss.is_none() { set_default!(config.generate_rss, false);
config.generate_rss = Some(false); set_default!(config.generate_tags_pages, true);
} set_default!(config.generate_categories_pages, true);
Ok(config) Ok(config)
} }
@ -100,6 +107,8 @@ impl Default for Config {
description: None, description: None,
language_code: Some("en".to_string()), language_code: Some("en".to_string()),
generate_rss: Some(false), generate_rss: Some(false),
generate_tags_pages: Some(true),
generate_categories_pages: Some(true),
extra: None, extra: None,
} }
} }

View file

@ -269,8 +269,12 @@ impl Site {
} }
// Outputting categories and pages // Outputting categories and pages
self.render_categories_and_tags(RenderList::Categories)?; if self.config.generate_categories_pages.unwrap() {
self.render_categories_and_tags(RenderList::Tags)?; self.render_categories_and_tags(RenderList::Categories)?;
}
if self.config.generate_tags_pages.unwrap() {
self.render_categories_and_tags(RenderList::Tags)?;
}
// And finally the index page // And finally the index page
let mut context = Context::new(); let mut context = Context::new();
@ -368,23 +372,27 @@ impl Site {
context.add("sections", &self.sections.values().collect::<Vec<&Section>>()); context.add("sections", &self.sections.values().collect::<Vec<&Section>>());
let mut categories = vec![]; let mut categories = vec![];
if !self.categories.is_empty() { if self.config.generate_categories_pages.unwrap() {
categories.push(self.config.make_permalink("categories")); if !self.categories.is_empty() {
for category in self.categories.keys() { categories.push(self.config.make_permalink("categories"));
categories.push( for category in self.categories.keys() {
self.config.make_permalink(&format!("categories/{}", slugify(category))) categories.push(
); self.config.make_permalink(&format!("categories/{}", slugify(category)))
);
}
} }
} }
context.add("categories", &categories); context.add("categories", &categories);
let mut tags = vec![]; let mut tags = vec![];
if !self.tags.is_empty() { if self.config.generate_tags_pages.unwrap() {
tags.push(self.config.make_permalink("tags")); if !self.tags.is_empty() {
for tag in self.tags.keys() { tags.push(self.config.make_permalink("tags"));
tags.push( for tag in self.tags.keys() {
self.config.make_permalink(&format!("tags/{}", slugify(tag))) tags.push(
); self.config.make_permalink(&format!("tags/{}", slugify(tag)))
);
}
} }
} }
context.add("tags", &tags); context.add("tags", &tags);