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.

207 lines
5.7KB

  1. use std::collections::HashMap;
  2. use chrono::prelude::*;
  3. use tera::Value;
  4. use toml;
  5. use errors::{Result};
  6. /// The front matter of every page
  7. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
  8. pub struct PageFrontMatter {
  9. /// <title> of the page
  10. pub title: Option<String>,
  11. /// Description in <meta> that appears when linked, e.g. on twitter
  12. pub description: Option<String>,
  13. /// Date if we want to order pages (ie blog post)
  14. pub date: Option<String>,
  15. /// The page slug. Will be used instead of the filename if present
  16. /// Can't be an empty string if present
  17. pub slug: Option<String>,
  18. /// The url the page appears at, overrides the slug if set in the front-matter
  19. /// otherwise is set after parsing front matter and sections
  20. /// Can't be an empty string if present
  21. pub url: Option<String>,
  22. /// Tags, not to be confused with categories
  23. pub tags: Option<Vec<String>>,
  24. /// Whether this page is a draft and should be published or not
  25. pub draft: Option<bool>,
  26. /// Only one category allowed
  27. pub category: Option<String>,
  28. /// Integer to use to order content. Lowest is at the bottom, highest first
  29. pub order: Option<usize>,
  30. /// Optional template, if we want to specify which template to render for that page
  31. #[serde(skip_serializing)]
  32. pub template: Option<String>,
  33. /// Any extra parameter present in the front matter
  34. pub extra: Option<HashMap<String, Value>>,
  35. }
  36. impl PageFrontMatter {
  37. pub fn parse(toml: &str) -> Result<PageFrontMatter> {
  38. let f: PageFrontMatter = match toml::from_str(toml) {
  39. Ok(d) => d,
  40. Err(e) => bail!(e),
  41. };
  42. if let Some(ref slug) = f.slug {
  43. if slug == "" {
  44. bail!("`slug` can't be empty if present")
  45. }
  46. }
  47. if let Some(ref url) = f.url {
  48. if url == "" {
  49. bail!("`url` can't be empty if present")
  50. }
  51. }
  52. Ok(f)
  53. }
  54. /// Converts the date in the front matter, which can be in 2 formats, into a NaiveDateTime
  55. pub fn date(&self) -> Option<NaiveDateTime> {
  56. match self.date {
  57. Some(ref d) => {
  58. if d.contains('T') {
  59. DateTime::parse_from_rfc3339(d).ok().and_then(|s| Some(s.naive_local()))
  60. } else {
  61. NaiveDate::parse_from_str(d, "%Y-%m-%d").ok().and_then(|s| Some(s.and_hms(0,0,0)))
  62. }
  63. },
  64. None => None,
  65. }
  66. }
  67. pub fn order(&self) -> usize {
  68. self.order.unwrap()
  69. }
  70. }
  71. impl Default for PageFrontMatter {
  72. fn default() -> PageFrontMatter {
  73. PageFrontMatter {
  74. title: None,
  75. description: None,
  76. date: None,
  77. slug: None,
  78. url: None,
  79. tags: None,
  80. draft: None,
  81. category: None,
  82. order: None,
  83. template: None,
  84. extra: None,
  85. }
  86. }
  87. }
  88. #[cfg(test)]
  89. mod tests {
  90. use super::PageFrontMatter;
  91. #[test]
  92. fn can_have_empty_front_matter() {
  93. let content = r#" "#;
  94. let res = PageFrontMatter::parse(content);
  95. assert!(res.is_ok());
  96. }
  97. #[test]
  98. fn can_parse_valid_front_matter() {
  99. let content = r#"
  100. title = "Hello"
  101. description = "hey there""#;
  102. let res = PageFrontMatter::parse(content);
  103. assert!(res.is_ok());
  104. let res = res.unwrap();
  105. assert_eq!(res.title.unwrap(), "Hello".to_string());
  106. assert_eq!(res.description.unwrap(), "hey there".to_string())
  107. }
  108. #[test]
  109. fn can_parse_tags() {
  110. let content = r#"
  111. title = "Hello"
  112. description = "hey there"
  113. slug = "hello-world"
  114. tags = ["rust", "html"]"#;
  115. let res = PageFrontMatter::parse(content);
  116. assert!(res.is_ok());
  117. let res = res.unwrap();
  118. assert_eq!(res.title.unwrap(), "Hello".to_string());
  119. assert_eq!(res.slug.unwrap(), "hello-world".to_string());
  120. assert_eq!(res.tags.unwrap(), ["rust".to_string(), "html".to_string()]);
  121. }
  122. #[test]
  123. fn errors_with_invalid_front_matter() {
  124. let content = r#"title = 1\n"#;
  125. let res = PageFrontMatter::parse(content);
  126. assert!(res.is_err());
  127. }
  128. #[test]
  129. fn errors_on_non_string_tag() {
  130. let content = r#"
  131. title = "Hello"
  132. description = "hey there"
  133. slug = "hello-world"
  134. tags = ["rust", 1]"#;
  135. let res = PageFrontMatter::parse(content);
  136. assert!(res.is_err());
  137. }
  138. #[test]
  139. fn errors_on_present_but_empty_slug() {
  140. let content = r#"
  141. title = "Hello"
  142. description = "hey there"
  143. slug = """#;
  144. let res = PageFrontMatter::parse(content);
  145. assert!(res.is_err());
  146. }
  147. #[test]
  148. fn errors_on_present_but_empty_url() {
  149. let content = r#"
  150. title = "Hello"
  151. description = "hey there"
  152. url = """#;
  153. let res = PageFrontMatter::parse(content);
  154. assert!(res.is_err());
  155. }
  156. #[test]
  157. fn can_parse_date_yyyy_mm_dd() {
  158. let content = r#"
  159. title = "Hello"
  160. description = "hey there"
  161. date = "2016-10-10""#;
  162. let res = PageFrontMatter::parse(content).unwrap();
  163. assert!(res.date().is_some());
  164. }
  165. #[test]
  166. fn can_parse_date_rfc3339() {
  167. let content = r#"
  168. title = "Hello"
  169. description = "hey there"
  170. date = "2002-10-02T15:00:00Z""#;
  171. let res = PageFrontMatter::parse(content).unwrap();
  172. assert!(res.date().is_some());
  173. }
  174. #[test]
  175. fn cannot_parse_random_date_format() {
  176. let content = r#"
  177. title = "Hello"
  178. description = "hey there"
  179. date = "2002/10/12""#;
  180. let res = PageFrontMatter::parse(content).unwrap();
  181. assert!(res.date().is_none());
  182. }
  183. }