|
@@ -43,6 +43,28 @@ fn read_posts_dir(cwd: &path::PathBuf) -> fs::ReadDir {
|
43
|
43
|
}
|
44
|
44
|
}
|
45
|
45
|
|
|
46
|
+fn render_post(cwd: &path::PathBuf, layout: &str, post: &Post) {
|
|
47
|
+ let post_template = fs::read_to_string(cwd.join("templates").join("post.html"))
|
|
48
|
+ .expect("Couldn't find post template");
|
|
49
|
+ let output = layout.replace(
|
|
50
|
+ "{{ contents }}",
|
|
51
|
+ &post_template
|
|
52
|
+ .replace("{{ title }}", &post.title)
|
|
53
|
+ .replace("{{ body }}", &post.body),
|
|
54
|
+ );
|
|
55
|
+ match fs::create_dir(cwd.join("public").join(&post.slug)) {
|
|
56
|
+ Ok(_) => {}
|
|
57
|
+ Err(err) => match err.kind() {
|
|
58
|
+ std::io::ErrorKind::AlreadyExists => {}
|
|
59
|
+ _ => panic!(err),
|
|
60
|
+ },
|
|
61
|
+ }
|
|
62
|
+ fs::write(
|
|
63
|
+ cwd.join("public").join(&post.slug).join("index.html"),
|
|
64
|
+ &output,
|
|
65
|
+ ).expect("Unable to write file");
|
|
66
|
+}
|
|
67
|
+
|
46
|
68
|
fn render_post_listing(cwd: &path::PathBuf, layout: &str, posts: &Vec<Post>) {
|
47
|
69
|
let post_listing = posts
|
48
|
70
|
.iter()
|
|
@@ -64,6 +86,7 @@ fn main() {
|
64
|
86
|
match path {
|
65
|
87
|
Ok(p) => {
|
66
|
88
|
let post = parse_post(p.path());
|
|
89
|
+ render_post(&cwd, &layout, &post);
|
67
|
90
|
posts.push(post);
|
68
|
91
|
}
|
69
|
92
|
Err(err) => panic!(err),
|