Section extra -> SitemapEntry (#850)

This commit is contained in:
Stan Rozenraukh 2019-11-26 14:36:52 -05:00 committed by Vincent Prouillet
parent 1a6edbcf63
commit a89b30073c
6 changed files with 38 additions and 14 deletions

View file

@ -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<String>,
/// Any extra parameter present in the front matter
pub extra: HashMap<String, Value>,
pub extra: Map<String, Value>,
}
impl SectionFrontMatter {
pub fn parse(toml: &str) -> Result<SectionFrontMatter> {
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(),
}
}
}

View file

@ -206,7 +206,7 @@ pub struct SerializingSection<'a> {
ancestors: Vec<String>,
title: &'a Option<String>,
description: &'a Option<String>,
extra: &'a HashMap<String, Value>,
extra: &'a Map<String, Value>,
path: &'a str,
components: &'a [String],
toc: &'a [Heading],

View file

@ -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};

View file

@ -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<String>,
extra: Option<&'a Map<String, Value>>,
pub permalink: Cow<'a, str>,
pub date: Option<String>,
pub extra: Option<&'a Map<String, Value>>,
}
// 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::<Vec<_>>();
for section in library.sections_values().iter().filter(|s| s.meta.paginate_by.is_some()) {

View file

@ -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]

View file

@ -2,4 +2,7 @@
title = "Programming"
sort_by = "weight"
weight = 1
[extra]
we_have_extra = "variables"
+++