From a89b30073c48d5c524d3cd15e9c8cf4f3f1e083b Mon Sep 17 00:00:00 2001 From: Stan Rozenraukh Date: Tue, 26 Nov 2019 14:36:52 -0500 Subject: [PATCH] Section extra -> SitemapEntry (#850) --- components/front_matter/src/section.rs | 19 +++++++++++-------- components/library/src/content/ser.rs | 2 +- components/site/src/lib.rs | 2 +- components/site/src/sitemap.rs | 12 ++++++++---- components/site/tests/site.rs | 14 ++++++++++++++ .../posts/tutorials/programming/_index.md | 3 +++ 6 files changed, 38 insertions(+), 14 deletions(-) diff --git a/components/front_matter/src/section.rs b/components/front_matter/src/section.rs index ffbcaf22..126e59d8 100644 --- a/components/front_matter/src/section.rs +++ b/components/front_matter/src/section.rs @@ -1,11 +1,9 @@ -use std::collections::HashMap; - -use tera::Value; +use tera::{Map, Value}; use toml; -use errors::Result; - use super::{InsertAnchor, SortBy}; +use errors::Result; +use utils::de::fix_toml_dates; static DEFAULT_PAGINATE_PATH: &str = "page"; @@ -63,16 +61,21 @@ pub struct SectionFrontMatter { #[serde(skip_serializing)] pub aliases: Vec, /// Any extra parameter present in the front matter - pub extra: HashMap, + pub extra: Map, } impl SectionFrontMatter { pub fn parse(toml: &str) -> Result { - let f: SectionFrontMatter = match toml::from_str(toml) { + let mut f: SectionFrontMatter = match toml::from_str(toml) { Ok(d) => d, Err(e) => bail!(e), }; + f.extra = match fix_toml_dates(f.extra) { + Value::Object(o) => o, + _ => unreachable!("Got something other than a table in section extra"), + }; + Ok(f) } @@ -102,7 +105,7 @@ impl Default for SectionFrontMatter { transparent: false, page_template: None, aliases: Vec::new(), - extra: HashMap::new(), + extra: Map::new(), } } } diff --git a/components/library/src/content/ser.rs b/components/library/src/content/ser.rs index 4d7938d9..e5177c50 100644 --- a/components/library/src/content/ser.rs +++ b/components/library/src/content/ser.rs @@ -206,7 +206,7 @@ pub struct SerializingSection<'a> { ancestors: Vec, title: &'a Option, description: &'a Option, - extra: &'a HashMap, + extra: &'a Map, path: &'a str, components: &'a [String], toc: &'a [Heading], diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index b2e85f41..be71ed4f 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -20,7 +20,7 @@ extern crate utils; #[cfg(test)] extern crate tempfile; -mod sitemap; +pub mod sitemap; use std::collections::HashMap; use std::fs::{copy, create_dir_all, remove_dir_all}; diff --git a/components/site/src/sitemap.rs b/components/site/src/sitemap.rs index 72152f7f..c0e8530c 100644 --- a/components/site/src/sitemap.rs +++ b/components/site/src/sitemap.rs @@ -11,9 +11,9 @@ use tera::{Map, Value}; /// for examples so we trim down all entries to only that #[derive(Debug, Serialize)] pub struct SitemapEntry<'a> { - permalink: Cow<'a, str>, - date: Option, - extra: Option<&'a Map>, + pub permalink: Cow<'a, str>, + pub date: Option, + pub extra: Option<&'a Map>, } // Hash/Eq is not implemented for tera::Map but in our case we only care about the permalink @@ -77,7 +77,11 @@ pub fn find_entries<'a>( .sections_values() .iter() .filter(|s| s.meta.render) - .map(|s| SitemapEntry::new(Cow::Borrowed(&s.permalink), None)) + .map(|s| { + let mut entry = SitemapEntry::new(Cow::Borrowed(&s.permalink), None); + entry.add_extra(&s.meta.extra); + entry + }) .collect::>(); for section in library.sections_values().iter().filter(|s| s.meta.paginate_by.is_some()) { diff --git a/components/site/tests/site.rs b/components/site/tests/site.rs index e061e19b..a85a0bfc 100644 --- a/components/site/tests/site.rs +++ b/components/site/tests/site.rs @@ -8,6 +8,7 @@ use std::path::Path; use common::{build_site, build_site_with_setup}; use config::Taxonomy; +use site::sitemap; use site::Site; #[test] @@ -87,6 +88,19 @@ fn can_parse_site() { .unwrap(); assert_eq!(prog_section.subsections.len(), 0); assert_eq!(prog_section.pages.len(), 2); + + // Testing extra variables in sections & sitemaps + // Regression test for #https://github.com/getzola/zola/issues/842 + assert_eq!( + prog_section.meta.extra.get("we_have_extra").and_then(|s| s.as_str()), + Some("variables") + ); + let sitemap_entries = sitemap::find_entries(&library, &site.taxonomies[..], &site.config); + let sitemap_entry = sitemap_entries + .iter() + .find(|e| e.permalink.ends_with("tutorials/programming/")) + .expect("expected to find programming section in sitemap"); + assert_eq!(Some(&prog_section.meta.extra), sitemap_entry.extra); } #[test] diff --git a/test_site/content/posts/tutorials/programming/_index.md b/test_site/content/posts/tutorials/programming/_index.md index 6515620e..f16966cf 100644 --- a/test_site/content/posts/tutorials/programming/_index.md +++ b/test_site/content/posts/tutorials/programming/_index.md @@ -2,4 +2,7 @@ title = "Programming" sort_by = "weight" weight = 1 + +[extra] +we_have_extra = "variables" +++