From 5d7e0bb33e2242d213746df3c924f69453e0e7d4 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Tue, 26 Sep 2017 17:25:55 +0900 Subject: [PATCH] Register all Tera global fns on change --- CHANGELOG.md | 2 ++ components/site/src/lib.rs | 9 ++++----- src/rebuild.rs | 8 ++++---- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 05974b0f..c25cdb0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ - `section.subsections` can now be sorted by a `weight` attribute on a section front-matter - Do nothing on directory adding/removal in livereload - Add back `draft` on pages that was wrongly removed +- Page and Section `path` field is not starting with a `/` anymore +- All Tera global fns are now rebuilt on changes ## 0.1.3 (2017-08-31) diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index 786029a7..14b57e25 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -242,15 +242,14 @@ impl Site { self.populate_sections(); self.populate_tags_and_categories(); - self.tera.register_global_function("get_page", global_fns::make_get_page(&self.pages)); - self.tera.register_global_function("get_section", global_fns::make_get_section(&self.sections)); - self.register_get_url_fn(); + self.register_tera_global_fns(); Ok(()) } - /// Separate fn as it can be called in the serve command - pub fn register_get_url_fn(&mut self) { + pub fn register_tera_global_fns(&mut self) { + self.tera.register_global_function("get_page", global_fns::make_get_page(&self.pages)); + self.tera.register_global_function("get_section", global_fns::make_get_section(&self.sections)); self.tera.register_global_function( "get_url", global_fns::make_get_url(self.permalinks.clone(), self.config.clone()) diff --git a/src/rebuild.rs b/src/rebuild.rs index bfb3b757..e2c9391f 100644 --- a/src/rebuild.rs +++ b/src/rebuild.rs @@ -129,7 +129,7 @@ pub fn after_content_change(site: &mut Site, path: &Path) -> Result<()> { }; } // Ensure we have our fn updated so it doesn't contain the permalinks deleted - site.register_get_url_fn(); + site.register_tera_global_fns(); // Deletion is something that doesn't happen all the time so we // don't need to optimise it too much return site.build(); @@ -165,9 +165,9 @@ pub fn after_content_change(site: &mut Site, path: &Path) -> Result<()> { return Ok(()); }, None => { - site.register_get_url_fn(); // New section, only render that one site.populate_sections(); + site.register_tera_global_fns(); return site.render_section(&site.sections[path], true); } }; @@ -177,7 +177,7 @@ pub fn after_content_change(site: &mut Site, path: &Path) -> Result<()> { let page = Page::from_file(path, &site.config)?; match site.add_page(page, true)? { Some(prev) => { - site.register_get_url_fn(); + site.register_tera_global_fns(); // Updating a page let current = site.pages[path].clone(); // Front matter didn't change, only content did @@ -216,10 +216,10 @@ pub fn after_content_change(site: &mut Site, path: &Path) -> Result<()> { }, None => { - site.register_get_url_fn(); // It's a new page! site.populate_sections(); site.populate_tags_and_categories(); + site.register_tera_global_fns(); // No need to optimise that yet, we can revisit if it becomes an issue site.build()?; }