Pass section in context when rendering a page if there is one

This commit is contained in:
Vincent Prouillet 2017-06-16 21:53:28 +09:00
parent e57fb7d594
commit e76ee7ae4a
7 changed files with 19 additions and 13 deletions

View file

@ -6,6 +6,7 @@
- Add extra builtin shortcode for Streamable videos
- `path` and `permalink` now end with a `/`
- Generate table of contents for each page
- Add `section` to a page Tera context if there is one
## 0.0.6 (2017-05-24)

View file

@ -172,9 +172,9 @@ pub fn after_content_change(site: &mut Site, path: &Path) -> Result<()> {
// Updating a page
let current = site.pages[path].clone();
// Front matter didn't change, only content did
// so we render only the section page, not its pages
// so we render only the section page, not its content
if current.meta == prev.meta {
return site.render_page(&site.pages[path]);
return site.render_page(&current, find_parent_section(site, &current));
}
// Front matter changed
@ -199,7 +199,7 @@ pub fn after_content_change(site: &mut Site, path: &Path) -> Result<()> {
site.render_index()?;
},
PageChangesNeeded::Render => {
site.render_page(&site.pages[path])?;
site.render_page(&site.pages[path], find_parent_section(site, &current))?;
},
};
}

View file

@ -16,7 +16,7 @@ use rendering::context::Context;
use fs::{read_file};
use content::utils::{find_related_assets, get_reading_analytics};
use content::file_info::FileInfo;
use content::Header;
use content::{Header, Section};
#[derive(Clone, Debug, PartialEq)]
@ -136,7 +136,7 @@ impl Page {
}
/// Renders the page using the default layout, unless specified in front-matter
pub fn render_html(&self, tera: &Tera, config: &Config) -> Result<String> {
pub fn render_html(&self, tera: &Tera, config: &Config, section: Option<&Section>) -> Result<String> {
let tpl_name = match self.meta.template {
Some(ref l) => l.to_string(),
None => "page.html".to_string()
@ -147,6 +147,7 @@ impl Page {
context.add("page", self);
context.add("current_url", &self.permalink);
context.add("current_path", &self.path);
context.add("section", &section);
tera.render(&tpl_name, &context)
.chain_err(|| format!("Failed to render page '{}'", self.file.path.display()))

View file

@ -95,10 +95,10 @@ impl<'a> Paginator<'a> {
}
Paginator {
all_pages: all_pages,
pagers: pagers,
paginate_by: paginate_by,
section: section,
all_pages,
pagers,
paginate_by,
section,
}
}

View file

@ -304,7 +304,7 @@ impl Site {
}
/// Renders a single content page
pub fn render_page(&self, page: &Page) -> Result<()> {
pub fn render_page(&self, page: &Page, section: Option<&Section>) -> Result<()> {
ensure_directory_exists(&self.output_path)?;
// Copy the nesting of the content directory if we have sections for that page
@ -322,7 +322,7 @@ impl Site {
create_directory(&current_path)?;
// Finally, create a index.html file there with the page rendered
let output = page.render_html(&self.tera, &self.config)?;
let output = page.render_html(&self.tera, &self.config, section)?;
create_file(&current_path.join("index.html"), &self.inject_livereload(output))?;
// Copy any asset we found previously into the same directory as the index.html
@ -501,7 +501,7 @@ impl Site {
if render_pages {
for page in &section.pages {
self.render_page(page)?;
self.render_page(page, Some(section))?;
}
}
@ -540,7 +540,7 @@ impl Site {
ensure_directory_exists(&self.output_path)?;
for page in self.get_all_orphan_pages() {
self.render_page(page)?;
self.render_page(page, None)?;
}
Ok(())

View file

@ -1,6 +1,7 @@
{% extends "index.html" %}
{% block content %}
{% if section %}Section:{{ section.permalink }}{% endif %}
{{ page.content | safe }}
{% if page.previous %}Previous article: {{ page.previous.permalink }}{% endif %}

View file

@ -128,6 +128,9 @@ fn can_build_site_without_live_reload() {
// Both pages and sections are in the sitemap
assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/posts/simple/</loc>"));
assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/posts/</loc>"));
// section is in the page context
assert!(file_contains!(public, "posts/python/index.html", "Section:"));
}
#[test]