From 09ade9573c669871e551391e3ad7a33980970d27 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Thu, 9 Mar 2017 16:46:38 +0900 Subject: [PATCH] Use a lazy_static version of tera templates --- src/site.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/site.rs b/src/site.rs index 59fa93d4..8a36809d 100644 --- a/src/site.rs +++ b/src/site.rs @@ -14,6 +14,18 @@ use page::Page; use utils::create_file; +lazy_static! { + static ref GUTENBERG_TERA: Tera = { + let mut tera = Tera::default(); + tera.add_raw_templates(vec![ + ("rss.xml", include_str!("templates/rss.xml")), + ("sitemap.xml", include_str!("templates/sitemap.xml")), + ]).unwrap(); + tera + }; +} + + #[derive(Debug, PartialEq)] enum RenderList { Tags, @@ -50,7 +62,10 @@ pub struct Site { impl Site { pub fn new(livereload: bool) -> Result { - let tera = Tera::new("templates/**/*").chain_err(|| "Error parsing templates")?; + let mut tera = Tera::new("templates/**/*") + .chain_err(|| "Error parsing templates")?; + tera.extend(&GUTENBERG_TERA)?; + let mut site = Site { config: get_config(), pages: HashMap::new(), @@ -258,10 +273,9 @@ impl Site { } fn render_sitemap(&self) -> Result<()> { - let tpl = String::from_utf8(include_bytes!("templates/sitemap.xml").to_vec()).unwrap(); let mut context = Context::new(); context.add("pages", &self.pages.values().collect::>()); - let sitemap = Tera::one_off(&tpl, &context, false)?; + let sitemap = self.templates.render("sitemap.xml", &context)?; let public = Path::new("public"); create_file(public.join("sitemap.xml"), &sitemap)?; @@ -278,7 +292,6 @@ impl Site { } fn render_rss_feed(&self) -> Result<()> { - let tpl = String::from_utf8(include_bytes!("templates/rss.xml").to_vec()).unwrap(); let mut context = Context::new(); let mut pages = self.pages.values() .filter(|p| p.meta.date.is_some()) @@ -295,7 +308,7 @@ impl Site { context.add("config", &self.config); context.add("feed_url", &self.get_rss_feed_url()); - let sitemap = Tera::one_off(&tpl, &context, false)?; + let sitemap = self.templates.render("rss.xml", &context)?; let public = Path::new("public"); create_file(public.join("rss.xml"), &sitemap)?;