Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Section extra -> SitemapEntry #850

Merged
merged 1 commit into from
Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions components/front_matter/src/section.rs
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -102,7 +105,7 @@ impl Default for SectionFrontMatter {
transparent: false,
page_template: None,
aliases: Vec::new(),
extra: HashMap::new(),
extra: Map::new(),
}
}
}
2 changes: 1 addition & 1 deletion components/library/src/content/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
2 changes: 1 addition & 1 deletion components/site/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
12 changes: 8 additions & 4 deletions components/site/src/sitemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()) {
Expand Down
25 changes: 19 additions & 6 deletions components/site/tests/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -161,7 +175,10 @@ fn can_build_site_without_live_reload() {
assert!(file_exists!(public, "nested_sass/scss.css"));

// no live reload code
assert_eq!(file_contains!(public, "index.html", "/livereload.js?port=1112&amp;mindelay=10"), false);
assert_eq!(
file_contains!(public, "index.html", "/livereload.js?port=1112&amp;mindelay=10"),
false
);

// Both pages and sections are in the sitemap
assert!(file_contains!(
Expand Down Expand Up @@ -470,11 +487,7 @@ fn can_build_site_with_pagination_for_index() {
"page/1/index.html",
"http-equiv=\"refresh\" content=\"0;url=https://replace-this-with-your-url.com/\""
));
assert!(file_contains!(
public,
"page/1/index.html",
"<title>Redirect</title>"
));
assert!(file_contains!(public, "page/1/index.html", "<title>Redirect</title>"));
assert!(file_contains!(
public,
"page/1/index.html",
Expand Down
3 changes: 3 additions & 0 deletions test_site/content/posts/tutorials/programming/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
title = "Programming"
sort_by = "weight"
weight = 1

[extra]
we_have_extra = "variables"
+++