A static site generator written in Rust
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

main.rs 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. extern crate clap;
  2. extern crate comrak;
  3. #[macro_use]
  4. extern crate lazy_static;
  5. extern crate regex;
  6. extern crate uuid;
  7. use std::{env, fs};
  8. use clap::{App, Arg};
  9. use post::{parse_post, read_posts_dir};
  10. use write::{write_post, write_post_listing};
  11. mod post;
  12. mod render;
  13. mod write;
  14. fn build() {
  15. let cwd = env::current_dir().expect("Couldn't read current directory");
  16. match fs::read_dir(cwd.join("public")) {
  17. Ok(_) => {
  18. fs::remove_dir_all(cwd.join("public")).unwrap();
  19. }
  20. Err(_) => {}
  21. }
  22. fs::create_dir(cwd.join("public")).expect("Couldn't create public directory");
  23. let layout_template = fs::read_to_string(&cwd.join("templates").join("layout.html"))
  24. .expect("Couldn't find layout template");
  25. let post_template = fs::read_to_string(cwd.join("templates").join("post.html"))
  26. .expect("Couldn't find post template");
  27. let post_listing_template = fs::read_to_string(cwd.join("templates").join("post_listing.html"))
  28. .expect("Couldn't find post listing item template");
  29. let post_item_template =
  30. fs::read_to_string(cwd.join("templates").join("post_listing_item.html"))
  31. .expect("Couldn't find post listing item template");
  32. let post_paths = read_posts_dir(&cwd.join("posts"));
  33. let posts = post_paths
  34. .into_iter()
  35. .map(|path| match path {
  36. Ok(p) => {
  37. let post = parse_post(p.path());
  38. write_post(&cwd, &layout_template, &post_template, &post);
  39. post
  40. }
  41. Err(err) => panic!(err),
  42. }).collect();
  43. write_post_listing(
  44. &cwd,
  45. &layout_template,
  46. &post_listing_template,
  47. &post_item_template,
  48. &posts,
  49. );
  50. }
  51. fn new(name: &str) {
  52. let cwd = env::current_dir().expect("Couldn't read current directory");
  53. let project_path = cwd.join(name);
  54. fs::create_dir(&project_path).expect(&format!("Couldn't create directory '{}'", &name));
  55. for dir in &["posts", "public", "templates"] {
  56. fs::create_dir(&project_path.join(&dir))
  57. .expect(&format!("Couldn't create {} directory", &dir));
  58. }
  59. for file in &["layout", "post_listing", "post", "post_listing_item"] {
  60. fs::write(
  61. &project_path
  62. .join("templates")
  63. .join(format!("{}.html", file)),
  64. "",
  65. ).expect(&format!("Couldn't write templates/{}.html", file));
  66. }
  67. }
  68. fn main() {
  69. let matches = App::new("tlon")
  70. .version("0.1.0")
  71. .author("Dylan Baker")
  72. .about("Highly opinionated static site generator")
  73. .arg(
  74. Arg::with_name("command")
  75. .required(true)
  76. .possible_values(&["build", "new"])
  77. .index(1),
  78. ).arg(
  79. Arg::with_name("name")
  80. .required_if("command", "new")
  81. .index(2),
  82. ).get_matches();
  83. let command = matches.value_of("command").unwrap();
  84. if command == "build" {
  85. build();
  86. } else if command == "new" {
  87. new(&matches.value_of("name").unwrap());
  88. }
  89. }
  90. #[cfg(test)]
  91. mod tests {
  92. #[allow(unused_imports)]
  93. use super::*;
  94. #[allow(unused_imports)]
  95. use uuid::Uuid;
  96. #[test]
  97. fn test_new() {
  98. let temp_dir = env::temp_dir();
  99. env::set_current_dir(&temp_dir).unwrap();
  100. let uuid = Uuid::new_v4().to_string();
  101. let project_dir = temp_dir.join(&uuid);
  102. new(&uuid);
  103. for dir in &["public", "posts", "templates"] {
  104. fs::read_dir(&project_dir.join(dir)).unwrap();
  105. }
  106. assert_eq!(
  107. "",
  108. fs::read_to_string(&project_dir.join("templates").join("post_listing.html")).unwrap()
  109. );
  110. assert_eq!(
  111. "",
  112. fs::read_to_string(&project_dir.join("templates").join("layout.html")).unwrap()
  113. );
  114. assert_eq!(
  115. "",
  116. fs::read_to_string(&project_dir.join("templates").join("post_listing_item.html"))
  117. .unwrap()
  118. );
  119. assert_eq!(
  120. "",
  121. fs::read_to_string(&project_dir.join("templates").join("post.html")).unwrap()
  122. );
  123. fs::remove_dir_all(project_dir).unwrap();
  124. }
  125. }