Add categories and tags to sitemap

This commit is contained in:
Vincent Prouillet 2017-03-20 12:42:43 +09:00
parent fd10e77e39
commit f63ec475eb
6 changed files with 119 additions and 52 deletions

View file

@ -66,6 +66,15 @@ impl Config {
Config::parse(&content) Config::parse(&content)
} }
/// Makes a url, taking into account that the base url might have a trailing slash
pub fn make_permalink(&self, path: &str) -> String {
if self.base_url.ends_with('/') {
format!("{}{}", self.base_url, path)
} else {
format!("{}/{}", self.base_url, path)
}
}
} }
impl Default for Config { impl Default for Config {

View file

@ -173,11 +173,7 @@ impl Page {
} }
} }
page.permalink = if config.base_url.ends_with('/') { page.permalink = config.make_permalink(&page.url);
format!("{}{}", config.base_url, page.url)
} else {
format!("{}/{}", config.base_url, page.url)
};
Ok(page) Ok(page)
} }

View file

@ -52,11 +52,7 @@ impl Section {
section.url = section.components.join("/"); section.url = section.components.join("/");
section.permalink = section.components.join("/"); section.permalink = section.components.join("/");
section.permalink = if config.base_url.ends_with('/') { section.permalink = config.make_permalink(&section.url);
format!("{}{}", config.base_url, section.url)
} else {
format!("{}/{}", config.base_url, section.url)
};
Ok(section) Ok(section)
} }

View file

