Refactor taxonomies

Fix #66
This commit is contained in:
Vincent Prouillet 2017-05-16 13:37:00 +09:00
parent 056bf55881
commit be784bbaa3
16 changed files with 247 additions and 185 deletions

View file

@ -1,5 +1,10 @@
# Changelog # Changelog
## 0.0.6 (unreleased)
- Fix missing serialized data for sections
- Change the single item template context for categories/tags
## 0.0.5 (2017-05-15) ## 0.0.5 (2017-05-15)
- Fix XML templates overriding and reloading - Fix XML templates overriding and reloading

View file

@ -36,13 +36,17 @@ pub fn notify_site_size(site: &Site) {
/// Display a warning in the console if there are ignored pages in the site /// Display a warning in the console if there are ignored pages in the site
pub fn warn_about_ignored_pages(site: &Site) { pub fn warn_about_ignored_pages(site: &Site) {
let ignored_pages = site.get_ignored_pages(); let ignored_pages: Vec<_> = site.sections
.values()
.flat_map(|s| s.ignored_pages.iter().map(|p| p.file.path.clone()))
.collect();
if !ignored_pages.is_empty() { if !ignored_pages.is_empty() {
warn(&format!( warn(&format!(
"{} page(s) ignored (missing date or order in a sorted section):", "{} page(s) ignored (missing date or order in a sorted section):",
ignored_pages.len() ignored_pages.len()
)); ));
for path in site.get_ignored_pages() { for path in ignored_pages {
warn(&format!("- {}", path.display())); warn(&format!("- {}", path.display()));
} }
} }

View file

@ -1,8 +1,21 @@
use std::path::Path; use std::path::Path;
use gutenberg::{Site, SectionFrontMatter, PageFrontMatter}; use gutenberg::{Site, Page, Section, SectionFrontMatter, PageFrontMatter};
use gutenberg::errors::Result; use gutenberg::errors::Result;
/// Finds the section that contains the page given if there is one
pub fn find_parent_section<'a>(site: &'a Site, page: &Page) -> Option<&'a Section> {
for section in site.sections.values() {
if section.is_child_page(page) {
return Some(section)
}
}
None
}
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
enum PageChangesNeeded { enum PageChangesNeeded {
/// Editing `tags` /// Editing `tags`
@ -101,7 +114,7 @@ pub fn after_content_change(site: &mut Site, path: &Path) -> Result<()> {
site.populate_tags_and_categories(); site.populate_tags_and_categories();
} }
if site.find_parent_section(&p).is_some() { if find_parent_section(site, &p).is_some() {
site.populate_sections(); site.populate_sections();
} }
}; };
@ -171,7 +184,7 @@ pub fn after_content_change(site: &mut Site, path: &Path) -> Result<()> {
site.render_categories()?; site.render_categories()?;
}, },
PageChangesNeeded::Sort => { PageChangesNeeded::Sort => {
let section_path = match site.find_parent_section(&site.pages[path]) { let section_path = match find_parent_section(site, &site.pages[path]) {
Some(s) => s.file.path.clone(), Some(s) => s.file.path.clone(),
None => continue // Do nothing if it's an orphan page None => continue // Do nothing if it's an orphan page
}; };

View file

@ -4,9 +4,11 @@ mod section;
mod sorting; mod sorting;
mod utils; mod utils;
mod file_info; mod file_info;
mod taxonomies;
pub use self::page::{Page}; pub use self::page::{Page};
pub use self::section::{Section}; pub use self::section::{Section};
pub use self::pagination::{Paginator, Pager}; pub use self::pagination::{Paginator, Pager};
pub use self::sorting::{SortBy, sort_pages, populate_previous_and_next_pages}; pub use self::sorting::{SortBy, sort_pages, populate_previous_and_next_pages};
pub use self::taxonomies::{Taxonomy, TaxonomyItem};

View file

@ -12,7 +12,7 @@ use errors::{Result, ResultExt};
use config::Config; use config::Config;
use front_matter::{PageFrontMatter, split_page_content}; use front_matter::{PageFrontMatter, split_page_content};
use markdown::markdown_to_html; use markdown::markdown_to_html;
use utils::{read_file}; use fs::{read_file};
use content::utils::{find_related_assets, get_reading_analytics}; use content::utils::{find_related_assets, get_reading_analytics};
use content::file_info::FileInfo; use content::file_info::FileInfo;
@ -163,7 +163,7 @@ impl Default for Page {
impl ser::Serialize for Page { impl ser::Serialize for Page {
fn serialize<S>(&self, serializer: S) -> StdResult<S::Ok, S::Error> where S: ser::Serializer { fn serialize<S>(&self, serializer: S) -> StdResult<S::Ok, S::Error> where S: ser::Serializer {
let mut state = serializer.serialize_struct("page", 16)?; let mut state = serializer.serialize_struct("page", 15)?;
state.serialize_field("content", &self.content)?; state.serialize_field("content", &self.content)?;
state.serialize_field("title", &self.meta.title)?; state.serialize_field("title", &self.meta.title)?;
state.serialize_field("description", &self.meta.description)?; state.serialize_field("description", &self.meta.description)?;
@ -173,7 +173,6 @@ impl ser::Serialize for Page {
state.serialize_field("permalink", &self.permalink)?; state.serialize_field("permalink", &self.permalink)?;
state.serialize_field("summary", &self.summary)?; state.serialize_field("summary", &self.summary)?;
state.serialize_field("tags", &self.meta.tags)?; state.serialize_field("tags", &self.meta.tags)?;
state.serialize_field("draft", &self.meta.draft)?;
state.serialize_field("category", &self.meta.category)?; state.serialize_field("category", &self.meta.category)?;
state.serialize_field("extra", &self.meta.extra)?; state.serialize_field("extra", &self.meta.extra)?;
let (word_count, reading_time) = get_reading_analytics(&self.raw_content); let (word_count, reading_time) = get_reading_analytics(&self.raw_content);

View file

@ -8,7 +8,7 @@ use serde::ser::{SerializeStruct, self};
use config::Config; use config::Config;
use front_matter::{SectionFrontMatter, split_section_content}; use front_matter::{SectionFrontMatter, split_section_content};
use errors::{Result, ResultExt}; use errors::{Result, ResultExt};
use utils::{read_file}; use fs::{read_file};
use markdown::markdown_to_html; use markdown::markdown_to_html;
use content::Page; use content::Page;
use content::file_info::FileInfo; use content::file_info::FileInfo;

135
src/content/taxonomies.rs Normal file
View file

@ -0,0 +1,135 @@
use std::collections::HashMap;
use slug::slugify;
use tera::{Context, Tera};
use config::Config;
use errors::{Result, ResultExt};
use content::Page;
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum TaxonomyKind {
Tags,
Categories,
}
/// A tag or category
#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct TaxonomyItem {
pub name: String,
pub slug: String,
pub pages: Vec<Page>,
}
impl TaxonomyItem {
pub fn new(name: &str, pages: Vec<Page>) -> TaxonomyItem {
TaxonomyItem {
name: name.to_string(),
slug: slugify(name),
pages,
}
}
}
/// All the tags or categories
#[derive(Debug, Clone, PartialEq)]
pub struct Taxonomy {
pub kind: TaxonomyKind,
// this vec is sorted by the count of item
pub items: Vec<TaxonomyItem>,
}
impl Taxonomy {
// TODO: take a Vec<&'a Page> if it makes a difference in terms of perf for actual sites
pub fn find_tags_and_categories(all_pages: Vec<Page>) -> (Taxonomy, Taxonomy) {
let mut tags = HashMap::new();
let mut categories = HashMap::new();
// Find all the tags/categories first
for page in all_pages {
if let Some(ref category) = page.meta.category {
categories
.entry(category.to_string())
.or_insert_with(|| vec![])
.push(page.clone());
}
if let Some(ref t) = page.meta.tags {
for tag in t {
tags
.entry(tag.to_string())
.or_insert_with(|| vec![])
.push(page.clone());
}
}
}
// Then make TaxonomyItem out of them, after sorting it
let tags_taxonomy = Taxonomy::new(TaxonomyKind::Tags, tags);
let categories_taxonomy = Taxonomy::new(TaxonomyKind::Categories, categories);
(tags_taxonomy, categories_taxonomy)
}
fn new(kind: TaxonomyKind, items: HashMap<String, Vec<Page>>) -> Taxonomy {
let mut sorted_items = vec![];
for (name, pages) in &items {
sorted_items.push(
TaxonomyItem::new(name, pages.clone())
);
}
sorted_items.sort_by(|a, b| b.pages.len().cmp(&a.pages.len()));
Taxonomy {
kind,
items: sorted_items,
}
}
pub fn len(&self) -> usize {
self.items.len()
}
pub fn get_single_item_name(&self) -> String {
match self.kind {
TaxonomyKind::Tags => "tag".to_string(),
TaxonomyKind::Categories => "category".to_string(),
}
}
pub fn get_list_name(&self) -> String {
match self.kind {
TaxonomyKind::Tags => "tags".to_string(),
TaxonomyKind::Categories => "categories".to_string(),
}
}
pub fn render_single_item(&self, item: &TaxonomyItem, tera: &Tera, config: &Config) -> Result<String> {
let name = self.get_single_item_name();
let mut context = Context::new();
context.add("config", config);
// TODO: how to sort categories and tag content?
// Have a setting in config.toml or a _category.md and _tag.md
// The latter is more in line with the rest of Gutenberg but order ordering
// doesn't really work across sections.
context.add(&name, item);
context.add("current_url", &config.make_permalink(&format!("{}/{}", name, item.slug)));
context.add("current_path", &format!("/{}/{}", name, item.slug));
tera.render(&format!("{}.html", name), &context)
.chain_err(|| format!("Failed to render {} page.", name))
}
pub fn render_list(&self, tera: &Tera, config: &Config) -> Result<String> {
let name = self.get_list_name();
let mut context = Context::new();
context.add("config", config);
context.add(&name, &self.items);
context.add("current_url", &config.make_permalink(&name));
context.add("current_path", &name);
tera.render(&format!("{}.html", name), &context)
.chain_err(|| format!("Failed to render {} page.", name))
}
}

View file

@ -24,8 +24,6 @@ pub struct PageFrontMatter {
pub url: Option<String>, pub url: Option<String>,
/// Tags, not to be confused with categories /// Tags, not to be confused with categories
pub tags: Option<Vec<String>>, pub tags: Option<Vec<String>>,
/// Whether this page is a draft and should be published or not
pub draft: Option<bool>,
/// Only one category allowed. Can't be an empty string if present /// Only one category allowed. Can't be an empty string if present
pub category: Option<String>, pub category: Option<String>,
/// Integer to use to order content. Lowest is at the bottom, highest first /// Integer to use to order content. Lowest is at the bottom, highest first
@ -100,7 +98,6 @@ impl Default for PageFrontMatter {
slug: None, slug: None,
url: None, url: None,
tags: None, tags: None,
draft: None,
category: None, category: None,
order: None, order: None,
template: None, template: None,

View file

@ -11,6 +11,15 @@ pub fn create_file<P: AsRef<Path>>(path: P, content: &str) -> Result<()> {
Ok(()) Ok(())
} }
/// Create a directory at the given path if it doesn't exist already
pub fn ensure_directory_exists<P: AsRef<Path>>(path: P) -> Result<()> {
let path = path.as_ref();
if !path.exists() {
create_directory(&path)?;
}
Ok(())
}
/// Very similar to `create_dir` from the std except it checks if the folder /// Very similar to `create_dir` from the std except it checks if the folder
/// exists before creating it /// exists before creating it
pub fn create_directory<P: AsRef<Path>>(path: P) -> Result<()> { pub fn create_directory<P: AsRef<Path>>(path: P) -> Result<()> {

View file

@ -19,7 +19,7 @@ extern crate base64;
#[cfg(test)] #[cfg(test)]
extern crate tempdir; extern crate tempdir;
mod utils; mod fs;
mod config; mod config;
pub mod errors; pub mod errors;
mod front_matter; mod front_matter;
@ -33,5 +33,5 @@ pub use site::{Site};
pub use config::{Config, get_config}; pub use config::{Config, get_config};
pub use front_matter::{PageFrontMatter, SectionFrontMatter, split_page_content, split_section_content}; pub use front_matter::{PageFrontMatter, SectionFrontMatter, split_page_content, split_section_content};
pub use content::{Page, Section, SortBy, sort_pages, populate_previous_and_next_pages}; pub use content::{Page, Section, SortBy, sort_pages, populate_previous_and_next_pages};
pub use utils::{create_file}; pub use fs::{create_file};
pub use markdown::markdown_to_html; pub use markdown::markdown_to_html;

View file

@ -1,47 +1,24 @@
use std::collections::{HashMap}; use std::collections::HashMap;
use std::iter::FromIterator;
use std::fs::{remove_dir_all, copy, create_dir_all}; use std::fs::{remove_dir_all, copy, create_dir_all};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use glob::glob; use glob::glob;
use tera::{Tera, Context}; use tera::{Tera, Context};
use slug::slugify;
use walkdir::WalkDir; use walkdir::WalkDir;
use errors::{Result, ResultExt}; use errors::{Result, ResultExt};
use config::{Config, get_config}; use config::{Config, get_config};
use utils::{create_file, create_directory}; use fs::{create_file, create_directory, ensure_directory_exists};
use content::{Page, Section, Paginator, SortBy, populate_previous_and_next_pages, sort_pages}; use content::{Page, Section, Paginator, SortBy, Taxonomy, populate_previous_and_next_pages, sort_pages};
use templates::{GUTENBERG_TERA, global_fns, render_redirect_template}; use templates::{GUTENBERG_TERA, global_fns, render_redirect_template};
#[derive(Debug, PartialEq)]
enum RenderList {
Tags,
Categories,
}
/// A tag or category
#[derive(Debug, Serialize, PartialEq)]
struct ListItem {
name: String,
slug: String,
count: usize,
}
impl ListItem {
pub fn new(name: &str, count: usize) -> ListItem {
ListItem {
name: name.to_string(),
slug: slugify(name),
count: count,
}
}
}
#[derive(Debug)] #[derive(Debug)]
pub struct Site { pub struct Site {
/// The base path of the gutenberg site
pub base_path: PathBuf, pub base_path: PathBuf,
/// The parsed config for the site
pub config: Config, pub config: Config,
pub pages: HashMap<PathBuf, Page>, pub pages: HashMap<PathBuf, Page>,
pub sections: HashMap<PathBuf, Section>, pub sections: HashMap<PathBuf, Section>,
@ -49,8 +26,8 @@ pub struct Site {
live_reload: bool, live_reload: bool,
output_path: PathBuf, output_path: PathBuf,
static_path: PathBuf, static_path: PathBuf,
pub tags: HashMap<String, Vec<PathBuf>>, pub tags: Option<Taxonomy>,
pub categories: HashMap<String, Vec<PathBuf>>, pub categories: Option<Taxonomy>,
/// A map of all .md files (section and pages) and their permalink /// A map of all .md files (section and pages) and their permalink
/// We need that if there are relative links in the content that need to be resolved /// We need that if there are relative links in the content that need to be resolved
pub permalinks: HashMap<String, String>, pub permalinks: HashMap<String, String>,
@ -75,8 +52,8 @@ impl Site {
live_reload: false, live_reload: false,
output_path: path.join("public"), output_path: path.join("public"),
static_path: path.join("static"), static_path: path.join("static"),
tags: HashMap::new(), tags: None,
categories: HashMap::new(), categories: None,
permalinks: HashMap::new(), permalinks: HashMap::new(),
}; };
@ -88,15 +65,6 @@ impl Site {
self.live_reload = true; self.live_reload = true;
} }
/// Gets the path of all ignored pages in the site
/// Used for reporting them in the CLI
pub fn get_ignored_pages(&self) -> Vec<PathBuf> {
self.sections
.values()
.flat_map(|s| s.ignored_pages.iter().map(|p| p.file.path.clone()))
.collect()
}
/// Get all the orphan (== without section) pages in the site /// Get all the orphan (== without section) pages in the site
pub fn get_all_orphan_pages(&self) -> Vec<&Page> { pub fn get_all_orphan_pages(&self) -> Vec<&Page> {
let mut pages_in_sections = vec![]; let mut pages_in_sections = vec![];
@ -115,17 +83,6 @@ impl Site {
orphans orphans
} }
/// Finds the section that contains the page given if there is one
pub fn find_parent_section(&self, page: &Page) -> Option<&Section> {
for section in self.sections.values() {
if section.is_child_page(page) {
return Some(section)
}
}
None
}
/// Used by tests to change the output path to a tmp dir /// Used by tests to change the output path to a tmp dir
#[doc(hidden)] #[doc(hidden)]
pub fn set_output_path<P: AsRef<Path>>(&mut self, path: P) { pub fn set_output_path<P: AsRef<Path>>(&mut self, path: P) {
@ -251,24 +208,23 @@ impl Site {
} }
} }
/// Separated from `parse` for easier testing /// Find all the tags and categories if it's asked in the config
pub fn populate_tags_and_categories(&mut self) { pub fn populate_tags_and_categories(&mut self) {
for page in self.pages.values() { let generate_tags_pages = self.config.generate_tags_pages.unwrap();
if let Some(ref category) = page.meta.category { let generate_categories_pages = self.config.generate_categories_pages.unwrap();
self.categories if !generate_tags_pages && !generate_categories_pages {
.entry(category.to_string()) return;
.or_insert_with(|| vec![]) }
.push(page.file.path.clone());
}
if let Some(ref tags) = page.meta.tags { // TODO: can we pass a reference?
for tag in tags { let (tags, categories) = Taxonomy::find_tags_and_categories(
self.tags self.pages.values().cloned().collect::<Vec<_>>()
.entry(tag.to_string()) );
.or_insert_with(|| vec![]) if generate_tags_pages {
.push(page.file.path.clone()); self.tags = Some(tags);
} }
} if generate_categories_pages {
self.categories = Some(categories);
} }
} }
@ -284,14 +240,6 @@ impl Site {
html html
} }
fn ensure_public_directory_exists(&self) -> Result<()> {
let public = self.output_path.clone();
if !public.exists() {
create_directory(&public)?;
}
Ok(())
}
/// Copy static file to public directory. /// Copy static file to public directory.
pub fn copy_static_file<P: AsRef<Path>>(&self, path: P) -> Result<()> { pub fn copy_static_file<P: AsRef<Path>>(&self, path: P) -> Result<()> {
let relative_path = path.as_ref().strip_prefix(&self.static_path).unwrap(); let relative_path = path.as_ref().strip_prefix(&self.static_path).unwrap();
@ -333,7 +281,7 @@ impl Site {
/// Renders a single content page /// Renders a single content page
pub fn render_page(&self, page: &Page) -> Result<()> { pub fn render_page(&self, page: &Page) -> Result<()> {
self.ensure_public_directory_exists()?; ensure_directory_exists(&self.output_path)?;
// Copy the nesting of the content directory if we have sections for that page // Copy the nesting of the content directory if we have sections for that page
let mut current_path = self.output_path.to_path_buf(); let mut current_path = self.output_path.to_path_buf();
@ -362,7 +310,7 @@ impl Site {
Ok(()) Ok(())
} }
/// Builds the site to the `public` directory after deleting it /// Deletes the `public` directory and builds the site
pub fn build(&self) -> Result<()> { pub fn build(&self) -> Result<()> {
self.clean()?; self.clean()?;
self.render_sections()?; self.render_sections()?;
@ -382,98 +330,45 @@ impl Site {
/// Renders robots.txt /// Renders robots.txt
pub fn render_robots(&self) -> Result<()> { pub fn render_robots(&self) -> Result<()> {
self.ensure_public_directory_exists()?; ensure_directory_exists(&self.output_path)?;
create_file( create_file(
self.output_path.join("robots.txt"), self.output_path.join("robots.txt"),
&self.tera.render("robots.txt", &Context::new())? &self.tera.render("robots.txt", &Context::new())?
) )
} }
/// Renders all categories if the config allows it /// Renders all categories and the single category pages if there are some
pub fn render_categories(&self) -> Result<()> { pub fn render_categories(&self) -> Result<()> {
if self.config.generate_categories_pages.unwrap() { if let Some(ref categories) = self.categories {
self.render_categories_and_tags(RenderList::Categories) self.render_taxonomy(categories)?;
} else {
Ok(())
} }
Ok(())
} }
/// Renders all tags if the config allows it /// Renders all tags and the single tag pages if there are some
pub fn render_tags(&self) -> Result<()> { pub fn render_tags(&self) -> Result<()> {
if self.config.generate_tags_pages.unwrap() { if let Some(ref tags) = self.tags {
self.render_categories_and_tags(RenderList::Tags) self.render_taxonomy(tags)?;
} else {
Ok(())
} }
Ok(())
} }
/// Render the /{categories, list} pages and each individual category/tag page fn render_taxonomy(&self, taxonomy: &Taxonomy) -> Result<()> {
/// They are the same thing fundamentally, a list of pages with something in common ensure_directory_exists(&self.output_path)?;
/// TODO: revisit this function, lots of things have changed since then
fn render_categories_and_tags(&self, kind: RenderList) -> Result<()> {
let items = match kind {
RenderList::Categories => &self.categories,
RenderList::Tags => &self.tags,
};
if items.is_empty() { let output_path = self.output_path.join(&taxonomy.get_list_name());
return Ok(()); let list_output = taxonomy.render_list(&self.tera, &self.config)?;
}
let (list_tpl_name, single_tpl_name, name, var_name) = if kind == RenderList::Categories {
("categories.html", "category.html", "categories", "category")
} else {
("tags.html", "tag.html", "tags", "tag")
};
self.ensure_public_directory_exists()?;
// Create the categories/tags directory first
let public = self.output_path.clone();
let mut output_path = public.to_path_buf();
output_path.push(name);
create_directory(&output_path)?; create_directory(&output_path)?;
// Then render the index page for that kind.
// We sort by number of page in that category/tag
let mut sorted_items = vec![];
for (item, count) in Vec::from_iter(items).into_iter().map(|(a, b)| (a, b.len())) {
sorted_items.push(ListItem::new(item, count));
}
sorted_items.sort_by(|a, b| b.count.cmp(&a.count));
let mut context = Context::new();
context.add(name, &sorted_items);
context.add("config", &self.config);
context.add("current_url", &self.config.make_permalink(name));
context.add("current_path", &format!("/{}", name));
// And render it immediately
let list_output = self.tera.render(list_tpl_name, &context)?;
create_file(output_path.join("index.html"), &self.inject_livereload(list_output))?; create_file(output_path.join("index.html"), &self.inject_livereload(list_output))?;
// Now, each individual item for item in &taxonomy.items {
for (item_name, pages_paths) in items.iter() { let single_output = taxonomy.render_single_item(item, &self.tera, &self.config)?;
let pages: Vec<&Page> = self.pages
.iter()
.filter(|&(path, _)| pages_paths.contains(path))
.map(|(_, page)| page)
.collect();
// TODO: how to sort categories and tag content?
// Have a setting in config.toml or a _category.md and _tag.md
// The latter is more in line with the rest of Gutenberg but order ordering
// doesn't really work across sections.
let mut context = Context::new(); create_directory(&output_path.join(&item.slug))?;
let slug = slugify(&item_name);
context.add(var_name, &item_name);
context.add(&format!("{}_slug", var_name), &slug);
context.add("pages", &pages);
context.add("config", &self.config);
context.add("current_url", &self.config.make_permalink(&format!("{}/{}", name, slug)));
context.add("current_path", &format!("/{}/{}", name, slug));
let single_output = self.tera.render(single_tpl_name, &context)?;
create_directory(&output_path.join(&slug))?;
create_file( create_file(
output_path.join(&slug).join("index.html"), output_path.join(&item.slug).join("index.html"),
&self.inject_livereload(single_output) &self.inject_livereload(single_output)
)?; )?;
} }
@ -483,28 +378,31 @@ impl Site {
/// What it says on the tin /// What it says on the tin
pub fn render_sitemap(&self) -> Result<()> { pub fn render_sitemap(&self) -> Result<()> {
self.ensure_public_directory_exists()?; ensure_directory_exists(&self.output_path)?;
let mut context = Context::new(); let mut context = Context::new();
context.add("pages", &self.pages.values().collect::<Vec<&Page>>()); context.add("pages", &self.pages.values().collect::<Vec<&Page>>());
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.config.generate_categories_pages.unwrap() && !self.categories.is_empty() { if let Some(ref c) = self.categories {
categories.push(self.config.make_permalink("categories")); let name = c.get_list_name();
for category in self.categories.keys() { categories.push(self.config.make_permalink(&name));
for item in &c.items {
categories.push( categories.push(
self.config.make_permalink(&format!("categories/{}", slugify(category))) self.config.make_permalink(&format!("{}/{}", &name, item.slug))
); );
} }
} }
context.add("categories", &categories); context.add("categories", &categories);
let mut tags = vec![]; let mut tags = vec![];
if self.config.generate_tags_pages.unwrap() && !self.tags.is_empty() { if let Some(ref t) = self.tags {
tags.push(self.config.make_permalink("tags")); let name = t.get_list_name();
for tag in self.tags.keys() { tags.push(self.config.make_permalink(&name));
for item in &t.items {
tags.push( tags.push(
self.config.make_permalink(&format!("tags/{}", slugify(tag))) self.config.make_permalink(&format!("{}/{}", &name, item.slug))
); );
} }
} }
@ -518,7 +416,7 @@ impl Site {
} }
pub fn render_rss_feed(&self) -> Result<()> { pub fn render_rss_feed(&self) -> Result<()> {
self.ensure_public_directory_exists()?; ensure_directory_exists(&self.output_path)?;
let mut context = Context::new(); let mut context = Context::new();
let pages = self.pages.values() let pages = self.pages.values()
@ -561,7 +459,7 @@ impl Site {
/// Renders a single section /// Renders a single section
pub fn render_section(&self, section: &Section, render_pages: bool) -> Result<()> { pub fn render_section(&self, section: &Section, render_pages: bool) -> Result<()> {
self.ensure_public_directory_exists()?; ensure_directory_exists(&self.output_path)?;
let public = self.output_path.clone(); let public = self.output_path.clone();
let mut output_path = public.to_path_buf(); let mut output_path = public.to_path_buf();
@ -611,7 +509,7 @@ impl Site {
/// Renders all pages that do not belong to any sections /// Renders all pages that do not belong to any sections
pub fn render_orphan_pages(&self) -> Result<()> { pub fn render_orphan_pages(&self) -> Result<()> {
self.ensure_public_directory_exists()?; ensure_directory_exists(&self.output_path)?;
for page in self.get_all_orphan_pages() { for page in self.get_all_orphan_pages() {
self.render_page(page)?; self.render_page(page)?;
@ -622,7 +520,7 @@ impl Site {
/// Renders a list of pages when the section/index is wanting pagination. /// Renders a list of pages when the section/index is wanting pagination.
fn render_paginated(&self, output_path: &Path, section: &Section) -> Result<()> { fn render_paginated(&self, output_path: &Path, section: &Section) -> Result<()> {
self.ensure_public_directory_exists()?; ensure_directory_exists(&self.output_path)?;
let paginate_path = match section.meta.paginate_path { let paginate_path = match section.meta.paginate_path {
Some(ref s) => s.clone(), Some(ref s) => s.clone(),

View file

@ -1,3 +1,3 @@
{% for category in categories %} {% for category in categories %}
{{ category.name }} {{ category.slug }} {{ category.count }} {{ category.name }} {{ category.slug }} {{ category.pages | length }}
{% endfor %} {% endfor %}

View file

@ -1,7 +1,7 @@
Category: {{ category }} Category: {{ category.name }}
{% for page in pages %} {% for page in category.pages %}
<article> <article>
<h3 class="post__title"><a href="{{ page.permalink }}">{{ page.title }}</a></h3> <h3 class="post__title"><a href="{{ page.permalink }}">{{ page.title }}</a></h3>
</article> </article>

View file

@ -1,6 +1,6 @@
Tag: {{ tag }} Tag: {{ tag.name }}
{% for page in pages %} {% for page in tag.pages %}
<article> <article>
<h3 class="post__title"><a href="{{ page.permalink }}">{{ page.title }}</a></h3> <h3 class="post__title"><a href="{{ page.permalink }}">{{ page.title }}</a></h3>
</article> </article>

View file

@ -1,3 +1,3 @@
{% for tag in tags %} {% for tag in tags %}
{{ tag.name }} {{ tag.slug }} {{ tag.count }} {{ tag.name }} {{ tag.slug }} {{ tag.pages | length }}
{% endfor %} {% endfor %}

View file

@ -190,7 +190,7 @@ fn can_build_site_with_categories() {
site.build().unwrap(); site.build().unwrap();
assert!(Path::new(&public).exists()); assert!(Path::new(&public).exists());
assert_eq!(site.categories.len(), 2); assert_eq!(site.categories.unwrap().len(), 2);
assert!(file_exists!(public, "index.html")); assert!(file_exists!(public, "index.html"));
assert!(file_exists!(public, "sitemap.xml")); assert!(file_exists!(public, "sitemap.xml"));
@ -243,7 +243,7 @@ fn can_build_site_with_tags() {
site.build().unwrap(); site.build().unwrap();
assert!(Path::new(&public).exists()); assert!(Path::new(&public).exists());
assert_eq!(site.tags.len(), 3); assert_eq!(site.tags.unwrap().len(), 3);
assert!(file_exists!(public, "index.html")); assert!(file_exists!(public, "index.html"));
assert!(file_exists!(public, "sitemap.xml")); assert!(file_exists!(public, "sitemap.xml"));