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.

125 lines
4.3KB

  1. use std::collections::HashMap;
  2. use std::path::{PathBuf};
  3. use tera::{GlobalFn, Value, from_value, to_value, Result};
  4. use content::{Page, Section};
  5. use config::Config;
  6. use utils::site::resolve_internal_link;
  7. pub fn make_get_page(all_pages: &HashMap<PathBuf, Page>) -> GlobalFn {
  8. let mut pages = HashMap::new();
  9. for page in all_pages.values() {
  10. pages.insert(page.file.relative.clone(), page.clone());
  11. }
  12. Box::new(move |args| -> Result<Value> {
  13. match args.get("path") {
  14. Some(val) => match from_value::<String>(val.clone()) {
  15. Ok(v) => {
  16. match pages.get(&v) {
  17. Some(p) => Ok(to_value(p).unwrap()),
  18. None => Err(format!("Page `{}` not found.", v).into())
  19. }
  20. },
  21. Err(_) => Err(format!("`get_page` received path={:?} but it requires a string", val).into()),
  22. },
  23. None => Err("`get_page` requires a `path` argument.".into()),
  24. }
  25. })
  26. }
  27. pub fn make_get_section(all_sections: &HashMap<PathBuf, Section>) -> GlobalFn {
  28. let mut sections = HashMap::new();
  29. for section in all_sections.values() {
  30. sections.insert(section.file.relative.clone(), section.clone());
  31. }
  32. Box::new(move |args| -> Result<Value> {
  33. match args.get("path") {
  34. Some(val) => match from_value::<String>(val.clone()) {
  35. Ok(v) => {
  36. match sections.get(&v) {
  37. Some(p) => Ok(to_value(p).unwrap()),
  38. None => Err(format!("Section `{}` not found.", v).into())
  39. }
  40. },
  41. Err(_) => Err(format!("`get_section` received path={:?} but it requires a string", val).into()),
  42. },
  43. None => Err("`get_section` requires a `path` argument.".into()),
  44. }
  45. })
  46. }
  47. pub fn make_get_url(permalinks: HashMap<String, String>) -> GlobalFn {
  48. Box::new(move |args| -> Result<Value> {
  49. match args.get("link") {
  50. Some(val) => match from_value::<String>(val.clone()) {
  51. Ok(v) => match resolve_internal_link(&v, &permalinks) {
  52. Ok(url) => Ok(to_value(url).unwrap()),
  53. Err(_) => Err(format!("Could not resolve URL for link `{}` not found.", v).into())
  54. },
  55. Err(_) => Err(format!("`get_url` received link={:?} but it requires a string", val).into()),
  56. },
  57. None => Err("`get_url` requires a `link` argument.".into()),
  58. }
  59. })
  60. }
  61. pub fn make_get_static_url(config: Config) -> GlobalFn {
  62. Box::new(move |args| -> Result<Value> {
  63. let cachebust = args
  64. .get("cachebust")
  65. .map_or(true, |c| {
  66. from_value::<bool>(c.clone()).unwrap_or(true)
  67. });
  68. match args.get("path") {
  69. Some(val) => match from_value::<String>(val.clone()) {
  70. Ok(v) => {
  71. let mut permalink = config.make_permalink(&v);
  72. if cachebust {
  73. permalink = format!("{}?t={}", permalink, config.build_timestamp.unwrap());
  74. }
  75. Ok(to_value(permalink).unwrap())
  76. },
  77. Err(_) => Err(format!("`get_static_url` received path={:?} but it requires a string", val).into()),
  78. },
  79. None => Err("`get_static_url` requires a `path` argument.".into()),
  80. }
  81. })
  82. }
  83. #[cfg(test)]
  84. mod tests {
  85. use super::make_get_static_url;
  86. use std::collections::HashMap;
  87. use tera::to_value;
  88. use config::Config;
  89. #[test]
  90. fn add_cachebust_to_static_url_by_default() {
  91. let config = Config::default();
  92. let static_fn = make_get_static_url(config);
  93. let mut args = HashMap::new();
  94. args.insert("path".to_string(), to_value("app.css").unwrap());
  95. assert_eq!(static_fn(args).unwrap(), "http://a-website.com/app.css/?t=1");
  96. }
  97. #[test]
  98. fn can_disable_cachebust_for_static_url() {
  99. let config = Config::default();
  100. let static_fn = make_get_static_url(config);
  101. let mut args = HashMap::new();
  102. args.insert("path".to_string(), to_value("app.css").unwrap());
  103. args.insert("cachebust".to_string(), to_value(false).unwrap());
  104. assert_eq!(static_fn(args).unwrap(), "http://a-website.com/app.css/");
  105. }
  106. }