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.

80 lines
2.0KB

  1. /// A page, can be a blog post or a basic page
  2. use std::collections::HashMap;
  3. use pulldown_cmark as cmark;
  4. use regex::Regex;
  5. use toml::Parser;
  6. use errors::{Result, ErrorKind};
  7. lazy_static! {
  8. static ref DELIM_RE: Regex = Regex::new(r"\+\+\+\s*\r?\n").unwrap();
  9. }
  10. #[derive(Debug, PartialEq)]
  11. struct Page {
  12. // <title> of the page
  13. title: String,
  14. // the url the page appears at (slug form)
  15. url: String,
  16. // the actual content of the page
  17. content: String,
  18. // tags, not to be confused with categories
  19. tags: Vec<String>,
  20. // any extra parameter present in the front matter
  21. // it will be passed to the template context
  22. extra: HashMap<String, String>,
  23. // only one category allowed
  24. category: Option<String>,
  25. // optional date if we want to order pages (ie block)
  26. date: Option<bool>,
  27. // optional layout, if we want to specify which html to render for that page
  28. layout: Option<String>,
  29. // description that appears when linked, e.g. on twitter
  30. description: Option<String>,
  31. }
  32. impl Page {
  33. // Parse a page given the content of the .md file
  34. // Files without front matter or with invalid front matter are considered
  35. // erroneous
  36. pub fn from_str(filename: &str, content: &str) -> Result<()> {
  37. // 1. separate front matter from content
  38. if !DELIM_RE.is_match(content) {
  39. return Err(ErrorKind::InvalidFrontMatter(filename.to_string()).into());
  40. }
  41. // 2. extract the front matter and the content
  42. let splits: Vec<&str> = DELIM_RE.splitn(content, 2).collect();
  43. let front_matter = splits[0];
  44. let content = splits[1];
  45. // 2. parse front matter
  46. let mut parser = Parser::new(&front_matter);
  47. if let Some(value) = parser.parse() {
  48. } else {
  49. // TODO: handle error in parsing TOML
  50. println!("parse errors: {:?}", parser.errors);
  51. }
  52. Ok(())
  53. }
  54. }
  55. #[cfg(test)]
  56. mod tests {
  57. use super::*;
  58. #[test]
  59. fn test_can_extract_front_matter() {
  60. }
  61. }