A static site generator written in Rust
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

write.rs 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. use config::Config;
  2. use post::Post;
  3. use render::{render_post, render_post_listing};
  4. use std::fs;
  5. use std::path;
  6. pub fn write_post(
  7. cwd: &path::PathBuf,
  8. layout: &str,
  9. post_template: &str,
  10. post: &Post,
  11. config: &Config,
  12. ) {
  13. match fs::create_dir(cwd.join("public").join(&post.slug)) {
  14. Ok(_) => {}
  15. Err(err) => match err.kind() {
  16. std::io::ErrorKind::AlreadyExists => {}
  17. _ => panic!(err),
  18. },
  19. }
  20. fs::write(
  21. cwd.join("public").join(&post.slug).join("index.html"),
  22. render_post(layout, post_template, post, config),
  23. )
  24. .expect("Unable to write file");
  25. }
  26. pub fn write_post_listing(
  27. cwd: &path::PathBuf,
  28. layout: &str,
  29. post_listing_template: &str,
  30. post_item_template: &str,
  31. posts: &Vec<Post>,
  32. config: &Config,
  33. ) {
  34. fs::write(
  35. cwd.join("public").join("index.html"),
  36. render_post_listing(
  37. layout,
  38. post_listing_template,
  39. post_item_template,
  40. posts,
  41. config,
  42. ),
  43. )
  44. .expect("Unable to write file");
  45. }
  46. #[cfg(test)]
  47. mod tests {
  48. #[allow(unused_imports)]
  49. use super::*;
  50. #[allow(unused_imports)]
  51. use std::{env, fs};
  52. #[allow(unused_imports)]
  53. use uuid::Uuid;
  54. #[test]
  55. fn test_write_post() {
  56. let temp_dir = env::temp_dir();
  57. let working_dir = temp_dir.join(&Uuid::new_v4().to_string());
  58. fs::create_dir(&working_dir).unwrap();
  59. env::set_current_dir(&working_dir).unwrap();
  60. let cwd = env::current_dir().unwrap();
  61. fs::create_dir(cwd.join("public")).unwrap();
  62. let layout =
  63. "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>";
  64. let post_template = "<article><h1>{{ title }}</h1><div>{{ body }}</div></article>";
  65. let post = Post {
  66. title: String::from("Hello world"),
  67. body: String::from("Lorem ipsum dolor sit amet"),
  68. slug: String::from("hello-world"),
  69. };
  70. let config = Config {
  71. site_name: "Test Site".to_string(),
  72. };
  73. write_post(&cwd, &layout, &post_template, &post, &config);
  74. let content =
  75. fs::read_to_string(cwd.join("public").join("hello-world").join("index.html")).unwrap();
  76. assert_eq!(
  77. "<html><head><title>Hello world | Test Site</title></head><body><article><h1>Hello world</h1><div><p>Lorem ipsum dolor sit amet</p></div></article></body></html>",
  78. content.replace("\n", "")
  79. );
  80. fs::remove_dir_all(temp_dir.join(&working_dir)).unwrap();
  81. }
  82. #[test]
  83. fn test_write_post_listing() {
  84. let temp_dir = env::temp_dir();
  85. let working_dir = temp_dir.join(&Uuid::new_v4().to_string());
  86. fs::create_dir(&working_dir).unwrap();
  87. env::set_current_dir(&working_dir).unwrap();
  88. let cwd = env::current_dir().unwrap();
  89. fs::create_dir(cwd.join("public")).unwrap();
  90. let layout =
  91. "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>";
  92. let post_listing_template = "<ul>{{ post_listing }}</ul>";
  93. let post_item_template = "<li><a href=\"/{{ slug }}\">{{ title }}</a></li>";
  94. let posts = vec![
  95. Post {
  96. title: String::from("First post"),
  97. body: String::from("lorem ipsum dolor sit amet"),
  98. slug: String::from("first-post"),
  99. },
  100. Post {
  101. title: String::from("Second post"),
  102. body: String::from("lorem ipsum dolor sit amet"),
  103. slug: String::from("second-post"),
  104. },
  105. Post {
  106. title: String::from("Third post"),
  107. body: String::from("lorem ipsum dolor sit amet"),
  108. slug: String::from("third-post"),
  109. },
  110. ];
  111. let config = Config {
  112. site_name: "Test Site".to_string(),
  113. };
  114. write_post_listing(
  115. &cwd,
  116. &layout,
  117. &post_listing_template,
  118. &post_item_template,
  119. &posts,
  120. &config,
  121. );
  122. assert_eq!(
  123. "<html><head><title>Test Site</title></head><body><ul><li><a href=\"/first-post\">First post</a></li><li><a href=\"/second-post\">Second post</a></li><li><a href=\"/third-post\">Third post</a></li></ul></body></html>",
  124. fs::read_to_string(&cwd.join("public").join("index.html"))
  125. .unwrap()
  126. .replace("\n", ""),
  127. );
  128. fs::remove_dir_all(temp_dir.join(&working_dir)).unwrap();
  129. }
  130. }