From 2d26bf038c35d894035d6733181cf3c0129c5aed Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Fri, 10 Mar 2017 21:36:43 +0900 Subject: [PATCH] Force refresh on content/template change and various fixes --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- src/cmd/serve.rs | 6 ++++-- src/front_matter.rs | 5 +++-- src/main.rs | 3 +++ src/page.rs | 23 +++++++++-------------- src/site.rs | 5 +---- 7 files changed, 23 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6ae09884..44a8b3e7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ [root] name = "gutenberg" -version = "0.1.0" +version = "0.0.1" dependencies = [ "chrono 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.20.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -774,7 +774,7 @@ dependencies = [ [[package]] name = "tera" version = "0.8.0" -source = "git+https://github.com/Keats/tera?branch=reload#31f8bd8238e3e6bdef9ecf4468079c535609b5eb" +source = "git+https://github.com/Keats/tera?branch=reload#ee038a6f3519ac35e1878ca9b29ec739a8f84a15" dependencies = [ "chrono 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "error-chain 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 0ef92e45..10d6f95a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gutenberg" -version = "0.1.0" +version = "0.0.1" authors = ["Vincent Prouillet "] license = "MIT" readme = "README.md" diff --git a/src/cmd/serve.rs b/src/cmd/serve.rs index 6211ce42..87da4c3f 100644 --- a/src/cmd/serve.rs +++ b/src/cmd/serve.rs @@ -119,11 +119,13 @@ pub fn serve(interface: &str, port: &str) -> Result<()> { match detect_change_kind(&pwd, &path) { (ChangeKind::Content, _) => { println!("-> Content changed {}", path.display()); - rebuild_done_handling(&broadcaster, site.rebuild(), ""); + // Force refresh + rebuild_done_handling(&broadcaster, site.rebuild_after_content_change(), "/x.js"); }, (ChangeKind::Templates, _) => { println!("-> Template changed {}", path.display()); - rebuild_done_handling(&broadcaster, site.rebuild_after_template_change(), ""); + // Force refresh + rebuild_done_handling(&broadcaster, site.rebuild_after_template_change(), "/x.js"); }, (ChangeKind::StaticFiles, p) => { println!("-> Static file changes detected {}", path.display()); diff --git a/src/front_matter.rs b/src/front_matter.rs index 1bf9411c..ca5349b5 100644 --- a/src/front_matter.rs +++ b/src/front_matter.rs @@ -36,9 +36,9 @@ pub struct FrontMatter { pub draft: Option, /// Only one category allowed pub category: Option, - /// Optional layout, if we want to specify which tpl to render for that page + /// Optional template, if we want to specify which template to render for that page #[serde(skip_serializing)] - pub layout: Option, + pub template: Option, /// Any extra parameter present in the front matter pub extra: Option>, } @@ -69,6 +69,7 @@ impl FrontMatter { Ok(f) } + /// Converts the date in the front matter, which can be in 2 formats, into a NaiveDateTime pub fn parse_date(&self) -> Option { match self.date { Some(ref d) => { diff --git a/src/main.rs b/src/main.rs index fe017a04..b0237a04 100644 --- a/src/main.rs +++ b/src/main.rs @@ -87,6 +87,9 @@ fn main() { Ok(()) => (), Err(e) => { println!("Error: {}", e); + for e in e.iter().skip(1) { + println!("Reason: {}", e) + } ::std::process::exit(1); }, }; diff --git a/src/page.rs b/src/page.rs index 4ff9c1ec..4f004bf1 100644 --- a/src/page.rs +++ b/src/page.rs @@ -182,22 +182,20 @@ impl Page { Page::parse(&path.strip_prefix("content").unwrap().to_string_lossy(), &content, config) } - fn get_layout_name(&self) -> String { - match self.meta.layout { - Some(ref l) => l.to_string(), - None => "page.html".to_string() - } - } - /// Renders the page using the default layout, unless specified in front-matter pub fn render_html(&self, tera: &Tera, config: &Config) -> Result { - let tpl = self.get_layout_name(); + let tpl_name = match self.meta.template { + Some(ref l) => l.to_string(), + None => "page.html".to_string() + }; + // TODO: create a helper to create context to ensure all contexts + // have the same names let mut context = Context::new(); - context.add("site", config); + context.add("config", config); context.add("page", self); - tera.render(&tpl, &context) - .chain_err(|| "Error while rendering template") + tera.render(&tpl_name, &context) + .chain_err(|| format!("Failed to render page '{}'", self.filename)) } } @@ -225,12 +223,10 @@ impl ser::Serialize for Page { impl PartialOrd for Page { fn partial_cmp(&self, other: &Page) -> Option { if self.meta.date.is_none() { - println!("No self data"); return Some(Ordering::Less); } if other.meta.date.is_none() { - println!("No other date"); return Some(Ordering::Greater); } @@ -337,7 +333,6 @@ Hello world"#; assert!(res.is_ok()); let page = res.unwrap(); assert_eq!(page.url, "posts/intro/hello-world"); - println!("{}", page.permalink); assert_eq!(page.permalink, format!("{}{}", conf.base_url, "/posts/intro/hello-world")); } diff --git a/src/site.rs b/src/site.rs index c3b8d48c..0094982a 100644 --- a/src/site.rs +++ b/src/site.rs @@ -149,16 +149,13 @@ impl Site { Ok(()) } - /// Re-parse and re-generate the site - /// Very dumb for now, ideally it would only rebuild what changed - pub fn rebuild(&mut self) -> Result<()> { + pub fn rebuild_after_content_change(&mut self) -> Result<()> { self.parse_site()?; self.build() } pub fn rebuild_after_template_change(&mut self) -> Result<()> { self.templates.full_reload()?; - println!("Reloaded templates"); self.build_pages() }