|
@@ -50,7 +50,7 @@ pub fn read_entry_dir(cwd: &path::Path) -> Vec<fs::DirEntry> {
|
50
|
50
|
}
|
51
|
51
|
}
|
52
|
52
|
|
53
|
|
-pub fn parse_entry(contents: &str, path: &path::Path) -> Entry {
|
|
53
|
+pub fn parse_entry(kind: EntryKind, contents: &str, path: &path::Path) -> Entry {
|
54
|
54
|
lazy_static! {
|
55
|
55
|
static ref re_with_date: Regex =
|
56
|
56
|
Regex::new(r"^# (?P<title>.*) \| (?P<date>\d{4}-\d{2}-\d{2})\n\n(?s)(?P<body>.*)")
|
|
@@ -61,40 +61,38 @@ pub fn parse_entry(contents: &str, path: &path::Path) -> Entry {
|
61
|
61
|
}
|
62
|
62
|
|
63
|
63
|
let filename = &path.file_name().unwrap().to_str().unwrap();
|
64
|
|
- let slug = &slug_re
|
|
64
|
+ let slug = slug_re
|
65
|
65
|
.captures(filename)
|
66
|
|
- .expect("Couldn't parse slug from filename")["slug"];
|
|
66
|
+ .expect("Couldn't parse slug from filename")["slug"]
|
|
67
|
+ .to_string();
|
67
|
68
|
|
68
|
|
- if let Some(date_string) = &re_with_date.captures(&contents) {
|
69
|
|
- let title = &re_with_date
|
|
69
|
+ if kind == EntryKind::Post {
|
|
70
|
+ let captures = &re_with_date
|
70
|
71
|
.captures(&contents)
|
71
|
|
- .expect("Couldn't parse title")["title"];
|
72
|
|
- let body = &re_with_date
|
73
|
|
- .captures(&contents)
|
74
|
|
- .expect("Couldn't parse title")["body"];
|
|
72
|
+ .expect("Couldn't parse post");
|
|
73
|
+ let title = captures["title"].to_string();
|
|
74
|
+ let body = captures["body"].to_string();
|
75
|
75
|
let date = Some(
|
76
|
|
- NaiveDate::parse_from_str(&date_string["date"], "%Y-%m-%d")
|
77
|
|
- .expect("Couldn't parse date"),
|
|
76
|
+ NaiveDate::parse_from_str(&captures["date"], "%Y-%m-%d").expect("Couldn't parse date"),
|
78
|
77
|
);
|
79
|
78
|
Entry {
|
80
|
|
- kind: EntryKind::Post,
|
81
|
|
- title: String::from(title),
|
82
|
|
- body: String::from(body),
|
83
|
|
- slug: String::from(slug),
|
84
|
|
- date: date,
|
|
79
|
+ kind,
|
|
80
|
+ title,
|
|
81
|
+ body,
|
|
82
|
+ slug,
|
|
83
|
+ date,
|
85
|
84
|
}
|
86
|
85
|
} else {
|
87
|
|
- let title = &re_without_date
|
88
|
|
- .captures(&contents)
|
89
|
|
- .expect("Couldn't parse title")["title"];
|
90
|
|
- let body = &re_without_date
|
|
86
|
+ let captures = &re_without_date
|
91
|
87
|
.captures(&contents)
|
92
|
|
- .expect("Couldn't parse title")["body"];
|
|
88
|
+ .expect("Couldn't parse page");
|
|
89
|
+ let title = captures["title"].to_string();
|
|
90
|
+ let body = captures["body"].to_string();
|
93
|
91
|
Entry {
|
94
|
|
- kind: EntryKind::Page,
|
95
|
|
- title: String::from(title),
|
96
|
|
- body: String::from(body),
|
97
|
|
- slug: String::from(slug),
|
|
92
|
+ kind,
|
|
93
|
+ title,
|
|
94
|
+ body,
|
|
95
|
+ slug,
|
98
|
96
|
date: None,
|
99
|
97
|
}
|
100
|
98
|
}
|
|
@@ -358,6 +356,7 @@ mod tests {
|
358
|
356
|
#[test]
|
359
|
357
|
fn test_parse_post_entry() {
|
360
|
358
|
let post = parse_entry(
|
|
359
|
+ EntryKind::Post,
|
361
|
360
|
"# Test Title | 2000-01-01\n\nThis is the body",
|
362
|
361
|
path::PathBuf::from("posts/one.md").as_path(),
|
363
|
362
|
);
|
|
@@ -372,6 +371,7 @@ mod tests {
|
372
|
371
|
#[test]
|
373
|
372
|
fn test_parse_page_entry() {
|
374
|
373
|
let post = parse_entry(
|
|
374
|
+ EntryKind::Page,
|
375
|
375
|
"# Test Title\n\nThis is the body",
|
376
|
376
|
path::PathBuf::from("pages/one.md").as_path(),
|
377
|
377
|
);
|