A static site generator written in Rust
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.

post.rs 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // use chrono::NaiveDate;
  2. // #[derive(Debug)]
  3. // pub struct Post {
  4. // pub title: String,
  5. // pub body: String,
  6. // pub slug: String,
  7. // pub date: NaiveDate,
  8. // }
  9. // impl Post {
  10. // fn date(&self) -> String {
  11. // self.date.format("%Y-%m-%d").to_string()
  12. // }
  13. // fn render(&self, template: &str) -> String {
  14. // template
  15. // .replace("{{ title }}", &self.title)
  16. // .replace("{{ slug }}", &self.slug)
  17. // .replace("{{ body }}", &self.body)
  18. // .replace("{{ date }}", &self.date())
  19. // }
  20. // }
  21. // #[cfg(test)]
  22. // mod tests {
  23. // #[allow(unused_imports)]
  24. // use super::*;
  25. // #[allow(unused_imports)]
  26. // #[allow(unused_imports)]
  27. // use std::{env, fs, path};
  28. // #[allow(unused_imports)]
  29. // use uuid::Uuid;
  30. // #[test]
  31. // fn test_read_posts_dir() {
  32. // let temp_dir = env::temp_dir();
  33. // let working_dir = temp_dir.join(&Uuid::new_v4().to_string());
  34. // fs::create_dir(&working_dir).unwrap();
  35. // env::set_current_dir(&working_dir).unwrap();
  36. // let cwd = env::current_dir().unwrap();
  37. // fs::create_dir(cwd.join("posts")).unwrap();
  38. // let post_body = "# This is a post\n\nHere is some content that goes in the post";
  39. // let mut uuids: Vec<String> = vec![];
  40. // for _ in 1..11 {
  41. // let uuid = String::from(Uuid::new_v4().to_string());
  42. // uuids.push(uuid.clone());
  43. // fs::write(
  44. // cwd.join("posts").join(format!("{}.md", &uuid)),
  45. // &String::from(post_body),
  46. // )
  47. // .unwrap();
  48. // }
  49. // let mut expected_paths: Vec<String> = uuids
  50. // .into_iter()
  51. // .map(|uuid| {
  52. // String::from(
  53. // cwd.join("posts")
  54. // .join(format!("{}.md", uuid))
  55. // .to_str()
  56. // .unwrap(),
  57. // )
  58. // })
  59. // .collect();
  60. // expected_paths.sort();
  61. // let mut actual_paths: Vec<String> = read_posts_dir(&cwd.join("posts"))
  62. // .into_iter()
  63. // .map(|dir_entry| String::from(dir_entry.path().to_str().unwrap()))
  64. // .collect();
  65. // actual_paths.sort();
  66. // assert_eq!(expected_paths, actual_paths);
  67. // fs::remove_dir_all(temp_dir.join(&working_dir)).unwrap();
  68. // }
  69. // #[test]
  70. // fn test_parse_post() {
  71. // let temp_dir = env::temp_dir();
  72. // let working_dir = temp_dir.join(&Uuid::new_v4().to_string());
  73. // fs::create_dir(&working_dir).unwrap();
  74. // env::set_current_dir(&working_dir).unwrap();
  75. // let cwd = env::current_dir().unwrap();
  76. // fs::create_dir(cwd.join("posts")).unwrap();
  77. // let slug = Uuid::new_v4().to_string();
  78. // let filetitle = format!("{}.md", slug);
  79. // fs::write(
  80. // cwd.join("posts").join(&filetitle),
  81. // "# This is a post | 2019-01-01\n\nHere is some content that goes in the post",
  82. // )
  83. // .unwrap();
  84. // let post = parse_post(cwd.join("posts").join(&filetitle));
  85. // let date = NaiveDate::from_ymd(2019, 1, 1);
  86. // assert_eq!("This is a post", post.title);
  87. // assert_eq!("Here is some content that goes in the post", post.body);
  88. // assert_eq!(slug, post.slug);
  89. // assert_eq!(date, post.date);
  90. // fs::remove_dir_all(temp_dir.join(&working_dir)).unwrap();
  91. // }
  92. // #[test]
  93. // fn test_post_with_multiple_paragraphs() {
  94. // let temp_dir = env::temp_dir();
  95. // let working_dir = temp_dir.join(&Uuid::new_v4().to_string());
  96. // fs::create_dir(&working_dir).unwrap();
  97. // env::set_current_dir(&working_dir).unwrap();
  98. // let cwd = env::current_dir().unwrap();
  99. // fs::create_dir(cwd.join("posts")).unwrap();
  100. // let slug = Uuid::new_v4().to_string();
  101. // let filetitle = format!("{}.md", slug);
  102. // fs::write(
  103. // cwd.join("posts").join(&filetitle),
  104. // "# This is a post | 2019-01-01\n\nHere is a line\n\nHere is another line\n\nAnd a third",
  105. // )
  106. // .unwrap();
  107. // let post = parse_post(cwd.join("posts").join(&filetitle));
  108. // let date = NaiveDate::from_ymd(2019, 1, 1);
  109. // assert_eq!("This is a post", post.title);
  110. // assert_eq!(
  111. // "Here is a line\n\nHere is another line\n\nAnd a third",
  112. // post.body
  113. // );
  114. // assert_eq!(slug, post.slug);
  115. // assert_eq!(date, post.date);
  116. // fs::remove_dir_all(temp_dir.join(&working_dir)).unwrap();
  117. // }
  118. // }