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.

page.rs 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. use regex::Regex;
  2. use std::fs;
  3. use std::path;
  4. #[derive(Debug)]
  5. pub struct Page {
  6. pub title: String,
  7. pub body: String,
  8. pub slug: String,
  9. }
  10. pub fn read_pages_dir(cwd: &path::PathBuf) -> Vec<fs::DirEntry> {
  11. match fs::read_dir(cwd) {
  12. Ok(pages) => pages.into_iter().map(|page| page.unwrap()).collect(),
  13. Err(err) => panic!(err),
  14. }
  15. }
  16. pub fn parse_page(path: path::PathBuf) -> Page {
  17. let contents = fs::read_to_string(&path).expect("Couldn't read page file");
  18. lazy_static! {
  19. static ref re: Regex = Regex::new(r"^# (?P<title>.*)\n\n(?s)(?P<body>.*)").unwrap();
  20. static ref slug_re: Regex = Regex::new(r"(?P<slug>\S+).md").unwrap();
  21. }
  22. let title = &re.captures(&contents).expect("Couldn't parse title")["title"];
  23. let body = &re.captures(&contents).expect("Couldn't parse body")["body"];
  24. let filename = &path.file_name().unwrap().to_str().unwrap();
  25. let slug = &slug_re.captures(filename).expect("Couldn't parse slug")["slug"];
  26. Page {
  27. title: String::from(title),
  28. body: String::from(body),
  29. slug: String::from(slug),
  30. }
  31. }