2018-09-09 18:12:53 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2018-10-31 07:18:57 +00:00
|
|
|
use tera::{Context, Tera};
|
2017-08-23 10:17:24 +00:00
|
|
|
|
2019-12-21 21:52:39 +00:00
|
|
|
use errors::{bail, Result};
|
2017-08-23 10:17:24 +00:00
|
|
|
|
2017-11-27 17:09:09 +00:00
|
|
|
static DEFAULT_TPL: &str = include_str!("default_tpl.html");
|
|
|
|
|
|
|
|
macro_rules! render_default_tpl {
|
2018-10-31 07:18:57 +00:00
|
|
|
($filename: expr, $url: expr) => {{
|
|
|
|
let mut context = Context::new();
|
|
|
|
context.insert("filename", $filename);
|
|
|
|
context.insert("url", $url);
|
2019-10-20 18:08:01 +00:00
|
|
|
Tera::one_off(DEFAULT_TPL, &context, true).map_err(std::convert::Into::into)
|
2018-10-31 07:18:57 +00:00
|
|
|
}};
|
2017-11-27 17:09:09 +00:00
|
|
|
}
|
|
|
|
|
2017-08-23 10:17:24 +00:00
|
|
|
/// Renders the given template with the given context, but also ensures that, if the default file
|
2017-08-24 23:38:03 +00:00
|
|
|
/// is not found, it will look up for the equivalent template for the current theme if there is one.
|
|
|
|
/// Lastly, if it's a default template (index, section or page), it will just return an empty string
|
|
|
|
/// to avoid an error if there isn't a template with that name
|
2018-10-31 07:18:57 +00:00
|
|
|
pub fn render_template(
|
|
|
|
name: &str,
|
|
|
|
tera: &Tera,
|
2019-01-23 18:20:02 +00:00
|
|
|
context: Context,
|
2018-10-31 07:18:57 +00:00
|
|
|
theme: &Option<String>,
|
|
|
|
) -> Result<String> {
|
2019-01-16 09:59:29 +00:00
|
|
|
// check if it is in the templates
|
2017-08-23 10:17:24 +00:00
|
|
|
if tera.templates.contains_key(name) {
|
2019-10-20 18:08:01 +00:00
|
|
|
return tera.render(name, &context).map_err(std::convert::Into::into);
|
2017-08-23 10:17:24 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 09:59:29 +00:00
|
|
|
// check if it is part of a theme
|
2018-05-11 11:54:16 +00:00
|
|
|
if let Some(ref t) = *theme {
|
2019-01-16 09:59:29 +00:00
|
|
|
let theme_template_name = format!("{}/templates/{}", t, name);
|
|
|
|
if tera.templates.contains_key(&theme_template_name) {
|
2019-10-20 18:08:01 +00:00
|
|
|
return tera.render(&theme_template_name, &context).map_err(std::convert::Into::into);
|
2019-01-16 09:59:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if it is part of ZOLA_TERA defaults
|
|
|
|
let default_name = format!("__zola_builtins/{}", name);
|
|
|
|
if tera.templates.contains_key(&default_name) {
|
2019-10-20 18:08:01 +00:00
|
|
|
return tera.render(&default_name, &context).map_err(std::convert::Into::into);
|
2017-08-23 10:17:24 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 17:09:09 +00:00
|
|
|
// maybe it's a default one?
|
|
|
|
match name {
|
2018-10-31 07:18:57 +00:00
|
|
|
"index.html" | "section.html" => render_default_tpl!(
|
|
|
|
name,
|
|
|
|
"https://www.getzola.org/documentation/templates/pages-sections/#section-variables"
|
|
|
|
),
|
|
|
|
"page.html" => render_default_tpl!(
|
|
|
|
name,
|
|
|
|
"https://www.getzola.org/documentation/templates/pages-sections/#page-variables"
|
|
|
|
),
|
2018-07-16 08:54:05 +00:00
|
|
|
"single.html" | "list.html" => {
|
2018-10-18 20:50:06 +00:00
|
|
|
render_default_tpl!(name, "https://www.getzola.org/documentation/templates/taxonomies/")
|
2018-07-31 13:17:31 +00:00
|
|
|
}
|
2018-10-31 07:18:57 +00:00
|
|
|
_ => bail!("Tried to render `{}` but the template wasn't found", name),
|
2017-08-24 23:38:03 +00:00
|
|
|
}
|
2017-08-23 10:17:24 +00:00
|
|
|
}
|
2017-08-24 23:38:03 +00:00
|
|
|
|
|
|
|
/// Rewrites the path from extend/macros of the theme used to ensure
|
|
|
|
/// that they will point to the right place (theme/templates/...)
|
|
|
|
/// Include is NOT supported as it would be a pain to add and using blocks
|
|
|
|
/// or macros is always better anyway for themes
|
2018-03-28 19:08:44 +00:00
|
|
|
/// This will also rename the shortcodes to NOT have the themes in the path
|
|
|
|
/// so themes shortcodes can be used.
|
2020-02-10 22:09:22 +00:00
|
|
|
pub fn rewrite_theme_paths(tera_theme: &mut Tera, site_templates: Vec<&str>, theme: &str) {
|
2018-03-28 19:08:44 +00:00
|
|
|
let mut shortcodes_to_move = vec![];
|
2018-09-09 18:12:53 +00:00
|
|
|
let mut templates = HashMap::new();
|
2020-02-10 22:09:22 +00:00
|
|
|
let old_templates = ::std::mem::replace(&mut tera_theme.templates, HashMap::new());
|
2018-03-28 19:08:44 +00:00
|
|
|
|
2017-08-24 23:38:03 +00:00
|
|
|
// We want to match the paths in the templates to the new names
|
2018-10-31 07:18:57 +00:00
|
|
|
for (key, mut tpl) in old_templates {
|
2018-09-09 18:12:53 +00:00
|
|
|
tpl.name = format!("{}/templates/{}", theme, tpl.name);
|
2020-02-10 22:09:22 +00:00
|
|
|
// 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
|
2017-08-24 23:38:03 +00:00
|
|
|
if let Some(ref p) = tpl.parent.clone() {
|
2020-02-10 22:09:22 +00:00
|
|
|
if !site_templates.contains(&p.as_ref()) {
|
|
|
|
tpl.parent = Some(format!("{}/templates/{}", theme, p));
|
|
|
|
}
|
2017-08-24 23:38:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Next the macros import
|
|
|
|
let mut updated = vec![];
|
|
|
|
for &(ref filename, ref namespace) in &tpl.imported_macro_files {
|
|
|
|
updated.push((format!("{}/templates/{}", theme, filename), namespace.to_string()));
|
|
|
|
}
|
|
|
|
tpl.imported_macro_files = updated;
|
2018-03-28 19:08:44 +00:00
|
|
|
|
|
|
|
if tpl.name.starts_with(&format!("{}/templates/shortcodes", theme)) {
|
|
|
|
let new_name = tpl.name.replace(&format!("{}/templates/", theme), "");
|
2018-09-09 18:12:53 +00:00
|
|
|
shortcodes_to_move.push((key, new_name.clone()));
|
2018-03-28 19:08:44 +00:00
|
|
|
tpl.name = new_name;
|
|
|
|
}
|
2018-09-09 18:12:53 +00:00
|
|
|
|
|
|
|
templates.insert(tpl.name.clone(), tpl);
|
2018-03-28 19:08:44 +00:00
|
|
|
}
|
|
|
|
|
2020-02-10 22:09:22 +00:00
|
|
|
tera_theme.templates = templates;
|
2018-09-09 18:12:53 +00:00
|
|
|
|
2018-03-28 19:08:44 +00:00
|
|
|
// and then replace shortcodes in the Tera instance using the new names
|
|
|
|
for (old_name, new_name) in shortcodes_to_move {
|
2020-02-10 22:09:22 +00:00
|
|
|
let tpl = tera_theme.templates.remove(&old_name).unwrap();
|
|
|
|
tera_theme.templates.insert(new_name, tpl);
|
2017-08-24 23:38:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2018-03-28 19:08:44 +00:00
|
|
|
use super::rewrite_theme_paths;
|
2018-10-31 07:18:57 +00:00
|
|
|
use tera::Tera;
|
2017-08-24 23:38:03 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_rewrite_all_paths_of_theme() {
|
2017-11-27 17:09:09 +00:00
|
|
|
let mut tera = Tera::parse("test-templates/*.html").unwrap();
|
2020-02-10 22:09:22 +00:00
|
|
|
rewrite_theme_paths(&mut tera, vec!["base.html"], "hyde");
|
2017-08-24 23:38:03 +00:00
|
|
|
// special case to make the test work: we also rename the files to
|
|
|
|
// match the imports
|
2020-02-10 22:09:22 +00:00
|
|
|
for (key, val) in &tera.templates.clone() {
|
2017-08-24 23:38:03 +00:00
|
|
|
tera.templates.insert(format!("hyde/templates/{}", key), val.clone());
|
|
|
|
}
|
2020-02-10 22:09:22 +00:00
|
|
|
// Adding our fake base
|
|
|
|
tera.add_raw_template("base.html", "Hello").unwrap();
|
2017-08-24 23:38:03 +00:00
|
|
|
tera.build_inheritance_chains().unwrap();
|
2020-02-10 22:09:22 +00:00
|
|
|
|
|
|
|
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())
|
|
|
|
);
|
2017-08-24 23:38:03 +00:00
|
|
|
}
|
|
|
|
}
|