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

Include unrendered pages in sections, add render property to templates #2585

Open
wants to merge 2 commits into
base: next
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions components/content/src/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ impl Library {
.get_mut(&self.taxo_name_to_slug[taxa_name])
.expect("taxa not found");

if !page.meta.render {
continue;
}
if !taxa_def.contains_key(term) {
taxa_def.insert(term.to_string(), Vec::new());
}
Expand Down Expand Up @@ -295,9 +298,6 @@ impl Library {

// Then once we took care of the sections, we find the pages of each section
for (path, page) in self.pages.iter_mut() {
if !page.meta.render {
continue;
}
let parent_filename = &index_filename_by_lang[&page.lang];
add_translation(&page.file.canonical, path);
let mut parent_section_path = page.file.parent.join(parent_filename);
Expand Down
4 changes: 4 additions & 0 deletions components/content/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub struct SerializingPage<'a> {
word_count: Option<usize>,
reading_time: Option<usize>,
assets: &'a [String],
render: bool,
draft: bool,
lang: &'a str,
lower: Option<Box<SerializingPage<'a>>>,
Expand Down Expand Up @@ -128,6 +129,7 @@ impl<'a> SerializingPage<'a> {
word_count: page.word_count,
reading_time: page.reading_time,
assets: &page.serialized_assets,
render: page.meta.render,
draft: page.meta.draft,
lang: &page.lang,
lower,
Expand All @@ -144,6 +146,7 @@ pub struct SerializingSection<'a> {
colocated_path: &'a Option<String>,
content: &'a str,
permalink: &'a str,
render: bool,
draft: bool,
ancestors: &'a [String],
title: &'a Option<String>,
Expand Down Expand Up @@ -207,6 +210,7 @@ impl<'a> SerializingSection<'a> {
relative_path: &section.file.relative,
colocated_path: &section.file.colocated_path,
ancestors: &section.ancestors,
render: section.meta.render,
draft: section.meta.draft,
content: &section.content,
permalink: &section.permalink,
Expand Down
17 changes: 15 additions & 2 deletions components/site/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ macro_rules! file_exists {

#[macro_export]
macro_rules! file_contains {
($root: expr, $path: expr, $text: expr) => {{
(@impl $root: expr, $path: expr) => {{
use std::io::prelude::*;
let mut path = $root.clone();
for component in $path.split('/') {
Expand All @@ -31,11 +31,24 @@ macro_rules! file_contains {
let mut file = std::fs::File::open(&path).expect(&format!("Failed to open {:?}", $path));
let mut s = String::new();
file.read_to_string(&mut s).unwrap();
println!("{}", s);
println!("{path:?} {s}");
s
}};
($root: expr, $path: expr, $text: expr) => {{
let s = file_contains!(@impl $root, $path);
s.contains($text)
}};
}

#[macro_export]
macro_rules! file_contains_regex {
($root: expr, $path: expr, $pat: expr) => {{
let s = file_contains!(@impl $root, $path);
let re = libs::regex::Regex::new($pat).unwrap();
re.is_match(&s)
}};
}

/// We return the tmpdir otherwise it would get out of scope and be deleted
/// The tests can ignore it if they dont need it by prefixing it with a `_`
pub fn build_site(name: &str) -> (Site, TempDir, PathBuf) {
Expand Down
93 changes: 65 additions & 28 deletions components/site/tests/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn can_parse_site() {
let library = site.library.read().unwrap();

// Correct number of pages (sections do not count as pages, draft are ignored)
assert_eq!(library.pages.len(), 36);
assert_eq!(library.pages.len(), 37);
let posts_path = path.join("content").join("posts");

// Make sure the page with a url doesn't have any sections
Expand All @@ -44,7 +44,7 @@ fn can_parse_site() {

let posts_section = library.sections.get(&posts_path.join("_index.md")).unwrap();
assert_eq!(posts_section.subsections.len(), 2);
assert_eq!(posts_section.pages.len(), 10); // 11 with 1 draft == 10
assert_eq!(posts_section.pages.len(), 12); // 13 with 1 draft == 12
assert_eq!(posts_section.ancestors, vec![index_section.file.relative.clone()]);

// Make sure we remove all the pwd + content from the sections
Expand Down Expand Up @@ -155,6 +155,33 @@ fn can_build_site_without_live_reload() {
"posts/tutorials/devops/nix.md"
));

assert!(file_exists!(public, "posts/access-render/index.html"));

// render = false pages can still be accessed directly
assert!(file_contains!(
public,
"posts/access-render/index.html",
"Path of unrendered page: /posts/render/"
));
// render = false pages can still be accessed through sections
assert!(file_contains_regex!(
public,
"posts/access-render/index.html",
r#"Pages in section with unrendered page: <ul>(<li>[^>]+</li>)*<li>/posts/render/</li>"#
));
// render = false sections can still be accessed directly
assert!(file_contains!(
public,
"posts/access-render/index.html",
"Pages in unrendered section: <ul><li>"
));
// render = false pages don't show up in taxonomies
assert!(!file_contains!(public, "podcast-authors/some-person/atom.xml", "/posts/render/"));
assert!(!file_contains!(public, "categories/a-category/atom.xml", "/posts/render/"));
// render = false pages don't even add terms to taxonomies
assert!(!file_exists!(public, "podcast-authors/other-person/atom.xml"));
assert!(!file_exists!(public, "categories/b-category/atom.xml"));

// aliases work
assert!(file_exists!(public, "an-old-url/old-page/index.html"));
assert!(file_contains!(public, "an-old-url/old-page/index.html", "something-else"));
Expand Down Expand Up @@ -216,6 +243,8 @@ fn can_build_site_without_live_reload() {
assert!(!file_contains!(public, "sitemap.xml", "draft"));
// render: false sections are not in the sitemap either
assert!(!file_contains!(public, "sitemap.xml", "posts/2018/</loc>"));
// render: false pages are not in the sitemap either
assert!(!file_contains!(public, "sitemap.xml", "posts/render/</loc>"));

// robots.txt has been rendered from the template
assert!(file_contains!(public, "robots.txt", "User-agent: zola"));
Expand Down Expand Up @@ -299,15 +328,19 @@ fn can_build_site_with_taxonomies() {
let mut pages = vec![];

let pages_data = std::mem::replace(&mut library.pages, AHashMap::new());
for (i, (_, mut page)) in pages_data.into_iter().enumerate() {
page.meta.taxonomies = {
let mut taxonomies = HashMap::new();
taxonomies.insert(
"categories".to_string(),
vec![if i % 2 == 0 { "A" } else { "B" }.to_string()],
);
taxonomies
};
let mut i = 0;
for (_, mut page) in pages_data.into_iter() {
if page.meta.render {
page.meta.taxonomies = {
let mut taxonomies = HashMap::new();
taxonomies.insert(
"categories".to_string(),
vec![if i % 2 == 0 { "A" } else { "B" }.to_string()],
);
taxonomies
};
i += 1;
}
pages.push(page);
}
for p in pages {
Expand Down Expand Up @@ -417,7 +450,7 @@ fn can_build_site_with_pagination_for_section() {
"posts/page/1/index.html",
"http-equiv=\"refresh\" content=\"0; url=https://replace-this-with-your-url.com/posts/\""
));
assert!(file_contains!(public, "posts/index.html", "Num pagers: 5"));
assert!(file_contains!(public, "posts/index.html", "Num pagers: 6"));
assert!(file_contains!(public, "posts/index.html", "Page size: 2"));
assert!(file_contains!(public, "posts/index.html", "Current index: 1"));
assert!(!file_contains!(public, "posts/index.html", "has_prev"));
Expand All @@ -430,12 +463,12 @@ fn can_build_site_with_pagination_for_section() {
assert!(file_contains!(
public,
"posts/index.html",
"Last: https://replace-this-with-your-url.com/posts/page/5/"
"Last: https://replace-this-with-your-url.com/posts/page/6/"
));
assert!(!file_contains!(public, "posts/index.html", "has_prev"));

assert!(file_exists!(public, "posts/page/2/index.html"));
assert!(file_contains!(public, "posts/page/2/index.html", "Num pagers: 5"));
assert!(file_contains!(public, "posts/page/2/index.html", "Num pagers: 6"));
assert!(file_contains!(public, "posts/page/2/index.html", "Page size: 2"));
assert!(file_contains!(public, "posts/page/2/index.html", "Current index: 2"));
assert!(file_contains!(public, "posts/page/2/index.html", "has_prev"));
Expand All @@ -448,11 +481,11 @@ fn can_build_site_with_pagination_for_section() {
assert!(file_contains!(
public,
"posts/page/2/index.html",
"Last: https://replace-this-with-your-url.com/posts/page/5/"
"Last: https://replace-this-with-your-url.com/posts/page/6/"
));

assert!(file_exists!(public, "posts/page/3/index.html"));
assert!(file_contains!(public, "posts/page/3/index.html", "Num pagers: 5"));
assert!(file_contains!(public, "posts/page/3/index.html", "Num pagers: 6"));
assert!(file_contains!(public, "posts/page/3/index.html", "Page size: 2"));
assert!(file_contains!(public, "posts/page/3/index.html", "Current index: 3"));
assert!(file_contains!(public, "posts/page/3/index.html", "has_prev"));
Expand All @@ -465,11 +498,11 @@ fn can_build_site_with_pagination_for_section() {
assert!(file_contains!(
public,
"posts/page/3/index.html",
"Last: https://replace-this-with-your-url.com/posts/page/5/"
"Last: https://replace-this-with-your-url.com/posts/page/6/"
));

assert!(file_exists!(public, "posts/page/4/index.html"));
assert!(file_contains!(public, "posts/page/4/index.html", "Num pagers: 5"));
assert!(file_contains!(public, "posts/page/4/index.html", "Num pagers: 6"));
assert!(file_contains!(public, "posts/page/4/index.html", "Page size: 2"));
assert!(file_contains!(public, "posts/page/4/index.html", "Current index: 4"));
assert!(file_contains!(public, "posts/page/4/index.html", "has_prev"));
Expand All @@ -482,7 +515,7 @@ fn can_build_site_with_pagination_for_section() {
assert!(file_contains!(
public,
"posts/page/4/index.html",
"Last: https://replace-this-with-your-url.com/posts/page/5/"
"Last: https://replace-this-with-your-url.com/posts/page/6/"
));

// sitemap contains the pager pages
Expand Down Expand Up @@ -589,15 +622,19 @@ fn can_build_site_with_pagination_for_taxonomy() {
let mut pages = vec![];

let pages_data = std::mem::replace(&mut library.pages, AHashMap::new());
for (i, (_, mut page)) in pages_data.into_iter().enumerate() {
page.meta.taxonomies = {
let mut taxonomies = HashMap::new();
taxonomies.insert(
"tags".to_string(),
vec![if i % 2 == 0 { "A" } else { "B" }.to_string()],
);
taxonomies
};
let mut i = 0;
for (_, mut page) in pages_data.into_iter() {
if page.meta.render {
page.meta.taxonomies = {
let mut taxonomies = HashMap::new();
taxonomies.insert(
"tags".to_string(),
vec![if i % 2 == 0 { "A" } else { "B" }.to_string()],
);
taxonomies
};
i += 1;
}
pages.push(page);
}
for p in pages {
Expand Down
3 changes: 3 additions & 0 deletions docs/content/documentation/templates/pages-sections.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ updated: String?;
slug: String;
path: String;
authors: Array<String>;
render: Bool;
draft: Bool;
// the path, split on '/'
components: Array<String>;
Expand Down Expand Up @@ -77,6 +78,8 @@ content: String;
title: String?;
description: String?;
path: String;
render: Bool;
draft: Bool;
// the path, split on '/'
components: Array<String>;
permalink: String;
Expand Down
9 changes: 9 additions & 0 deletions test_site/content/posts/access-render.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
+++
title = 'render = false tests'
slug = 'access-render'
template = 'access_render.html'
date = 2000-01-01
+++

This post exists to test that unrendered sections and pages are still accessible
via templates.
4 changes: 4 additions & 0 deletions test_site/content/posts/render.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ title = "Page with content but without generated folder"
description = ""
date = 2017-04-01
render = false

[taxonomies]
categories = ["a-category", "b-category"]
podcast_authors = ["Some Person", "Other Person"]
+++

Don't generate a folder for this page
15 changes: 15 additions & 0 deletions test_site/templates/access_render.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends "index.html" %}

{% block content %}
{% set unrendered_page = get_page(path='posts/render.md') %}
{% set section_with_unrendered = get_section(path='posts/_index.md') %}
{% set unrendered_section = get_section(path='posts/2018/_index.md') %}
Path of unrendered page: {{ unrendered_page.path | safe }}
Pages in section with unrendered page: <ul>{% for page in section_with_unrendered.pages -%}
<li>{{ page.path | safe }}</li>
{%- endfor %}</ul>
Pages in unrendered section: <ul>{% for page in unrendered_section.pages -%}
<li>{{ page.path | safe }}</li>
{%- endfor %}</ul>
{% endblock content %}