Rename taxonomies paginate and error on unknown ones

This commit is contained in:
Vincent Prouillet 2018-07-16 18:14:48 +02:00
parent 2f100ecd01
commit 18bef00671
8 changed files with 40 additions and 19 deletions

View file

@ -35,7 +35,7 @@ pub struct Taxonomy {
pub name: String,
/// If this is set, the list of individual taxonomy term page will be paginated
/// by this much
pub paginate: Option<usize>,
pub paginate_by: Option<usize>,
pub paginate_path: Option<String>,
/// Whether to generate a RSS feed only for each taxonomy term, defaults to false
pub rss: bool,
@ -43,7 +43,7 @@ pub struct Taxonomy {
impl Taxonomy {
pub fn is_paginated(&self) -> bool {
if let Some(paginate_by) = self.paginate {
if let Some(paginate_by) = self.paginate_by {
paginate_by > 0
} else {
false
@ -55,7 +55,7 @@ impl Default for Taxonomy {
fn default() -> Taxonomy {
Taxonomy {
name: String::new(),
paginate: None,
paginate_by: None,
paginate_path: None,
rss: false,
}

View file

@ -104,7 +104,7 @@ impl<'a> Paginator<'a> {
/// Create a new paginator from a taxonomy
/// It will always at least create one pager (the first) even if there are no pages to paginate
pub fn from_taxonomy(taxonomy: &'a Taxonomy, item: &'a TaxonomyItem) -> Paginator<'a> {
let paginate_by = taxonomy.kind.paginate.unwrap();
let paginate_by = taxonomy.kind.paginate_by.unwrap();
let mut paginator = Paginator {
all_pages: &item.pages,
pagers: vec![],
@ -339,7 +339,7 @@ mod tests {
];
let taxonomy_def = TaxonomyConfig {
name: "tags".to_string(),
paginate: Some(2),
paginate_by: Some(2),
..TaxonomyConfig::default()
};
let taxonomy_item = TaxonomyItem {

View file

@ -112,7 +112,7 @@ fn delete_element(site: &mut Site, path: &Path, is_section: bool) -> Result<()>
site.permalinks.remove(&p.file.relative);
if !p.meta.taxonomies.is_empty() {
site.populate_taxonomies();
site.populate_taxonomies()?;
}
// if there is a parent section, we will need to re-render it
@ -207,7 +207,7 @@ fn handle_page_editing(site: &mut Site, path: &Path) -> Result<()> {
// Sort always comes first if present so the rendering will be fine
match changes {
PageChangesNeeded::Taxonomies => {
site.populate_taxonomies();
site.populate_taxonomies()?;
site.register_tera_global_fns();
site.render_taxonomies()?;
},
@ -240,7 +240,7 @@ fn handle_page_editing(site: &mut Site, path: &Path) -> Result<()> {
// It's a new page!
None => {
site.populate_sections();
site.populate_taxonomies();
site.populate_taxonomies()?;
site.register_tera_global_fns();
// No need to optimise that yet, we can revisit if it becomes an issue
site.build()

View file

@ -246,7 +246,7 @@ impl Site {
self.register_early_global_fns();
self.render_markdown()?;
self.populate_sections();
self.populate_taxonomies();
self.populate_taxonomies()?;
self.register_tera_global_fns();
Ok(())
@ -407,9 +407,9 @@ impl Site {
}
/// Find all the tags and categories if it's asked in the config
pub fn populate_taxonomies(&mut self) {
pub fn populate_taxonomies(&mut self) -> Result<()> {
if self.config.taxonomies.is_empty() {
return;
return Ok(());
}
self.taxonomies = find_taxonomies(
@ -420,7 +420,9 @@ impl Site {
.cloned()
.collect::<Vec<_>>()
.as_slice()
);
)?;
Ok(())
}
/// Inject live reload script tag if in live reload mode

View file

@ -223,7 +223,7 @@ fn can_build_site_with_taxonomies() {
taxonomies
};
}
site.populate_taxonomies();
site.populate_taxonomies().unwrap();
let tmp_dir = tempdir().expect("create temp dir");
let public = &tmp_dir.path().join("public");
site.set_output_path(&public);

View file

@ -3,6 +3,7 @@ extern crate serde_derive;
extern crate tera;
extern crate slug;
#[macro_use]
extern crate errors;
extern crate config;
extern crate content;
@ -110,7 +111,7 @@ impl Taxonomy {
}
}
pub fn find_taxonomies(config: &Config, all_pages: &[Page]) -> Vec<Taxonomy> {
pub fn find_taxonomies(config: &Config, all_pages: &[Page]) -> Result<Vec<Taxonomy>> {
let taxonomies_def = {
let mut m = HashMap::new();
for t in &config.taxonomies {
@ -136,7 +137,7 @@ pub fn find_taxonomies(config: &Config, all_pages: &[Page]) -> Vec<Taxonomy> {
.push(page.clone());
}
} else {
// TODO: bail with error
bail!("Page `{}` has taxonomy `{}` which is not defined in config.toml", page.file.path.display(), name);
}
}
}
@ -147,7 +148,7 @@ pub fn find_taxonomies(config: &Config, all_pages: &[Page]) -> Vec<Taxonomy> {
taxonomies.push(Taxonomy::new(taxonomies_def[name].clone(), config, taxo));
}
taxonomies
Ok(taxonomies)
}
@ -184,7 +185,7 @@ mod tests {
page3.meta.taxonomies = taxo_page3;
let pages = vec![page1, page2, page3];
let taxonomies = find_taxonomies(&config, &pages);
let taxonomies = find_taxonomies(&config, &pages).unwrap();
let (tags, categories, authors) = {
let mut t = None;
let mut c = None;
@ -228,4 +229,22 @@ mod tests {
assert_eq!(categories.items[1].permalink, "http://a-website.com/categories/programming-tutorials/");
assert_eq!(categories.items[1].pages.len(), 1);
}
#[test]
fn errors_on_unknown_taxonomy() {
let mut config = Config::default();
config.taxonomies = vec![
Taxonomy {name: "authors".to_string(), ..Taxonomy::default()},
];
let mut page1 = Page::default();
let mut taxo_page1 = HashMap::new();
taxo_page1.insert("tags".to_string(), vec!["rust".to_string(), "db".to_string()]);
page1.meta.taxonomies = taxo_page1;
let taxonomies = find_taxonomies(&config, &vec![page1]);
assert!(taxonomies.is_err());
let err = taxonomies.unwrap_err();
// no path as this is created by Default
assert_eq!(err.description(), "Page `` has taxonomy `tags` which is not defined in config.toml");
}
}

View file

@ -10,7 +10,7 @@ The first step is to define the taxonomies in your [config.toml](./documentation
A taxonomy has 4 variables:
- `name`: a required string that will be used in the URLs, usually the plural version (i.e. tags, categories etc)
- `paginate`: if this is set to a number, each term page will be paginated by this much.
- `paginate_by`: if this is set to a number, each term page will be paginated by this much.
- `paginate_path`: if set, will be the path used by paginated page and the page number will be appended after it.
For example the default would be page/1
- `rss`: if set to `true`, a RSS feed will be generated for each individual term.

View file

@ -44,7 +44,7 @@ rss_limit = 20
# Example:
# taxonomies = [
# {name: "tags", rss: true}, # each tag will have its own RSS feed
# {name: "categories", paginate: 5}, # 5 terms per page
# {name: "categories", paginate_by: 5}, # 5 terms per page
# ]
#
taxonomies = []