浏览代码

Separate rendering templates and writing to disk

master
Dylan Baker 6 年前
父节点
当前提交
a575de5882
共有 1 个文件被更改,包括 44 次插入22 次删除
  1. 44
    22
      src/main.rs

+ 44
- 22
src/main.rs 查看文件

44
     }
44
     }
45
 }
45
 }
46
 
46
 
47
-fn render_post(cwd: &path::PathBuf, layout: &str, post_template: &str, post: &Post) {
48
-    let output = layout.replace(
47
+fn generate_post(layout: &str, post_template: &str, post: &Post) -> String {
48
+    layout.replace(
49
         "{{ contents }}",
49
         "{{ contents }}",
50
         &post_template
50
         &post_template
51
             .replace("{{ title }}", &post.title)
51
             .replace("{{ title }}", &post.title)
52
             .replace("{{ body }}", &post.body),
52
             .replace("{{ body }}", &post.body),
53
-    );
53
+    )
54
+}
55
+
56
+fn render_post(cwd: &path::PathBuf, layout: &str, post_template: &str, post: &Post) {
54
     match fs::create_dir(cwd.join("public").join(&post.slug)) {
57
     match fs::create_dir(cwd.join("public").join(&post.slug)) {
55
         Ok(_) => {}
58
         Ok(_) => {}
56
         Err(err) => match err.kind() {
59
         Err(err) => match err.kind() {
60
     }
63
     }
61
     fs::write(
64
     fs::write(
62
         cwd.join("public").join(&post.slug).join("index.html"),
65
         cwd.join("public").join(&post.slug).join("index.html"),
63
-        &output,
66
+        generate_post(layout, post_template, post),
64
     ).expect("Unable to write file");
67
     ).expect("Unable to write file");
65
 }
68
 }
66
 
69
 
70
+fn generate_post_listing(
71
+    layout: &str,
72
+    post_listing_template: &str,
73
+    post_item_template: &str,
74
+    posts: &Vec<Post>,
75
+) -> String {
76
+    layout.replace(
77
+        "{{ contents }}",
78
+        &post_listing_template.replace(
79
+            "{{ post_listing }}",
80
+            &posts
81
+                .iter()
82
+                .map(|ref post| {
83
+                    post_item_template
84
+                        .replace("{{ slug }}", &post.slug)
85
+                        .replace("{{ title }}", &post.title)
86
+                }).collect::<Vec<String>>()
87
+                .join("\n"),
88
+        ),
89
+    )
90
+}
91
+
67
 fn render_post_listing(
92
 fn render_post_listing(
68
     cwd: &path::PathBuf,
93
     cwd: &path::PathBuf,
69
     layout: &str,
94
     layout: &str,
95
+    post_listing_template: &str,
70
     post_item_template: &str,
96
     post_item_template: &str,
71
     posts: &Vec<Post>,
97
     posts: &Vec<Post>,
72
 ) {
98
 ) {
73
-    let post_listing_template = fs::read_to_string(cwd.join("templates").join("index.html"))
74
-        .expect("Couldn't read templates/index.html");
75
-    let post_listing = post_listing_template.replace(
76
-        "{{ post_listing }}",
77
-        &posts
78
-            .iter()
79
-            .map(|ref post| {
80
-                post_item_template
81
-                    .replace("{{ slug }}", &post.slug)
82
-                    .replace("{{ title }}", &post.title)
83
-            }).collect::<Vec<String>>()
84
-            .join("\n"),
85
-    );
86
-
87
-    let output = layout.replace("{{ contents }}", &post_listing);
88
-    fs::write(cwd.join("public").join("index.html"), &output).expect("Unable to write file");
99
+    fs::write(
100
+        cwd.join("public").join("index.html"),
101
+        generate_post_listing(layout, post_listing_template, post_item_template, posts),
102
+    ).expect("Unable to write file");
89
 }
103
 }
90
 
104
 
91
 fn build() {
105
 fn build() {
103
         .expect("Couldn't find layout template");
117
         .expect("Couldn't find layout template");
104
     let post_template = fs::read_to_string(cwd.join("templates").join("post.html"))
118
     let post_template = fs::read_to_string(cwd.join("templates").join("post.html"))
105
         .expect("Couldn't find post template");
119
         .expect("Couldn't find post template");
120
+    let post_listing_template = fs::read_to_string(cwd.join("templates").join("post_listing.html"))
121
+        .expect("Couldn't find post listing item template");
106
     let post_item_template =
122
     let post_item_template =
107
         fs::read_to_string(cwd.join("templates").join("post_listing_item.html"))
123
         fs::read_to_string(cwd.join("templates").join("post_listing_item.html"))
108
             .expect("Couldn't find post listing item template");
124
             .expect("Couldn't find post listing item template");
119
             Err(err) => panic!(err),
135
             Err(err) => panic!(err),
120
         }).collect();
136
         }).collect();
121
 
137
 
122
-    render_post_listing(&cwd, &layout_template, &post_item_template, &posts);
138
+    render_post_listing(
139
+        &cwd,
140
+        &layout_template,
141
+        &post_listing_template,
142
+        &post_item_template,
143
+        &posts,
144
+    );
123
 }
145
 }
124
 
146
 
125
 fn new(name: &str) {
147
 fn new(name: &str) {
133
             .expect(&format!("Couldn't create {} directory", &dir));
155
             .expect(&format!("Couldn't create {} directory", &dir));
134
     }
156
     }
135
 
157
 
136
-    for file in &["layout", "index", "post", "post_listing_item"] {
158
+    for file in &["layout", "post_listing", "post", "post_listing_item"] {
137
         fs::write(
159
         fs::write(
138
             &project_path
160
             &project_path
139
                 .join("templates")
161
                 .join("templates")

正在加载...
取消
保存