|
@@ -44,13 +44,16 @@ fn read_posts_dir(cwd: &path::PathBuf) -> fs::ReadDir {
|
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
|
49
|
"{{ contents }}",
|
50
|
50
|
&post_template
|
51
|
51
|
.replace("{{ title }}", &post.title)
|
52
|
52
|
.replace("{{ body }}", &post.body),
|
53
|
|
- );
|
|
53
|
+ )
|
|
54
|
+}
|
|
55
|
+
|
|
56
|
+fn render_post(cwd: &path::PathBuf, layout: &str, post_template: &str, post: &Post) {
|
54
|
57
|
match fs::create_dir(cwd.join("public").join(&post.slug)) {
|
55
|
58
|
Ok(_) => {}
|
56
|
59
|
Err(err) => match err.kind() {
|
|
@@ -60,32 +63,43 @@ fn render_post(cwd: &path::PathBuf, layout: &str, post_template: &str, post: &Po
|
60
|
63
|
}
|
61
|
64
|
fs::write(
|
62
|
65
|
cwd.join("public").join(&post.slug).join("index.html"),
|
63
|
|
- &output,
|
|
66
|
+ generate_post(layout, post_template, post),
|
64
|
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
|
92
|
fn render_post_listing(
|
68
|
93
|
cwd: &path::PathBuf,
|
69
|
94
|
layout: &str,
|
|
95
|
+ post_listing_template: &str,
|
70
|
96
|
post_item_template: &str,
|
71
|
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
|
105
|
fn build() {
|
|
@@ -103,6 +117,8 @@ fn build() {
|
103
|
117
|
.expect("Couldn't find layout template");
|
104
|
118
|
let post_template = fs::read_to_string(cwd.join("templates").join("post.html"))
|
105
|
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
|
122
|
let post_item_template =
|
107
|
123
|
fs::read_to_string(cwd.join("templates").join("post_listing_item.html"))
|
108
|
124
|
.expect("Couldn't find post listing item template");
|
|
@@ -119,7 +135,13 @@ fn build() {
|
119
|
135
|
Err(err) => panic!(err),
|
120
|
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
|
147
|
fn new(name: &str) {
|
|
@@ -133,7 +155,7 @@ fn new(name: &str) {
|
133
|
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
|
159
|
fs::write(
|
138
|
160
|
&project_path
|
139
|
161
|
.join("templates")
|