diff --git a/CHANGELOG.md b/CHANGELOG.md index f08c1974..cbdb55f2 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 27820350..775d5348 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 bcea054f..429bc328 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 00000000..bec39b19 --- /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 + +