From 855d2376dfe8033f8c722947427436b3120342a5 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Mon, 10 Feb 2020 23:09:22 +0100 Subject: [PATCH] Fix some theme extension Closes #937 --- components/site/src/lib.rs | 8 ++++-- components/utils/src/templates.rs | 33 ++++++++++++++++------ components/utils/test-templates/base.html | 10 +++++++ components/utils/test-templates/index.html | 6 +++- 4 files changed, 44 insertions(+), 13 deletions(-) create mode 100644 components/utils/test-templates/base.html diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index c83c8607..eaba38e9 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -76,17 +76,19 @@ impl Site { ); let mut tera_theme = Tera::parse(&theme_tpl_glob) .map_err(|e| Error::chain("Error parsing templates from themes", e))?; - rewrite_theme_paths(&mut tera_theme, &theme); + rewrite_theme_paths( + &mut tera_theme, + tera.templates.values().map(|v| v.name.as_ref()).collect(), + &theme, + ); // TODO: we do that twice, make it dry? if theme_path.join("templates").join("robots.txt").exists() { tera_theme .add_template_file(theme_path.join("templates").join("robots.txt"), None)?; } - tera_theme.build_inheritance_chains()?; tera.extend(&tera_theme)?; } tera.extend(&ZOLA_TERA)?; - // the `extend` above already does it but hey tera.build_inheritance_chains()?; // TODO: Tera doesn't use globset right now so we can load the robots.txt as part diff --git a/components/utils/src/templates.rs b/components/utils/src/templates.rs index 0e526f69..5c25d255 100644 --- a/components/utils/src/templates.rs +++ b/components/utils/src/templates.rs @@ -67,17 +67,21 @@ pub fn render_template( /// or macros is always better anyway for themes /// This will also rename the shortcodes to NOT have the themes in the path /// so themes shortcodes can be used. -pub fn rewrite_theme_paths(tera: &mut Tera, theme: &str) { +pub fn rewrite_theme_paths(tera_theme: &mut Tera, site_templates: Vec<&str>, theme: &str) { let mut shortcodes_to_move = vec![]; let mut templates = HashMap::new(); - let old_templates = ::std::mem::replace(&mut tera.templates, HashMap::new()); + let old_templates = ::std::mem::replace(&mut tera_theme.templates, HashMap::new()); // We want to match the paths in the templates to the new names 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 one + // If a template with the same name is also in site, assumes it overrides the theme one + // and do not change anything if let Some(ref p) = tpl.parent.clone() { - tpl.parent = Some(format!("{}/templates/{}", theme, p)); + if !site_templates.contains(&p.as_ref()) { + tpl.parent = Some(format!("{}/templates/{}", theme, p)); + } } // Next the macros import @@ -96,12 +100,12 @@ pub fn rewrite_theme_paths(tera: &mut Tera, theme: &str) { templates.insert(tpl.name.clone(), tpl); } - tera.templates = templates; + tera_theme.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(); - tera.templates.insert(new_name, tpl); + let tpl = tera_theme.templates.remove(&old_name).unwrap(); + tera_theme.templates.insert(new_name, tpl); } } @@ -113,12 +117,23 @@ mod tests { #[test] fn can_rewrite_all_paths_of_theme() { let mut tera = Tera::parse("test-templates/*.html").unwrap(); - rewrite_theme_paths(&mut tera, "hyde"); + rewrite_theme_paths(&mut tera, vec!["base.html"], "hyde"); // special case to make the test work: we also rename the files to // match the imports - for (key, val) in tera.templates.clone() { + for (key, val) in &tera.templates.clone() { tera.templates.insert(format!("hyde/templates/{}", key), val.clone()); } + // Adding our fake base + tera.add_raw_template("base.html", "Hello").unwrap(); tera.build_inheritance_chains().unwrap(); + + assert_eq!( + tera.templates["hyde/templates/index.html"].parent, + Some("base.html".to_string()) + ); + assert_eq!( + tera.templates["hyde/templates/child.html"].parent, + Some("hyde/templates/index.html".to_string()) + ); } } diff --git a/components/utils/test-templates/base.html b/components/utils/test-templates/base.html new file mode 100644 index 00000000..d562d6b3 --- /dev/null +++ b/components/utils/test-templates/base.html @@ -0,0 +1,10 @@ + + + + + + + {% block body %} + {% endblock body %} + + \ No newline at end of file diff --git a/components/utils/test-templates/index.html b/components/utils/test-templates/index.html index c15db638..b4fbbb6c 100644 --- a/components/utils/test-templates/index.html +++ b/components/utils/test-templates/index.html @@ -1 +1,5 @@ -Some base template, used in tests to check whether path rewriting works. +{% extends "base.html" %} + +{% block body %} +The default text +{% endblock body %}