@ -60,6 +60,8 @@ pub struct Site {
pub templates: Tera, pub templates: Tera,
live_reload: bool, live_reload: bool,
output_path: PathBuf, output_path: PathBuf,
pub tags: HashMap<String, Vec<PathBuf>>,
pub categories: HashMap<String, Vec<PathBuf>>,
} }
impl Site { impl Site {
@ -80,8 +82,10 @@ impl Site {
templates: tera, templates: tera,
live_reload: false, live_reload: false,
output_path: PathBuf::from("public"), output_path: PathBuf::from("public"),
tags: HashMap::new(),
categories: HashMap::new(),
}; };
site.parse_site()?; site.parse()?;
Ok(site) Ok(site)
} }
@ -99,7 +103,7 @@ impl Site {
/// Reads all .md files in the `content` directory and create pages /// Reads all .md files in the `content` directory and create pages
/// out of them /// out of them
fn parse_site(&mut self) -> Result<()> { pub fn parse(&mut self) -> Result<()> {
let path = self.base_path.to_string_lossy().replace("\\", "/"); let path = self.base_path.to_string_lossy().replace("\\", "/");
let content_glob = format!("{}/{}", path, "content/**/*.md"); let content_glob = format!("{}/{}", path, "content/**/*.md");
@ -122,7 +126,6 @@ impl Site {
self.pages.insert(page.file_path.clone(), page); self.pages.insert(page.file_path.clone(), page);
} }
} }
// Find out the direct subsections of each subsection if there are some // Find out the direct subsections of each subsection if there are some
let mut grandparent_paths = HashMap::new(); let mut grandparent_paths = HashMap::new();
for section in sections.values() { for section in sections.values() {
@ -140,10 +143,32 @@ impl Site {
} }
self.sections = sections; self.sections = sections;
self.parse_tags_and_categories();
Ok(()) Ok(())
} }
/// Separated from `parse` for easier testing
pub fn parse_tags_and_categories(&mut self) {
for page in self.pages.values() {
if let Some(ref category) = page.meta.category {
self.categories
.entry(category.to_string())
.or_insert_with(|| vec![])
.push(page.file_path.clone());
}
if let Some(ref tags) = page.meta.tags {
for tag in tags {
self.tags
.entry(tag.to_string())
.or_insert_with(|| vec![])
.push(page.file_path.clone());
}
}
}
}
/// Inject live reload script tag if in live reload mode /// Inject live reload script tag if in live reload mode
fn inject_livereload(&self, html: String) -> String { fn inject_livereload(&self, html: String) -> String {
if self.live_reload { if self.live_reload {
@ -197,7 +222,7 @@ impl Site {
} }
pub fn rebuild_after_content_change(&mut self) -> Result<()> { pub fn rebuild_after_content_change(&mut self) -> Result<()> {
self.parse_site()?; self.parse()?;
self.build() self.build()
} }
@ -213,8 +238,6 @@ impl Site {
} }
let mut pages = vec![]; let mut pages = vec![];
let mut category_pages: HashMap<String, Vec<&Page>> = HashMap::new();
let mut tag_pages: HashMap<String, Vec<&Page>> = HashMap::new();
// First we render the pages themselves // First we render the pages themselves
for page in self.pages.values() { for page in self.pages.values() {
@ -243,21 +266,11 @@ impl Site {
} }
pages.push(page); pages.push(page);
if let Some(ref category) = page.meta.category {
category_pages.entry(category.to_string()).or_insert_with(|| vec![]).push(page);
}
if let Some(ref tags) = page.meta.tags {
for tag in tags {
tag_pages.entry(tag.to_string()).or_insert_with(|| vec![]).push(page);
}
}
} }
// Outputting categories and pages // Outputting categories and pages
self.render_categories_and_tags(RenderList::Categories, &category_pages)?; self.render_categories_and_tags(RenderList::Categories)?;
self.render_categories_and_tags(RenderList::Tags, &tag_pages)?; 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();
@ -275,48 +288,63 @@ impl Site {
self.clean()?; self.clean()?;
self.build_pages()?; self.build_pages()?;
self.render_sitemap()?; self.render_sitemap()?;
if self.config.generate_rss.unwrap() { if self.config.generate_rss.unwrap() {
self.render_rss_feed()?; self.render_rss_feed()?;
} }
self.render_sections()?; self.render_sections()?;
self.copy_static_directory() self.copy_static_directory()
} }
/// Render the /{categories, list} pages and each individual category/tag page /// Render the /{categories, list} pages and each individual category/tag page
fn render_categories_and_tags(&self, kind: RenderList, container: &HashMap<String, Vec<&Page>>) -> Result<()> { /// They are the same thing fundamentally, a list of pages with something in common
if container.is_empty() { 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() {
return Ok(()); return Ok(());
} }
let (name, list_tpl_name, single_tpl_name, var_name) = if kind == RenderList::Categories { let (list_tpl_name, single_tpl_name, name, var_name) = if kind == RenderList::Categories {
("categories", "categories.html", "category.html", "category") ("categories.html", "category.html", "categories", "category")
} else { } else {
("tags", "tags.html", "tag.html", "tag") ("tags.html", "tag.html", "tags", "tag")
}; };
// Create the categories/tags directory first
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();
output_path.push(name); output_path.push(name);
create_directory(&output_path)?; create_directory(&output_path)?;
// First we render the list of categories/tags page // Then render the index page for that kind.
let mut sorted_container = vec![]; // We sort by number of page in that category/tag
for (item, count) in Vec::from_iter(container).into_iter().map(|(a, b)| (a, b.len())) { let mut sorted_items = vec![];
sorted_container.push(ListItem::new(item, count)); for (item, count) in Vec::from_iter(items).into_iter().map(|(a, b)| (a, b.len())) {
sorted_items.push(ListItem::new(&item, count));
} }
sorted_container.sort_by(|a, b| b.count.cmp(&a.count)); sorted_items.sort_by(|a, b| b.count.cmp(&a.count));
let mut context = Context::new(); let mut context = Context::new();
context.add(name, &sorted_container); context.add(name, &sorted_items);
context.add("config", &self.config); context.add("config", &self.config);
// And render it immediately
let list_output = self.templates.render(list_tpl_name, &context)?; let list_output = self.templates.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))?;
// and then each individual item // Now, each individual item
for (item_name, mut pages) in container.clone() { for (item_name, pages_paths) in items.iter() {
let mut context = Context::new(); let mut pages: Vec<&Page> = self.pages
.iter()
.filter(|&(path, _)| pages_paths.contains(&path))
.map(|(_, page)| page)
.collect();
pages.sort_by(|a, b| a.partial_cmp(b).unwrap()); pages.sort_by(|a, b| a.partial_cmp(b).unwrap());
let mut context = Context::new();
let slug = slugify(&item_name); let slug = slugify(&item_name);
context.add(var_name, &item_name); context.add(var_name, &item_name);
context.add(&format!("{}_slug", var_name), &slug); context.add(&format!("{}_slug", var_name), &slug);
@ -338,7 +366,29 @@ impl Site {
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>>());
// TODO: add categories and tags pages
let mut categories = vec![];
if !self.categories.is_empty() {
categories.push(self.config.make_permalink("categories"));
for category in self.categories.keys() {
categories.push(
self.config.make_permalink(&format!("categories/{}", slugify(category)))
);
}
}
context.add("categories", &categories);
let mut tags = vec![];
if !self.tags.is_empty() {
tags.push(self.config.make_permalink("tags"));
for tag in self.tags.keys() {
tags.push(
self.config.make_permalink(&format!("tags/{}", slugify(tag)))
);
}
}
context.add("tags", &tags);
let sitemap = self.templates.render("sitemap.xml", &context)?; let sitemap = self.templates.render("sitemap.xml", &context)?;
create_file(self.output_path.join("sitemap.xml"), &sitemap)?; create_file(self.output_path.join("sitemap.xml"), &sitemap)?;

View file

@ -12,4 +12,14 @@
<loc>{{ section.permalink | safe }}</loc> <loc>{{ section.permalink | safe }}</loc>
</url> </url>
{% endfor %} {% endfor %}
{% for category in categories %}
<url>
<loc>{{ category | safe }}</loc>
</url>
{% endfor %}
{% for tag in tags %}
<url>
<loc>{{ tag | safe }}</loc>
</url>
{% endfor %}
</urlset> </urlset>

View file

@ -164,22 +164,21 @@ fn test_can_build_site_with_categories() {
path.push("test_site"); path.push("test_site");
let mut site = Site::new(&path).unwrap(); let mut site = Site::new(&path).unwrap();
let mut i = 0; for (i, page) in site.pages.values_mut().enumerate() {
for (_, page) in &mut site.pages {
page.meta.category = if i % 2 == 0 { page.meta.category = if i % 2 == 0 {
Some("A".to_string()) Some("A".to_string())
} else { } else {
Some("B".to_string()) Some("B".to_string())
}; };
i += 1;
} }
site.parse_tags_and_categories();
let tmp_dir = TempDir::new("example").expect("create temp dir"); let tmp_dir = TempDir::new("example").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);
site.build().unwrap(); site.build().unwrap();
assert!(Path::new(&public).exists()); assert!(Path::new(&public).exists());
assert_eq!(site.categories.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"));
@ -202,6 +201,10 @@ fn test_can_build_site_with_categories() {
assert!(file_exists!(public, "categories/b/index.html")); assert!(file_exists!(public, "categories/b/index.html"));
// Tags aren't // Tags aren't
assert_eq!(file_exists!(public, "tags/index.html"), false); assert_eq!(file_exists!(public, "tags/index.html"), false);
// Categories are in the sitemap
assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/categories</loc>"));
assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/categories/a</loc>"));
} }
#[test] #[test]
@ -210,15 +213,14 @@ fn test_can_build_site_with_tags() {
path.push("test_site"); path.push("test_site");
let mut site = Site::new(&path).unwrap(); let mut site = Site::new(&path).unwrap();
let mut i = 0; for (i, page) in site.pages.values_mut().enumerate() {
for (_, page) in &mut site.pages {
page.meta.tags = if i % 2 == 0 { page.meta.tags = if i % 2 == 0 {
Some(vec!["tag1".to_string(), "tag2".to_string()]) Some(vec!["tag1".to_string(), "tag2".to_string()])
} else { } else {
Some(vec!["tag with space".to_string()]) Some(vec!["tag with space".to_string()])
}; };
i += 1;
} }
site.parse_tags_and_categories();
let tmp_dir = TempDir::new("example").expect("create temp dir"); let tmp_dir = TempDir::new("example").expect("create temp dir");
let public = &tmp_dir.path().join("public"); let public = &tmp_dir.path().join("public");
@ -226,6 +228,7 @@ fn test_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!(file_exists!(public, "index.html")); assert!(file_exists!(public, "index.html"));
assert!(file_exists!(public, "sitemap.xml")); assert!(file_exists!(public, "sitemap.xml"));
@ -249,4 +252,7 @@ fn test_can_build_site_with_tags() {
assert!(file_exists!(public, "tags/tag-with-space/index.html")); assert!(file_exists!(public, "tags/tag-with-space/index.html"));
// Categories aren't // Categories aren't
assert_eq!(file_exists!(public, "categories/index.html"), false); assert_eq!(file_exists!(public, "categories/index.html"), false);
// Tags are in the sitemap
assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/tags</loc>"));
assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/tags/tag-with-space</loc>"));
} }