2017-05-03 14:16:09 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::path::{PathBuf};
|
|
|
|
|
|
|
|
use tera::{GlobalFn, Value, from_value, to_value, Result};
|
|
|
|
|
2017-05-23 11:03:25 +00:00
|
|
|
use content::{Page, Section};
|
2017-08-07 11:38:13 +00:00
|
|
|
use config::Config;
|
2017-07-01 07:47:41 +00:00
|
|
|
use utils::site::resolve_internal_link;
|
2017-05-03 14:16:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
pub fn make_get_page(all_pages: &HashMap<PathBuf, Page>) -> GlobalFn {
|
|
|
|
let mut pages = HashMap::new();
|
|
|
|
for page in all_pages.values() {
|
2017-05-15 10:53:39 +00:00
|
|
|
pages.insert(page.file.relative.clone(), page.clone());
|
2017-05-03 14:16:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Box::new(move |args| -> Result<Value> {
|
|
|
|
match args.get("path") {
|
|
|
|
Some(val) => match from_value::<String>(val.clone()) {
|
|
|
|
Ok(v) => {
|
|
|
|
match pages.get(&v) {
|
|
|
|
Some(p) => Ok(to_value(p).unwrap()),
|
|
|
|
None => Err(format!("Page `{}` not found.", v).into())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(_) => Err(format!("`get_page` received path={:?} but it requires a string", val).into()),
|
|
|
|
},
|
|
|
|
None => Err("`get_page` requires a `path` argument.".into()),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2017-05-17 10:04:26 +00:00
|
|
|
|
2017-05-23 11:03:25 +00:00
|
|
|
pub fn make_get_section(all_sections: &HashMap<PathBuf, Section>) -> GlobalFn {
|
|
|
|
let mut sections = HashMap::new();
|
|
|
|
for section in all_sections.values() {
|
|
|
|
sections.insert(section.file.relative.clone(), section.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
Box::new(move |args| -> Result<Value> {
|
|
|
|
match args.get("path") {
|
|
|
|
Some(val) => match from_value::<String>(val.clone()) {
|
|
|
|
Ok(v) => {
|
|
|
|
match sections.get(&v) {
|
|
|
|
Some(p) => Ok(to_value(p).unwrap()),
|
|
|
|
None => Err(format!("Section `{}` not found.", v).into())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(_) => Err(format!("`get_section` received path={:?} but it requires a string", val).into()),
|
|
|
|
},
|
|
|
|
None => Err("`get_section` requires a `path` argument.".into()),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-08-07 14:29:58 +00:00
|
|
|
pub fn make_get_url(permalinks: HashMap<String, String>, config: Config) -> GlobalFn {
|
2017-08-07 11:38:13 +00:00
|
|
|
Box::new(move |args| -> Result<Value> {
|
|
|
|
let cachebust = args
|
|
|
|
.get("cachebust")
|
2017-08-07 14:29:58 +00:00
|
|
|
.map_or(false, |c| {
|
|
|
|
from_value::<bool>(c.clone()).unwrap_or(false)
|
2017-08-07 11:38:13 +00:00
|
|
|
});
|
|
|
|
|
2017-08-07 14:29:58 +00:00
|
|
|
if args.contains_key("link") {
|
|
|
|
println!("> DEPRECATION -- `link` is deprecated for `get_url`: use `path` instead");
|
|
|
|
}
|
|
|
|
|
|
|
|
match args.get("link").or_else(|| args.get("path")) {
|
2017-08-07 11:38:13 +00:00
|
|
|
Some(val) => match from_value::<String>(val.clone()) {
|
|
|
|
Ok(v) => {
|
2017-08-07 14:29:58 +00:00
|
|
|
// Internal link
|
|
|
|
if v.starts_with("./") {
|
|
|
|
match resolve_internal_link(&v, &permalinks) {
|
|
|
|
Ok(url) => Ok(to_value(url).unwrap()),
|
|
|
|
Err(_) => Err(format!("Could not resolve URL for link `{}` not found.", v).into())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// anything else
|
|
|
|
let mut permalink = config.make_permalink(&v);
|
|
|
|
if cachebust {
|
|
|
|
permalink = format!("{}?t={}", permalink, config.build_timestamp.unwrap());
|
|
|
|
}
|
|
|
|
return Ok(to_value(permalink).unwrap());
|
2017-08-07 11:38:13 +00:00
|
|
|
}
|
|
|
|
},
|
2017-08-07 14:29:58 +00:00
|
|
|
Err(_) => Err(format!("`get_url` received path={:?} but it requires a string", val).into()),
|
2017-08-07 11:38:13 +00:00
|
|
|
},
|
2017-08-07 14:29:58 +00:00
|
|
|
None => Err("`get_url` requires a `path` argument.".into()),
|
2017-08-07 11:38:13 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2017-08-07 14:29:58 +00:00
|
|
|
use super::make_get_url;
|
2017-08-07 11:38:13 +00:00
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use tera::to_value;
|
|
|
|
|
|
|
|
use config::Config;
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
2017-08-07 14:29:58 +00:00
|
|
|
fn can_add_cachebust_to_url() {
|
2017-08-07 11:38:13 +00:00
|
|
|
let config = Config::default();
|
2017-08-07 14:29:58 +00:00
|
|
|
let static_fn = make_get_url(HashMap::new(), config);
|
2017-08-07 11:38:13 +00:00
|
|
|
let mut args = HashMap::new();
|
|
|
|
args.insert("path".to_string(), to_value("app.css").unwrap());
|
2017-08-07 14:29:58 +00:00
|
|
|
args.insert("cachebust".to_string(), to_value(true).unwrap());
|
2017-08-07 11:38:13 +00:00
|
|
|
assert_eq!(static_fn(args).unwrap(), "http://a-website.com/app.css/?t=1");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2017-08-07 14:29:58 +00:00
|
|
|
fn can_link_to_some_static_file() {
|
2017-08-07 11:38:13 +00:00
|
|
|
let config = Config::default();
|
2017-08-07 14:29:58 +00:00
|
|
|
let static_fn = make_get_url(HashMap::new(), config);
|
2017-08-07 11:38:13 +00:00
|
|
|
let mut args = HashMap::new();
|
|
|
|
args.insert("path".to_string(), to_value("app.css").unwrap());
|
|
|
|
assert_eq!(static_fn(args).unwrap(), "http://a-website.com/app.css/");
|
|
|
|
}
|
|
|
|
}
|