Fix loading html files in themes outside of templates

Fix #412
This commit is contained in:
Vincent Prouillet 2018-09-09 20:12:53 +02:00
parent 38b30eb144
commit 367f58b0a3
4 changed files with 23 additions and 3 deletions

View file

@ -4,6 +4,7 @@
- Gutenberg has changed name to REPLACE_ME! - Gutenberg has changed name to REPLACE_ME!
- Update dependencies, fixing a few bugs with templates - 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) ## 0.4.2 (2018-09-03)

View file

@ -102,7 +102,11 @@ impl Site {
bail!("Theme `{}` is missing a templates folder", theme); 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")?; let mut tera_theme = Tera::parse(&theme_tpl_glob).chain_err(|| "Error parsing templates from themes")?;
rewrite_theme_paths(&mut tera_theme, &theme); rewrite_theme_paths(&mut tera_theme, &theme);
tera_theme.build_inheritance_chains()?; tera_theme.build_inheritance_chains()?;

View file

@ -1,3 +1,5 @@
use std::collections::HashMap;
use tera::{Tera, Context}; use tera::{Tera, Context};
use errors::Result; 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. /// so themes shortcodes can be used.
pub fn rewrite_theme_paths(tera: &mut Tera, theme: &str) { pub fn rewrite_theme_paths(tera: &mut Tera, theme: &str) {
let mut shortcodes_to_move = vec![]; 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 // 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 // First the parent if there is none
if let Some(ref p) = tpl.parent.clone() { if let Some(ref p) = tpl.parent.clone() {
tpl.parent = Some(format!("{}/templates/{}", theme, p)); 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)) { if tpl.name.starts_with(&format!("{}/templates/shortcodes", theme)) {
let new_name = tpl.name.replace(&format!("{}/templates/", 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; 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 // and then replace shortcodes in the Tera instance using the new names
for (old_name, new_name) in shortcodes_to_move { for (old_name, new_name) in shortcodes_to_move {
let tpl = tera.templates.remove(&old_name).unwrap(); let tpl = tera.templates.remove(&old_name).unwrap();

View file

@ -0,0 +1,6 @@
This should not be loaded.
https://github.com/Keats/gutenberg/issues/412
<li>IllegalMacroParam: \( \def\mymacro#1{#2} \mymacro{x} \) </li>
{{ hey( }}