From 1e0744601e4b0d4076b5afb33b7ae8ab64379fb2 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Thu, 11 May 2017 14:33:23 +0900 Subject: [PATCH] Slighty smarter rebuild on page content change --- src/site.rs | 65 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 24 deletions(-) diff --git a/src/site.rs b/src/site.rs index 47a6b6c..47cecfd 100644 --- a/src/site.rs +++ b/src/site.rs @@ -212,11 +212,15 @@ impl Site { } /// Called in serve, add a page again updating permalinks and its content - fn add_page_and_render(&mut self, path: &Path) -> Result<()> { + /// The bool in the result is whether the front matter has been updated or not + fn add_page_and_render(&mut self, path: &Path) -> Result<(bool, Page)> { + let existing_page = self.pages.get(path).expect("Page was supposed to exist in add_page_and_render").clone(); self.add_page(path)?; let mut page = self.pages.get_mut(path).unwrap(); self.permalinks.insert(page.relative_path.clone(), page.permalink.clone()); - page.render_markdown(&self.permalinks, &self.tera, &self.config) + page.render_markdown(&self.permalinks, &self.tera, &self.config)?; + + Ok((existing_page.meta != page.meta, page.clone())) } /// Find out the direct subsections of each subsection if there are some @@ -337,25 +341,37 @@ impl Site { self.add_section(path)?; } else { // probably just an update so just re-parse that page - // TODO: we can compare the frontmatter of the existing and new one - // to see if we need to update re-build the whole thing or just that - // page - self.add_page_and_render(path)?; + let (frontmatter_changed, page) = self.add_page_and_render(path)?; + // TODO: can probably be smarter and check what changed + if frontmatter_changed { + self.populate_sections(); + self.populate_tags_and_categories(); + self.build()?; + } else { + self.render_page(&page)?; + } } - } else if is_section { - // File doesn't exist -> a deletion so we remove it from everything - let relative_path = self.sections[path].relative_path.clone(); - self.sections.remove(path); - self.permalinks.remove(&relative_path); } else { - let relative_path = self.pages[path].relative_path.clone(); - self.pages.remove(path); + // File doesn't exist -> a deletion so we remove it from everything + let relative_path = if is_section { + self.sections[path].relative_path.clone() + } else { + self.pages[path].relative_path.clone() + }; self.permalinks.remove(&relative_path); + + if is_section { + self.sections.remove(path); + } else { + self.pages.remove(path); + } + // TODO: probably no need to do that, we should be able to only re-render a page or a section. + self.populate_sections(); + self.populate_tags_and_categories(); + self.build()?; } - // TODO: probably no need to do that, we should be able to only re-render a page or a section. - self.populate_sections(); - self.populate_tags_and_categories(); - self.build() + + Ok(()) } pub fn rebuild_after_template_change(&mut self, path: &Path) -> Result<()> { @@ -577,9 +593,6 @@ impl Site { .collect(); for section in self.sections.values() { - if !section.meta.should_render() { - continue; - } let mut output_path = public.to_path_buf(); for component in §ion.components { output_path.push(component); @@ -589,6 +602,14 @@ impl Site { } } + for page in §ion.pages { + self.render_page(page)?; + } + + if !section.meta.should_render() { + continue; + } + if section.meta.is_paginated() { self.render_paginated(&output_path, section)?; } else { @@ -599,10 +620,6 @@ impl Site { )?; create_file(output_path.join("index.html"), &self.inject_livereload(output))?; } - - for page in §ion.pages { - self.render_page(page)?; - } } Ok(())