1234567891011121314151617181920212223242526272829303132333435363738 |
- use regex::Regex;
-
- use std::fs;
- use std::path;
-
- #[derive(Debug)]
- pub struct Page {
- pub title: String,
- pub body: String,
- pub slug: String,
- }
-
- pub fn read_pages_dir(cwd: &path::PathBuf) -> Vec<fs::DirEntry> {
- match fs::read_dir(cwd) {
- Ok(pages) => pages.into_iter().map(|page| page.unwrap()).collect(),
- Err(err) => panic!(err),
- }
- }
-
- pub fn parse_page(path: path::PathBuf) -> Page {
- let contents = fs::read_to_string(&path).expect("Couldn't read page file");
-
- lazy_static! {
- static ref re: Regex = Regex::new(r"^# (?P<title>.*)\n\n(?s)(?P<body>.*)").unwrap();
- static ref slug_re: Regex = Regex::new(r"(?P<slug>\S+).md").unwrap();
- }
-
- let title = &re.captures(&contents).expect("Couldn't parse title")["title"];
- let body = &re.captures(&contents).expect("Couldn't parse body")["body"];
-
- let filename = &path.file_name().unwrap().to_str().unwrap();
- let slug = &slug_re.captures(filename).expect("Couldn't parse slug")["slug"];
- Page {
- title: String::from(title),
- body: String::from(body),
- slug: String::from(slug),
- }
- }
|