A static site generator written in Rust
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

write.rs 4.5KB

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