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.

607 lines
20KB

  1. extern crate toml;
  2. extern crate serde_json;
  3. extern crate error_chain;
  4. use std::collections::HashMap;
  5. use std::sync::{Arc, Mutex};
  6. use std::path::PathBuf;
  7. use csv::Reader;
  8. use tera::{GlobalFn, Value, from_value, to_value, Result, Map};
  9. use library::{Taxonomy, Library};
  10. use config::Config;
  11. use utils::site::resolve_internal_link;
  12. use utils::fs::read_file;
  13. use imageproc;
  14. macro_rules! required_arg {
  15. ($ty: ty, $e: expr, $err: expr) => {
  16. match $e {
  17. Some(v) => match from_value::<$ty>(v.clone()) {
  18. Ok(u) => u,
  19. Err(_) => return Err($err.into())
  20. },
  21. None => return Err($err.into())
  22. }
  23. };
  24. }
  25. macro_rules! optional_arg {
  26. ($ty: ty, $e: expr, $err: expr) => {
  27. match $e {
  28. Some(v) => match from_value::<$ty>(v.clone()) {
  29. Ok(u) => Some(u),
  30. Err(_) => return Err($err.into())
  31. },
  32. None => None
  33. }
  34. };
  35. }
  36. pub fn make_trans(config: Config) -> GlobalFn {
  37. let translations_config = config.translations;
  38. let default_lang = config.default_language.clone();
  39. Box::new(move |args| -> Result<Value> {
  40. let key = required_arg!(String, args.get("key"), "`trans` requires a `key` argument.");
  41. let lang = optional_arg!(
  42. String,
  43. args.get("lang"),
  44. "`trans`: `lang` must be a string."
  45. ).unwrap_or_else(|| default_lang.clone());
  46. let translations = &translations_config[lang.as_str()];
  47. Ok(to_value(&translations[key.as_str()]).unwrap())
  48. })
  49. }
  50. pub fn make_get_page(library: &Library) -> GlobalFn {
  51. let mut pages = HashMap::new();
  52. for page in library.pages_values() {
  53. pages.insert(
  54. page.file.relative.clone(),
  55. to_value(library.get_page(&page.file.path).unwrap().to_serialized(library)).unwrap(),
  56. );
  57. }
  58. Box::new(move |args| -> Result<Value> {
  59. let path = required_arg!(
  60. String,
  61. args.get("path"),
  62. "`get_page` requires a `path` argument with a string value"
  63. );
  64. match pages.get(&path) {
  65. Some(p) => Ok(p.clone()),
  66. None => Err(format!("Page `{}` not found.", path).into())
  67. }
  68. })
  69. }
  70. pub fn make_get_section(library: &Library) -> GlobalFn {
  71. let mut sections = HashMap::new();
  72. let mut sections_basic = HashMap::new();
  73. for section in library.sections_values() {
  74. sections.insert(
  75. section.file.relative.clone(),
  76. to_value(library.get_section(&section.file.path).unwrap().to_serialized(library)).unwrap(),
  77. );
  78. sections_basic.insert(
  79. section.file.relative.clone(),
  80. to_value(library.get_section(&section.file.path).unwrap().to_serialized_basic(library)).unwrap(),
  81. );
  82. }
  83. Box::new(move |args| -> Result<Value> {
  84. let path = required_arg!(
  85. String,
  86. args.get("path"),
  87. "`get_section` requires a `path` argument with a string value"
  88. );
  89. let metadata_only = args
  90. .get("metadata_only")
  91. .map_or(false, |c| {
  92. from_value::<bool>(c.clone()).unwrap_or(false)
  93. });
  94. let container = if metadata_only {
  95. &sections_basic
  96. } else {
  97. &sections
  98. };
  99. match container.get(&path) {
  100. Some(p) => Ok(p.clone()),
  101. None => Err(format!("Section `{}` not found.", path).into())
  102. }
  103. })
  104. }
  105. pub fn make_get_url(permalinks: HashMap<String, String>, config: Config) -> GlobalFn {
  106. Box::new(move |args| -> Result<Value> {
  107. let cachebust = args
  108. .get("cachebust")
  109. .map_or(false, |c| {
  110. from_value::<bool>(c.clone()).unwrap_or(false)
  111. });
  112. let trailing_slash = args
  113. .get("trailing_slash")
  114. .map_or(false, |c| {
  115. from_value::<bool>(c.clone()).unwrap_or(false)
  116. });
  117. let path = required_arg!(
  118. String,
  119. args.get("path"),
  120. "`get_url` requires a `path` argument with a string value"
  121. );
  122. if path.starts_with("./") {
  123. match resolve_internal_link(&path, &permalinks) {
  124. Ok(url) => Ok(to_value(url).unwrap()),
  125. Err(_) => Err(format!("Could not resolve URL for link `{}` not found.", path).into())
  126. }
  127. } else {
  128. // anything else
  129. let mut permalink = config.make_permalink(&path);
  130. if !trailing_slash && permalink.ends_with('/') {
  131. permalink.pop(); // Removes the slash
  132. }
  133. if cachebust {
  134. permalink = format!("{}?t={}", permalink, config.build_timestamp.unwrap());
  135. }
  136. Ok(to_value(permalink).unwrap())
  137. }
  138. })
  139. }
  140. pub fn make_get_taxonomy(all_taxonomies: &[Taxonomy], library: &Library) -> GlobalFn {
  141. let mut taxonomies = HashMap::new();
  142. for taxonomy in all_taxonomies {
  143. taxonomies.insert(
  144. taxonomy.kind.name.clone(),
  145. to_value(taxonomy.to_serialized(library)).unwrap()
  146. );
  147. }
  148. Box::new(move |args| -> Result<Value> {
  149. let kind = required_arg!(
  150. String,
  151. args.get("kind"),
  152. "`get_taxonomy` requires a `kind` argument with a string value"
  153. );
  154. let container = match taxonomies.get(&kind) {
  155. Some(c) => c,
  156. None => return Err(
  157. format!("`get_taxonomy` received an unknown taxonomy as kind: {}", kind).into()
  158. ),
  159. };
  160. Ok(to_value(container).unwrap())
  161. })
  162. }
  163. pub fn make_get_taxonomy_url(all_taxonomies: &[Taxonomy]) -> GlobalFn {
  164. let mut taxonomies = HashMap::new();
  165. for taxonomy in all_taxonomies {
  166. let mut items = HashMap::new();
  167. for item in &taxonomy.items {
  168. items.insert(item.name.clone(), item.permalink.clone());
  169. }
  170. taxonomies.insert(taxonomy.kind.name.clone(), items);
  171. }
  172. Box::new(move |args| -> Result<Value> {
  173. let kind = required_arg!(
  174. String,
  175. args.get("kind"),
  176. "`get_taxonomy_url` requires a `kind` argument with a string value"
  177. );
  178. let name = required_arg!(
  179. String,
  180. args.get("name"),
  181. "`get_taxonomy_url` requires a `name` argument with a string value"
  182. );
  183. let container = match taxonomies.get(&kind) {
  184. Some(c) => c,
  185. None => return Err(
  186. format!("`get_taxonomy_url` received an unknown taxonomy as kind: {}", kind).into()
  187. )
  188. };
  189. if let Some(ref permalink) = container.get(&name) {
  190. return Ok(to_value(permalink.clone()).unwrap());
  191. }
  192. Err(
  193. format!("`get_taxonomy_url`: couldn't find `{}` in `{}` taxonomy", name, kind).into()
  194. )
  195. })
  196. }
  197. pub fn make_resize_image(imageproc: Arc<Mutex<imageproc::Processor>>) -> GlobalFn {
  198. static DEFAULT_OP: &'static str = "fill";
  199. const DEFAULT_Q: u8 = 75;
  200. Box::new(move |args| -> Result<Value> {
  201. let path = required_arg!(
  202. String,
  203. args.get("path"),
  204. "`resize_image` requires a `path` argument with a string value"
  205. );
  206. let width = optional_arg!(
  207. u32,
  208. args.get("width"),
  209. "`resize_image`: `width` must be a non-negative integer"
  210. );
  211. let height = optional_arg!(
  212. u32,
  213. args.get("height"),
  214. "`resize_image`: `height` must be a non-negative integer"
  215. );
  216. let op = optional_arg!(
  217. String,
  218. args.get("op"),
  219. "`resize_image`: `op` must be a string"
  220. ).unwrap_or_else(|| DEFAULT_OP.to_string());
  221. let quality = optional_arg!(
  222. u8,
  223. args.get("quality"),
  224. "`resize_image`: `quality` must be a number"
  225. ).unwrap_or(DEFAULT_Q);
  226. if quality == 0 || quality > 100 {
  227. return Err("`resize_image`: `quality` must be in range 1-100".to_string().into());
  228. }
  229. let mut imageproc = imageproc.lock().unwrap();
  230. if !imageproc.source_exists(&path) {
  231. return Err(format!("`resize_image`: Cannot find path: {}", path).into());
  232. }
  233. let imageop = imageproc::ImageOp::from_args(path.clone(), &op, width, height, quality)
  234. .map_err(|e| format!("`resize_image`: {}", e))?;
  235. let url = imageproc.insert(imageop);
  236. to_value(url).map_err(|err| err.into())
  237. })
  238. }
  239. /// A global function to load data from a data file.
  240. /// Currently the supported formats are json, toml and csv
  241. pub fn make_load_data(content_path: PathBuf) -> GlobalFn {
  242. Box::new(move |args| -> Result<Value> {
  243. let path_arg: String = required_arg!(
  244. String,
  245. args.get("path"),
  246. "`load_data`: requires a `path` argument with a string value, being a path to a file"
  247. );
  248. let kind_arg = optional_arg!(
  249. String,
  250. args.get("kind"),
  251. "`load_data`: `kind` needs to be an argument with a string value, being one of the supported `load_data` file types (csv, json, toml)"
  252. );
  253. let full_path = content_path.join(&path_arg);
  254. let extension = match full_path.extension() {
  255. Some(value) => value.to_str().unwrap().to_lowercase(),
  256. None => return Err(format!("`load_data`: Cannot parse file extension of specified file: {}", path_arg).into())
  257. };
  258. let file_kind = kind_arg.unwrap_or(extension);
  259. let result_value: Result<Value> = match file_kind.as_str() {
  260. "toml" => load_toml(&full_path),
  261. "csv" => load_csv(&full_path),
  262. "json" => load_json(&full_path),
  263. _ => return Err(format!("'load_data': {} - is an unsupported file kind", file_kind).into())
  264. };
  265. result_value
  266. })
  267. }
  268. /// load/parse a json file from the given path and place it into a
  269. /// tera value
  270. fn load_json(json_path: &PathBuf) -> Result<Value> {
  271. let content_string: String = read_file(json_path)
  272. .map_err(|e| format!("`load_data`: error {} loading json file {}", json_path.to_str().unwrap(), e))?;
  273. let json_content = serde_json::from_str(content_string.as_str()).unwrap();
  274. let tera_value: Value = json_content;
  275. return Ok(tera_value);
  276. }
  277. /// load/parse a toml file from the given path, and place it into a
  278. /// tera Value
  279. fn load_toml(toml_path: &PathBuf) -> Result<Value> {
  280. let content_string: String = read_file(toml_path)
  281. .map_err(|e| format!("`load_data`: error {} loading toml file {}", toml_path.to_str().unwrap(), e))?;
  282. let toml_content: toml::Value = toml::from_str(&content_string)
  283. .map_err(|e| format!("'load_data': {} - {}", toml_path.to_str().unwrap(), e))?;
  284. to_value(toml_content).map_err(|err| err.into())
  285. }
  286. /// Load/parse a csv file from the given path, and place it into a
  287. /// tera Value.
  288. ///
  289. /// An example csv file `example.csv` could be:
  290. /// ```csv
  291. /// Number, Title
  292. /// 1,Gutenberg
  293. /// 2,Printing
  294. /// ```
  295. /// The json value output would be:
  296. /// ```json
  297. /// {
  298. /// "headers": ["Number", "Title"],
  299. /// "records": [
  300. /// ["1", "Gutenberg"],
  301. /// ["2", "Printing"]
  302. /// ],
  303. /// }
  304. /// ```
  305. fn load_csv(csv_path: &PathBuf) -> Result<Value> {
  306. let mut reader = Reader::from_path(csv_path.clone())
  307. .map_err(|e| format!("'load_data': {} - {}", csv_path.to_str().unwrap(), e))?;
  308. let mut csv_map = Map::new();
  309. {
  310. let hdrs = reader.headers()
  311. .map_err(|e| format!("'load_data': {} - {} - unable to read CSV header line (line 1) for CSV file", csv_path.to_str().unwrap(), e))?;
  312. let headers_array = hdrs.iter()
  313. .map(|v| Value::String(v.to_string()))
  314. .collect();
  315. csv_map.insert(String::from("headers"), Value::Array(headers_array));
  316. }
  317. {
  318. let records = reader.records();
  319. let mut records_array: Vec<Value> = Vec::new();
  320. for result in records {
  321. let record = result.unwrap();
  322. let mut elements_array: Vec<Value> = Vec::new();
  323. for e in record.into_iter() {
  324. elements_array.push(Value::String(String::from(e)));
  325. }
  326. records_array.push(Value::Array(elements_array));
  327. }
  328. csv_map.insert(String::from("records"), Value::Array(records_array));
  329. }
  330. let csv_value: Value = Value::Object(csv_map);
  331. to_value(csv_value).map_err(|err| err.into())
  332. }
  333. #[cfg(test)]
  334. mod tests {
  335. use super::{make_get_url, make_get_taxonomy, make_get_taxonomy_url, make_trans, make_load_data};
  336. use std::collections::HashMap;
  337. use std::path::PathBuf;
  338. use tera::{to_value, Value};
  339. use config::{Config, Taxonomy as TaxonomyConfig};
  340. use library::{Taxonomy, TaxonomyItem, Library};
  341. #[test]
  342. fn can_add_cachebust_to_url() {
  343. let config = Config::default();
  344. let static_fn = make_get_url(HashMap::new(), config);
  345. let mut args = HashMap::new();
  346. args.insert("path".to_string(), to_value("app.css").unwrap());
  347. args.insert("cachebust".to_string(), to_value(true).unwrap());
  348. assert_eq!(static_fn(args).unwrap(), "http://a-website.com/app.css?t=1");
  349. }
  350. #[test]
  351. fn can_add_trailing_slashes() {
  352. let config = Config::default();
  353. let static_fn = make_get_url(HashMap::new(), config);
  354. let mut args = HashMap::new();
  355. args.insert("path".to_string(), to_value("app.css").unwrap());
  356. args.insert("trailing_slash".to_string(), to_value(true).unwrap());
  357. assert_eq!(static_fn(args).unwrap(), "http://a-website.com/app.css/");
  358. }
  359. #[test]
  360. fn can_add_slashes_and_cachebust() {
  361. let config = Config::default();
  362. let static_fn = make_get_url(HashMap::new(), config);
  363. let mut args = HashMap::new();
  364. args.insert("path".to_string(), to_value("app.css").unwrap());
  365. args.insert("trailing_slash".to_string(), to_value(true).unwrap());
  366. args.insert("cachebust".to_string(), to_value(true).unwrap());
  367. assert_eq!(static_fn(args).unwrap(), "http://a-website.com/app.css/?t=1");
  368. }
  369. #[test]
  370. fn can_link_to_some_static_file() {
  371. let config = Config::default();
  372. let static_fn = make_get_url(HashMap::new(), config);
  373. let mut args = HashMap::new();
  374. args.insert("path".to_string(), to_value("app.css").unwrap());
  375. assert_eq!(static_fn(args).unwrap(), "http://a-website.com/app.css");
  376. }
  377. #[test]
  378. fn can_get_taxonomy() {
  379. let taxo_config = TaxonomyConfig { name: "tags".to_string(), ..TaxonomyConfig::default() };
  380. let library = Library::new(0, 0);
  381. let tag = TaxonomyItem::new(
  382. "Programming",
  383. "tags",
  384. &Config::default(),
  385. vec![],
  386. &library
  387. );
  388. let tags = Taxonomy {
  389. kind: taxo_config,
  390. items: vec![tag],
  391. };
  392. let taxonomies = vec![tags.clone()];
  393. let static_fn = make_get_taxonomy(&taxonomies, &library);
  394. // can find it correctly
  395. let mut args = HashMap::new();
  396. args.insert("kind".to_string(), to_value("tags").unwrap());
  397. let res = static_fn(args).unwrap();
  398. let res_obj = res.as_object().unwrap();
  399. assert_eq!(res_obj["kind"], to_value(tags.kind).unwrap());
  400. assert_eq!(res_obj["items"].clone().as_array().unwrap().len(), 1);
  401. assert_eq!(
  402. res_obj["items"].clone().as_array().unwrap()[0].clone().as_object().unwrap()["name"],
  403. Value::String("Programming".to_string())
  404. );
  405. assert_eq!(
  406. res_obj["items"].clone().as_array().unwrap()[0].clone().as_object().unwrap()["slug"],
  407. Value::String("programming".to_string())
  408. );
  409. assert_eq!(
  410. res_obj["items"].clone().as_array().unwrap()[0].clone().as_object().unwrap()["permalink"],
  411. Value::String("http://a-website.com/tags/programming/".to_string())
  412. );
  413. assert_eq!(
  414. res_obj["items"].clone().as_array().unwrap()[0].clone().as_object().unwrap()["pages"],
  415. Value::Array(vec![])
  416. );
  417. // and errors if it can't find it
  418. let mut args = HashMap::new();
  419. args.insert("kind".to_string(), to_value("something-else").unwrap());
  420. assert!(static_fn(args).is_err());
  421. }
  422. #[test]
  423. fn can_get_taxonomy_url() {
  424. let taxo_config = TaxonomyConfig { name: "tags".to_string(), ..TaxonomyConfig::default() };
  425. let library = Library::new(0, 0);
  426. let tag = TaxonomyItem::new(
  427. "Programming",
  428. "tags",
  429. &Config::default(),
  430. vec![],
  431. &library
  432. );
  433. let tags = Taxonomy {
  434. kind: taxo_config,
  435. items: vec![tag],
  436. };
  437. let taxonomies = vec![tags.clone()];
  438. let static_fn = make_get_taxonomy_url(&taxonomies);
  439. // can find it correctly
  440. let mut args = HashMap::new();
  441. args.insert("kind".to_string(), to_value("tags").unwrap());
  442. args.insert("name".to_string(), to_value("Programming").unwrap());
  443. assert_eq!(static_fn(args).unwrap(), to_value("http://a-website.com/tags/programming/").unwrap());
  444. // and errors if it can't find it
  445. let mut args = HashMap::new();
  446. args.insert("kind".to_string(), to_value("tags").unwrap());
  447. args.insert("name".to_string(), to_value("random").unwrap());
  448. assert!(static_fn(args).is_err());
  449. }
  450. #[test]
  451. fn can_translate_a_string() {
  452. let trans_config = r#"
  453. base_url = "https://remplace-par-ton-url.fr"
  454. default_language = "fr"
  455. [translations]
  456. [translations.fr]
  457. title = "Un titre"
  458. [translations.en]
  459. title = "A title"
  460. "#;
  461. let config = Config::parse(trans_config).unwrap();
  462. let static_fn = make_trans(config);
  463. let mut args = HashMap::new();
  464. args.insert("key".to_string(), to_value("title").unwrap());
  465. assert_eq!(static_fn(args.clone()).unwrap(), "Un titre");
  466. args.insert("lang".to_string(), to_value("en").unwrap());
  467. assert_eq!(static_fn(args.clone()).unwrap(), "A title");
  468. args.insert("lang".to_string(), to_value("fr").unwrap());
  469. assert_eq!(static_fn(args.clone()).unwrap(), "Un titre");
  470. }
  471. #[test]
  472. fn can_load_toml()
  473. {
  474. let static_fn = make_load_data(PathBuf::from("../utils/test-files"));
  475. let mut args = HashMap::new();
  476. args.insert("path".to_string(), to_value("test.toml").unwrap());
  477. let result = static_fn(args.clone()).unwrap();
  478. //TOML does not load in order, and also dates are not returned as strings, but
  479. //rather as another object with a key and value
  480. assert_eq!(result, json!({
  481. "category": {
  482. "date": {
  483. "$__toml_private_datetime": "1979-05-27T07:32:00Z"
  484. },
  485. "key": "value"
  486. },
  487. }));
  488. }
  489. #[test]
  490. fn can_load_csv()
  491. {
  492. let static_fn = make_load_data(PathBuf::from("../utils/test-files"));
  493. let mut args = HashMap::new();
  494. args.insert("path".to_string(), to_value("test.csv").unwrap());
  495. let result = static_fn(args.clone()).unwrap();
  496. assert_eq!(result, json!({
  497. "headers": ["Number", "Title"],
  498. "records": [
  499. ["1", "Gutenberg"],
  500. ["2", "Printing"]
  501. ],
  502. }))
  503. }
  504. #[test]
  505. fn can_load_json()
  506. {
  507. let static_fn = make_load_data(PathBuf::from("../utils/test-files"));
  508. let mut args = HashMap::new();
  509. args.insert("path".to_string(), to_value("test.json").unwrap());
  510. let result = static_fn(args.clone()).unwrap();
  511. assert_eq!(result, json!({
  512. "key": "value",
  513. "array": [1, 2, 3],
  514. "subpackage": {
  515. "subkey": 5
  516. }
  517. }))
  518. }
  519. }