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.

46 lines
1.6KB

  1. use std::collections::HashMap;
  2. use std::path::{PathBuf};
  3. use tera::{GlobalFn, Value, from_value, to_value, Result};
  4. use content::Page;
  5. use site::resolve_internal_link;
  6. pub fn make_get_page(all_pages: &HashMap<PathBuf, Page>) -> GlobalFn {
  7. let mut pages = HashMap::new();
  8. for page in all_pages.values() {
  9. pages.insert(page.file.relative.clone(), page.clone());
  10. }
  11. Box::new(move |args| -> Result<Value> {
  12. match args.get("path") {
  13. Some(val) => match from_value::<String>(val.clone()) {
  14. Ok(v) => {
  15. match pages.get(&v) {
  16. Some(p) => Ok(to_value(p).unwrap()),
  17. None => Err(format!("Page `{}` not found.", v).into())
  18. }
  19. },
  20. Err(_) => Err(format!("`get_page` received path={:?} but it requires a string", val).into()),
  21. },
  22. None => Err("`get_page` requires a `path` argument.".into()),
  23. }
  24. })
  25. }
  26. pub fn make_get_url(permalinks: HashMap<String, String>,) -> GlobalFn {
  27. Box::new(move |args| -> Result<Value> {
  28. match args.get("link") {
  29. Some(val) => match from_value::<String>(val.clone()) {
  30. Ok(v) => match resolve_internal_link(&v, &permalinks) {
  31. Ok(url) => Ok(to_value(url).unwrap()),
  32. Err(_) => Err(format!("Could not resolve URL for link `{}` not found.", v).into())
  33. },
  34. Err(_) => Err(format!("`get_url` received link={:?} but it requires a string", val).into()),
  35. },
  36. None => Err("`get_url` requires a `link` argument.".into()),
  37. }
  38. })
  39. }