Browse Source

Rename taxonomies paginate and error on unknown ones

index-subcmd
Vincent Prouillet 6 years ago
parent
commit
18bef00671
8 changed files with 40 additions and 19 deletions
  1. +3
    -3
      components/config/src/lib.rs
  2. +2
    -2
      components/pagination/src/lib.rs
  3. +3
    -3
      components/rebuild/src/lib.rs
  4. +6
    -4
      components/site/src/lib.rs
  5. +1
    -1
      components/site/tests/site.rs
  6. +23
    -4
      components/taxonomies/src/lib.rs
  7. +1
    -1
      docs/content/documentation/content/taxonomies.md
  8. +1
    -1
      docs/content/documentation/getting-started/configuration.md

+ 3
- 3
components/config/src/lib.rs View File

@@ -35,7 +35,7 @@ pub struct Taxonomy {
pub name: String, pub name: String,
/// If this is set, the list of individual taxonomy term page will be paginated /// If this is set, the list of individual taxonomy term page will be paginated
/// by this much /// by this much
pub paginate: Option<usize>,
pub paginate_by: Option<usize>,
pub paginate_path: Option<String>, pub paginate_path: Option<String>,
/// Whether to generate a RSS feed only for each taxonomy term, defaults to false /// Whether to generate a RSS feed only for each taxonomy term, defaults to false
pub rss: bool, pub rss: bool,
@@ -43,7 +43,7 @@ pub struct Taxonomy {


impl Taxonomy { impl Taxonomy {
pub fn is_paginated(&self) -> bool { pub fn is_paginated(&self) -> bool {
if let Some(paginate_by) = self.paginate {
if let Some(paginate_by) = self.paginate_by {
paginate_by > 0 paginate_by > 0
} else { } else {
false false
@@ -55,7 +55,7 @@ impl Default for Taxonomy {
fn default() -> Taxonomy { fn default() -> Taxonomy {
Taxonomy { Taxonomy {
name: String::new(), name: String::new(),
paginate: None,
paginate_by: None,
paginate_path: None, paginate_path: None,
rss: false, rss: false,
} }


+ 2
- 2
components/pagination/src/lib.rs View File

@@ -104,7 +104,7 @@ impl<'a> Paginator<'a> {
/// Create a new paginator from a taxonomy /// 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 /// 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> { 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 { let mut paginator = Paginator {
all_pages: &item.pages, all_pages: &item.pages,
pagers: vec![], pagers: vec![],
@@ -339,7 +339,7 @@ mod tests {
]; ];
let taxonomy_def = TaxonomyConfig { let taxonomy_def = TaxonomyConfig {
name: "tags".to_string(), name: "tags".to_string(),
paginate: Some(2),
paginate_by: Some(2),
..TaxonomyConfig::default() ..TaxonomyConfig::default()
}; };
let taxonomy_item = TaxonomyItem { let taxonomy_item = TaxonomyItem {


+ 3
- 3
components/rebuild/src/lib.rs View File

@@ -112,7 +112,7 @@ fn delete_element(site: &mut Site, path: &Path, is_section: bool) -> Result<()>
site.permalinks.remove(&p.file.relative); site.permalinks.remove(&p.file.relative);


if !p.meta.taxonomies.is_empty() { 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 // 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 // Sort always comes first if present so the rendering will be fine
match changes { match changes {
PageChangesNeeded::Taxonomies => { PageChangesNeeded::Taxonomies => {
site.populate_taxonomies();
site.populate_taxonomies()?;
site.register_tera_global_fns(); site.register_tera_global_fns();
site.render_taxonomies()?; site.render_taxonomies()?;
}, },
@@ -240,7 +240,7 @@ fn handle_page_editing(site: &mut Site, path: &Path) -> Result<()> {
// It's a new page! // It's a new page!
None => { None => {
site.populate_sections(); site.populate_sections();
site.populate_taxonomies();
site.populate_taxonomies()?;
site.register_tera_global_fns(); site.register_tera_global_fns();
// No need to optimise that yet, we can revisit if it becomes an issue // No need to optimise that yet, we can revisit if it becomes an issue
site.build() site.build()


+ 6
- 4
components/site/src/lib.rs View File

@@ -246,7 +246,7 @@ impl Site {
self.register_early_global_fns(); self.register_early_global_fns();
self.render_markdown()?; self.render_markdown()?;
self.populate_sections(); self.populate_sections();
self.populate_taxonomies();
self.populate_taxonomies()?;
self.register_tera_global_fns(); self.register_tera_global_fns();


Ok(()) Ok(())
@@ -407,9 +407,9 @@ impl Site {
} }


/// Find all the tags and categories if it's asked in the config /// 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() { if self.config.taxonomies.is_empty() {
return;
return Ok(());
} }


self.taxonomies = find_taxonomies( self.taxonomies = find_taxonomies(
@@ -420,7 +420,9 @@ impl Site {
.cloned() .cloned()
.collect::<Vec<_>>() .collect::<Vec<_>>()
.as_slice() .as_slice()
);
)?;

Ok(())
} }


/// Inject live reload script tag if in live reload mode /// Inject live reload script tag if in live reload mode


+ 1
- 1
components/site/tests/site.rs View File

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


+ 23
- 4
components/taxonomies/src/lib.rs View File

@@ -3,6 +3,7 @@ extern crate serde_derive;
extern crate tera; extern crate tera;
extern crate slug; extern crate slug;


#[macro_use]
extern crate errors; extern crate errors;
extern crate config; extern crate config;
extern crate content; 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 taxonomies_def = {
let mut m = HashMap::new(); let mut m = HashMap::new();
for t in &config.taxonomies { for t in &config.taxonomies {
@@ -136,7 +137,7 @@ pub fn find_taxonomies(config: &Config, all_pages: &[Page]) -> Vec<Taxonomy> {
.push(page.clone()); .push(page.clone());
} }
} else { } 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.push(Taxonomy::new(taxonomies_def[name].clone(), config, taxo));
} }


taxonomies
Ok(taxonomies)
} }




@@ -184,7 +185,7 @@ mod tests {
page3.meta.taxonomies = taxo_page3; page3.meta.taxonomies = taxo_page3;
let pages = vec![page1, page2, 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 (tags, categories, authors) = {
let mut t = None; let mut t = None;
let mut c = 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].permalink, "http://a-website.com/categories/programming-tutorials/");
assert_eq!(categories.items[1].pages.len(), 1); 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");
}
} }

+ 1
- 1
docs/content/documentation/content/taxonomies.md View File

@@ -10,7 +10,7 @@ The first step is to define the taxonomies in your [config.toml](./documentation
A taxonomy has 4 variables: 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) - `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. - `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 For example the default would be page/1
- `rss`: if set to `true`, a RSS feed will be generated for each individual term. - `rss`: if set to `true`, a RSS feed will be generated for each individual term.


+ 1
- 1
docs/content/documentation/getting-started/configuration.md View File

@@ -44,7 +44,7 @@ rss_limit = 20
# Example: # Example:
# taxonomies = [ # taxonomies = [
# {name: "tags", rss: true}, # each tag will have its own RSS feed # {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 = [] taxonomies = []


Loading…
Cancel
Save