A static site generator written in Rust
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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