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.

106 lines
3.9KB

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