Browse Source

Implement render post

master
Dylan Baker 6 years ago
parent
commit
d0604fde70
1 changed files with 23 additions and 0 deletions
  1. 23
    0
      src/main.rs

+ 23
- 0
src/main.rs View File

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
 fn render_post_listing(cwd: &path::PathBuf, layout: &str, posts: &Vec<Post>) {
68
 fn render_post_listing(cwd: &path::PathBuf, layout: &str, posts: &Vec<Post>) {
47
     let post_listing = posts
69
     let post_listing = posts
48
         .iter()
70
         .iter()
64
         match path {
86
         match path {
65
             Ok(p) => {
87
             Ok(p) => {
66
                 let post = parse_post(p.path());
88
                 let post = parse_post(p.path());
89
+                render_post(&cwd, &layout, &post);
67
                 posts.push(post);
90
                 posts.push(post);
68
             }
91
             }
69
             Err(err) => panic!(err),
92
             Err(err) => panic!(err),

Loading…
Cancel
Save