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.

main.rs 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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::create_dir(cwd.join("public")) {
  17. Ok(_) => {}
  18. Err(err) => match err.kind() {
  19. std::io::ErrorKind::AlreadyExists => {}
  20. _ => panic!(err),
  21. },
  22. }
  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. mod tests {
  91. #[allow(unused_imports)]
  92. use super::*;
  93. #[allow(unused_imports)]
  94. use uuid::Uuid;
  95. #[test]
  96. fn test_new() {
  97. let temp_dir = env::temp_dir();
  98. env::set_current_dir(&temp_dir).unwrap();
  99. let uuid = Uuid::new_v4().to_string();
  100. let project_dir = temp_dir.join(&uuid);
  101. new(&uuid);
  102. for dir in &["public", "posts", "templates"] {
  103. fs::read_dir(&project_dir.join(dir)).unwrap();
  104. }
  105. assert_eq!(
  106. "",
  107. fs::read_to_string(&project_dir.join("templates").join("post_listing.html")).unwrap()
  108. );
  109. assert_eq!(
  110. "",
  111. fs::read_to_string(&project_dir.join("templates").join("layout.html")).unwrap()
  112. );
  113. assert_eq!(
  114. "",
  115. fs::read_to_string(&project_dir.join("templates").join("post_listing_item.html"))
  116. .unwrap()
  117. );
  118. assert_eq!(
  119. "",
  120. fs::read_to_string(&project_dir.join("templates").join("post.html")).unwrap()
  121. );
  122. fs::remove_dir_all(project_dir).unwrap();
  123. }
  124. }