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.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");
}
}

View file

@ -69,6 +69,7 @@ impl Section {
section.path = section.components.join("/");
section.permalink = config.make_permalink(&section.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<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 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", &sections);
}
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<PathBuf> {
let mut paths = vec![];
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 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,15 +609,20 @@ impl Site {
Ok(())
}
fn render_sections(&self) -> Result<()> {
self.ensure_public_directory_exists()?;
let public = self.output_path.clone();
let sections: HashMap<String, Section> = 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<String, Section> {
self.sections
.values()
.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();
for component in &section.components {
output_path.push(component);
@ -634,21 +637,28 @@ impl Site {
}
if !section.meta.should_render() {
continue;
return Ok(());
}
if section.meta.is_paginated() {
self.render_paginated(&output_path, section)?;
} else {
let output = section.render_html(
&sections,
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(())
}

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
pub fn find_content_components<P: AsRef<Path>>(path: P) -> Vec<String> {
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();
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() {