|
@@ -43,16 +43,32 @@ fn read_posts_dir(cwd: &path::PathBuf) -> fs::ReadDir {
|
43
|
43
|
}
|
44
|
44
|
}
|
45
|
45
|
|
|
46
|
+fn render_post_listing(cwd: &path::PathBuf, layout: &str, posts: &Vec<Post>) {
|
|
47
|
+ let post_listing = posts
|
|
48
|
+ .iter()
|
|
49
|
+ .map(|ref post| format!("<li><a href='/{}/'>{}</a></li>", post.slug, post.title))
|
|
50
|
+ .collect::<Vec<String>>()
|
|
51
|
+ .join("\n");
|
|
52
|
+
|
|
53
|
+ let output = layout.replace("{{ contents }}", &post_listing);
|
|
54
|
+ fs::write(cwd.join("public").join("index.html"), &output).expect("Unable to write file");
|
|
55
|
+}
|
|
56
|
+
|
46
|
57
|
fn main() {
|
47
|
58
|
let cwd = env::current_dir()
|
48
|
59
|
.expect("Couldn't read current directory");
|
|
60
|
+ let layout = fs::read_to_string(cwd.join("templates").join("layout.html")).expect("Couldn't find layout template");
|
49
|
61
|
let post_paths = read_posts_dir(&cwd.join("posts"));
|
|
62
|
+ let mut posts: Vec<Post> = vec![];
|
50
|
63
|
for path in post_paths {
|
51
|
64
|
match path {
|
52
|
65
|
Ok(p) => {
|
53
|
66
|
let post = parse_post(p.path());
|
54
|
|
- },
|
|
67
|
+ posts.push(post);
|
|
68
|
+ }
|
55
|
69
|
Err(err) => panic!(err),
|
56
|
70
|
}
|
57
|
71
|
}
|
|
72
|
+
|
|
73
|
+ render_post_listing(&cwd, &layout, &posts);
|
58
|
74
|
}
|