Browse Source

Fix some theme extension

Closes #937
index-subcmd
Vincent Prouillet 4 years ago
parent
commit
855d2376df
4 changed files with 44 additions and 13 deletions
  1. +5
    -3
      components/site/src/lib.rs
  2. +24
    -9
      components/utils/src/templates.rs
  3. +10
    -0
      components/utils/test-templates/base.html
  4. +5
    -1
      components/utils/test-templates/index.html

+ 5
- 3
components/site/src/lib.rs View File

@@ -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


+ 24
- 9
components/utils/src/templates.rs View File

@@ -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())
);
}
}

+ 10
- 0
components/utils/test-templates/base.html View File

@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
</head>

<body>
{% block body %}
{% endblock body %}
</body>
</html>

+ 5
- 1
components/utils/test-templates/index.html View File

@@ -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 %}

Loading…
Cancel
Save