Skip to content

Commit a89b300

Browse files
stanistanKeats
authored andcommitted
Section extra -> SitemapEntry (#850)
1 parent 1a6edbc commit a89b300

File tree

6 files changed

+38
-14
lines changed

6 files changed

+38
-14
lines changed

components/front_matter/src/section.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
use std::collections::HashMap;
2-
3-
use tera::Value;
1+
use tera::{Map, Value};
42
use toml;
53

6-
use errors::Result;
7-
84
use super::{InsertAnchor, SortBy};
5+
use errors::Result;
6+
use utils::de::fix_toml_dates;
97

108
static DEFAULT_PAGINATE_PATH: &str = "page";
119

@@ -63,16 +61,21 @@ pub struct SectionFrontMatter {
6361
#[serde(skip_serializing)]
6462
pub aliases: Vec<String>,
6563
/// Any extra parameter present in the front matter
66-
pub extra: HashMap<String, Value>,
64+
pub extra: Map<String, Value>,
6765
}
6866

6967
impl SectionFrontMatter {
7068
pub fn parse(toml: &str) -> Result<SectionFrontMatter> {
71-
let f: SectionFrontMatter = match toml::from_str(toml) {
69+
let mut f: SectionFrontMatter = match toml::from_str(toml) {
7270
Ok(d) => d,
7371
Err(e) => bail!(e),
7472
};
7573

74+
f.extra = match fix_toml_dates(f.extra) {
75+
Value::Object(o) => o,
76+
_ => unreachable!("Got something other than a table in section extra"),
77+
};
78+
7679
Ok(f)
7780
}
7881

@@ -102,7 +105,7 @@ impl Default for SectionFrontMatter {
102105
transparent: false,
103106
page_template: None,
104107
aliases: Vec::new(),
105-
extra: HashMap::new(),
108+
extra: Map::new(),
106109
}
107110
}
108111
}

components/library/src/content/ser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ pub struct SerializingSection<'a> {
206206
ancestors: Vec<String>,
207207
title: &'a Option<String>,
208208
description: &'a Option<String>,
209-
extra: &'a HashMap<String, Value>,
209+
extra: &'a Map<String, Value>,
210210
path: &'a str,
211211
components: &'a [String],
212212
toc: &'a [Heading],

components/site/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ extern crate utils;
2020
#[cfg(test)]
2121
extern crate tempfile;
2222

23-
mod sitemap;
23+
pub mod sitemap;
2424

2525
use std::collections::HashMap;
2626
use std::fs::{copy, create_dir_all, remove_dir_all};

components/site/src/sitemap.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ use tera::{Map, Value};
1111
/// for examples so we trim down all entries to only that
1212
#[derive(Debug, Serialize)]
1313
pub struct SitemapEntry<'a> {
14-
permalink: Cow<'a, str>,
15-
date: Option<String>,
16-
extra: Option<&'a Map<String, Value>>,
14+
pub permalink: Cow<'a, str>,
15+
pub date: Option<String>,
16+
pub extra: Option<&'a Map<String, Value>>,
1717
}
1818

1919
// 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>(
7777
.sections_values()
7878
.iter()
7979
.filter(|s| s.meta.render)
80-
.map(|s| SitemapEntry::new(Cow::Borrowed(&s.permalink), None))
80+
.map(|s| {
81+
let mut entry = SitemapEntry::new(Cow::Borrowed(&s.permalink), None);
82+
entry.add_extra(&s.meta.extra);
83+
entry
84+
})
8185
.collect::<Vec<_>>();
8286

8387
for section in library.sections_values().iter().filter(|s| s.meta.paginate_by.is_some()) {

components/site/tests/site.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::path::Path;
88

99
use common::{build_site, build_site_with_setup};
1010
use config::Taxonomy;
11+
use site::sitemap;
1112
use site::Site;
1213

1314
#[test]
@@ -87,6 +88,19 @@ fn can_parse_site() {
8788
.unwrap();
8889
assert_eq!(prog_section.subsections.len(), 0);
8990
assert_eq!(prog_section.pages.len(), 2);
91+
92+
// Testing extra variables in sections & sitemaps
93+
// Regression test for #https://github.com/getzola/zola/issues/842
94+
assert_eq!(
95+
prog_section.meta.extra.get("we_have_extra").and_then(|s| s.as_str()),
96+
Some("variables")
97+
);
98+
let sitemap_entries = sitemap::find_entries(&library, &site.taxonomies[..], &site.config);
99+
let sitemap_entry = sitemap_entries
100+
.iter()
101+
.find(|e| e.permalink.ends_with("tutorials/programming/"))
102+
.expect("expected to find programming section in sitemap");
103+
assert_eq!(Some(&prog_section.meta.extra), sitemap_entry.extra);
90104
}
91105

92106
#[test]

test_site/content/posts/tutorials/programming/_index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
title = "Programming"
33
sort_by = "weight"
44
weight = 1
5+
6+
[extra]
7+
we_have_extra = "variables"
58
+++

0 commit comments

Comments
 (0)