From 367f58b0a37ca13d5b853cc3954fbb04c70532c2 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Sun, 9 Sep 2018 20:12:53 +0200 Subject: [PATCH] Fix loading html files in themes outside of templates Fix #412 --- CHANGELOG.md | 1 + components/site/src/lib.rs | 6 +++++- components/utils/src/templates.rs | 13 +++++++++++-- test_site/themes/sample/static/some-html.html | 6 ++++++ 4 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 test_site/themes/sample/static/some-html.html diff --git a/CHANGELOG.md b/CHANGELOG.md index f08c197..cbdb55f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Gutenberg has changed name to REPLACE_ME! - Update dependencies, fixing a few bugs with templates +- Make Gutenberg load only .html files in themes from the templates folder ## 0.4.2 (2018-09-03) diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index 2782035..775d534 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -102,7 +102,11 @@ impl Site { bail!("Theme `{}` is missing a templates folder", theme); } - let theme_tpl_glob = format!("{}/{}", path.to_string_lossy().replace("\\", "/"), "themes/**/*.html"); + let theme_tpl_glob = format!( + "{}/{}", + path.to_string_lossy().replace("\\", "/"), + format!("themes/{}/templates/**/*.html", theme) + ); let mut tera_theme = Tera::parse(&theme_tpl_glob).chain_err(|| "Error parsing templates from themes")?; rewrite_theme_paths(&mut tera_theme, &theme); tera_theme.build_inheritance_chains()?; diff --git a/components/utils/src/templates.rs b/components/utils/src/templates.rs index bcea054..429bc32 100644 --- a/components/utils/src/templates.rs +++ b/components/utils/src/templates.rs @@ -1,3 +1,5 @@ +use std::collections::HashMap; + use tera::{Tera, Context}; use errors::Result; @@ -57,9 +59,12 @@ pub fn render_template(name: &str, tera: &Tera, context: &Context, theme: &Optio /// so themes shortcodes can be used. pub fn rewrite_theme_paths(tera: &mut Tera, theme: &str) { let mut shortcodes_to_move = vec![]; + let mut templates = HashMap::new(); + let old_templates = ::std::mem::replace(&mut tera.templates, HashMap::new()); // We want to match the paths in the templates to the new names - for tpl in tera.templates.values_mut() { + for (key, mut tpl) in old_templates{ + tpl.name = format!("{}/templates/{}", theme, tpl.name); // First the parent if there is none if let Some(ref p) = tpl.parent.clone() { tpl.parent = Some(format!("{}/templates/{}", theme, p)); @@ -74,11 +79,15 @@ pub fn rewrite_theme_paths(tera: &mut Tera, theme: &str) { if tpl.name.starts_with(&format!("{}/templates/shortcodes", theme)) { let new_name = tpl.name.replace(&format!("{}/templates/", theme), ""); - shortcodes_to_move.push((tpl.name.clone(), new_name.clone())); + shortcodes_to_move.push((key, new_name.clone())); tpl.name = new_name; } + + templates.insert(tpl.name.clone(), tpl); } + tera.templates = templates; + // and then replace shortcodes in the Tera instance using the new names for (old_name, new_name) in shortcodes_to_move { let tpl = tera.templates.remove(&old_name).unwrap(); diff --git a/test_site/themes/sample/static/some-html.html b/test_site/themes/sample/static/some-html.html new file mode 100644 index 0000000..bec39b1 --- /dev/null +++ b/test_site/themes/sample/static/some-html.html @@ -0,0 +1,6 @@ +This should not be loaded. +https://github.com/Keats/gutenberg/issues/412 + +
  • IllegalMacroParam: \( \def\mymacro#1{#2} \mymacro{x} \)
  • + +{{ hey( }}