diff --git a/CHANGELOG.md b/CHANGELOG.md index d64b70d..e3a906c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/src/bin/rebuild.rs b/src/bin/rebuild.rs index 4b5eac3..e52b915 100644 --- a/src/bin/rebuild.rs +++ b/src/bin/rebuild.rs @@ -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(¤t, find_parent_section(site, ¤t)); } // 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, ¤t))?; }, }; } diff --git a/src/content/page.rs b/src/content/page.rs index 4f7d60c..390044e 100644 --- a/src/content/page.rs +++ b/src/content/page.rs @@ -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 { + pub fn render_html(&self, tera: &Tera, config: &Config, section: Option<&Section>) -> Result { 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", §ion); tera.render(&tpl_name, &context) .chain_err(|| format!("Failed to render page '{}'", self.file.path.display())) diff --git a/src/content/pagination.rs b/src/content/pagination.rs index a94f731..0c3159b 100644 --- a/src/content/pagination.rs +++ b/src/content/pagination.rs @@ -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, } } diff --git a/src/site.rs b/src/site.rs index 07013d8..2e7fec7 100644 --- a/src/site.rs +++ b/src/site.rs @@ -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(¤t_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(¤t_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 §ion.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(()) diff --git a/test_site/templates/page.html b/test_site/templates/page.html index 25e0455..25bdbf8 100644 --- a/test_site/templates/page.html +++ b/test_site/templates/page.html @@ -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 %} diff --git a/tests/site.rs b/tests/site.rs index 6194f8f..b650202 100644 --- a/tests/site.rs +++ b/tests/site.rs @@ -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", "https://replace-this-with-your-url.com/posts/simple/")); assert!(file_contains!(public, "sitemap.xml", "https://replace-this-with-your-url.com/posts/")); + + // section is in the page context + assert!(file_contains!(public, "posts/python/index.html", "Section:")); } #[test]