You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
4.1KB

  1. use std::collections::HashMap;
  2. use tera::{Context, Tera};
  3. use errors::Result;
  4. static DEFAULT_TPL: &str = include_str!("default_tpl.html");
  5. macro_rules! render_default_tpl {
  6. ($filename: expr, $url: expr) => {{
  7. let mut context = Context::new();
  8. context.insert("filename", $filename);
  9. context.insert("url", $url);
  10. Tera::one_off(DEFAULT_TPL, &context, true).map_err(|e| e.into())
  11. }};
  12. }
  13. /// Renders the given template with the given context, but also ensures that, if the default file
  14. /// is not found, it will look up for the equivalent template for the current theme if there is one.
  15. /// Lastly, if it's a default template (index, section or page), it will just return an empty string
  16. /// to avoid an error if there isn't a template with that name
  17. pub fn render_template(
  18. name: &str,
  19. tera: &Tera,
  20. context: &Context,
  21. theme: &Option<String>,
  22. ) -> Result<String> {
  23. if tera.templates.contains_key(name) {
  24. return tera.render(name, context).map_err(|e| e.into());
  25. }
  26. if let Some(ref t) = *theme {
  27. return tera.render(&format!("{}/templates/{}", t, name), context).map_err(|e| e.into());
  28. }
  29. // maybe it's a default one?
  30. match name {
  31. "index.html" | "section.html" => render_default_tpl!(
  32. name,
  33. "https://www.getzola.org/documentation/templates/pages-sections/#section-variables"
  34. ),
  35. "page.html" => render_default_tpl!(
  36. name,
  37. "https://www.getzola.org/documentation/templates/pages-sections/#page-variables"
  38. ),
  39. "single.html" | "list.html" => {
  40. render_default_tpl!(name, "https://www.getzola.org/documentation/templates/taxonomies/")
  41. }
  42. _ => bail!("Tried to render `{}` but the template wasn't found", name),
  43. }
  44. }
  45. /// Rewrites the path from extend/macros of the theme used to ensure
  46. /// that they will point to the right place (theme/templates/...)
  47. /// Include is NOT supported as it would be a pain to add and using blocks
  48. /// or macros is always better anyway for themes
  49. /// This will also rename the shortcodes to NOT have the themes in the path
  50. /// so themes shortcodes can be used.
  51. pub fn rewrite_theme_paths(tera: &mut Tera, theme: &str) {
  52. let mut shortcodes_to_move = vec![];
  53. let mut templates = HashMap::new();
  54. let old_templates = ::std::mem::replace(&mut tera.templates, HashMap::new());
  55. // We want to match the paths in the templates to the new names
  56. for (key, mut tpl) in old_templates {
  57. tpl.name = format!("{}/templates/{}", theme, tpl.name);
  58. // First the parent if there is none
  59. if let Some(ref p) = tpl.parent.clone() {
  60. tpl.parent = Some(format!("{}/templates/{}", theme, p));
  61. }
  62. // Next the macros import
  63. let mut updated = vec![];
  64. for &(ref filename, ref namespace) in &tpl.imported_macro_files {
  65. updated.push((format!("{}/templates/{}", theme, filename), namespace.to_string()));
  66. }
  67. tpl.imported_macro_files = updated;
  68. if tpl.name.starts_with(&format!("{}/templates/shortcodes", theme)) {
  69. let new_name = tpl.name.replace(&format!("{}/templates/", theme), "");
  70. shortcodes_to_move.push((key, new_name.clone()));
  71. tpl.name = new_name;
  72. }
  73. templates.insert(tpl.name.clone(), tpl);
  74. }
  75. tera.templates = templates;
  76. // and then replace shortcodes in the Tera instance using the new names
  77. for (old_name, new_name) in shortcodes_to_move {
  78. let tpl = tera.templates.remove(&old_name).unwrap();
  79. tera.templates.insert(new_name, tpl);
  80. }
  81. }
  82. #[cfg(test)]
  83. mod tests {
  84. use super::rewrite_theme_paths;
  85. use tera::Tera;
  86. #[test]
  87. fn can_rewrite_all_paths_of_theme() {
  88. let mut tera = Tera::parse("test-templates/*.html").unwrap();
  89. rewrite_theme_paths(&mut tera, "hyde");
  90. // special case to make the test work: we also rename the files to
  91. // match the imports
  92. for (key, val) in tera.templates.clone() {
  93. tera.templates.insert(format!("hyde/templates/{}", key), val.clone());
  94. }
  95. tera.build_inheritance_chains().unwrap();
  96. }
  97. }