Browse Source

Fix loading html files in themes outside of templates

Fix #412
index-subcmd
Vincent Prouillet 5 years ago
parent
commit
367f58b0a3
4 changed files with 23 additions and 3 deletions
  1. +1
    -0
      CHANGELOG.md
  2. +5
    -1
      components/site/src/lib.rs
  3. +11
    -2
      components/utils/src/templates.rs
  4. +6
    -0
      test_site/themes/sample/static/some-html.html

+ 1
- 0
CHANGELOG.md View File

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


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

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


+ 11
- 2
components/utils/src/templates.rs View File

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


+ 6
- 0
test_site/themes/sample/static/some-html.html 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( }}

Loading…
Cancel
Save