A static site generator written in Rust
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

main.rs 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #[macro_use]
  2. extern crate lazy_static;
  3. extern crate regex;
  4. use std::env;
  5. use std::fs;
  6. use std::path;
  7. use regex::Regex;
  8. #[derive(Debug)]
  9. struct Post {
  10. title: String,
  11. body: String,
  12. slug: String,
  13. }
  14. fn parse_post(path: path::PathBuf) -> Post {
  15. let contents = fs::read_to_string(&path).expect("Couldn't read post file");
  16. lazy_static! {
  17. static ref re: Regex = Regex::new(r"^# (?P<title>.*)\n\n(?P<body>.*)").unwrap();
  18. static ref slug_re: Regex = Regex::new(r"(?P<slug>\S+).md").unwrap();
  19. }
  20. let title = &re.captures(&contents).expect("Couldn't parse title")["title"];
  21. let body = &re.captures(&contents).expect("Couldn't parse body")["body"];
  22. let filename = &path.file_name().unwrap().to_str().unwrap();
  23. let slug = &slug_re.captures(filename).expect("Couldn't parse slug")["slug"];
  24. Post {
  25. title: String::from(title),
  26. body: String::from(body),
  27. slug: String::from(slug),
  28. }
  29. }
  30. fn read_posts_dir() -> fs::ReadDir {
  31. let cwd = env::current_dir()
  32. .expect("Couldn't read current directory")
  33. .join("posts");
  34. match fs::read_dir(cwd) {
  35. Ok(posts) => posts,
  36. Err(err) => panic!(err),
  37. }
  38. }
  39. fn main() {
  40. let post_paths = read_posts_dir();
  41. for path in post_paths {
  42. match path {
  43. Ok(p) => {
  44. let post = parse_post(p.path());
  45. },
  46. Err(err) => panic!(err),
  47. }
  48. }
  49. }