|
@@ -1,9 +1,16 @@
|
|
1
|
+use config::Config;
|
1
|
2
|
use post::Post;
|
2
|
3
|
use render::{render_post, render_post_listing};
|
3
|
4
|
use std::fs;
|
4
|
5
|
use std::path;
|
5
|
6
|
|
6
|
|
-pub fn write_post(cwd: &path::PathBuf, layout: &str, post_template: &str, post: &Post) {
|
|
7
|
+pub fn write_post(
|
|
8
|
+ cwd: &path::PathBuf,
|
|
9
|
+ layout: &str,
|
|
10
|
+ post_template: &str,
|
|
11
|
+ post: &Post,
|
|
12
|
+ config: &Config,
|
|
13
|
+) {
|
7
|
14
|
match fs::create_dir(cwd.join("public").join(&post.slug)) {
|
8
|
15
|
Ok(_) => {}
|
9
|
16
|
Err(err) => match err.kind() {
|
|
@@ -13,7 +20,7 @@ pub fn write_post(cwd: &path::PathBuf, layout: &str, post_template: &str, post:
|
13
|
20
|
}
|
14
|
21
|
fs::write(
|
15
|
22
|
cwd.join("public").join(&post.slug).join("index.html"),
|
16
|
|
- render_post(layout, post_template, post),
|
|
23
|
+ render_post(layout, post_template, post, config),
|
17
|
24
|
).expect("Unable to write file");
|
18
|
25
|
}
|
19
|
26
|
|
|
@@ -23,10 +30,17 @@ pub fn write_post_listing(
|
23
|
30
|
post_listing_template: &str,
|
24
|
31
|
post_item_template: &str,
|
25
|
32
|
posts: &Vec<Post>,
|
|
33
|
+ config: &Config,
|
26
|
34
|
) {
|
27
|
35
|
fs::write(
|
28
|
36
|
cwd.join("public").join("index.html"),
|
29
|
|
- render_post_listing(layout, post_listing_template, post_item_template, posts),
|
|
37
|
+ render_post_listing(
|
|
38
|
+ layout,
|
|
39
|
+ post_listing_template,
|
|
40
|
+ post_item_template,
|
|
41
|
+ posts,
|
|
42
|
+ config,
|
|
43
|
+ ),
|
30
|
44
|
).expect("Unable to write file");
|
31
|
45
|
}
|
32
|
46
|
|
|
@@ -49,23 +63,25 @@ mod tests {
|
49
|
63
|
let cwd = env::current_dir().unwrap();
|
50
|
64
|
fs::create_dir(cwd.join("public")).unwrap();
|
51
|
65
|
|
52
|
|
- let layout = "<html><head><title>Test</title></head><body>{{ contents }}</body></html>";
|
|
66
|
+ let layout =
|
|
67
|
+ "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>";
|
53
|
68
|
let post_template = "<article><h1>{{ title }}</h1><div>{{ body }}</div></article>";
|
54
|
69
|
let post = Post {
|
55
|
70
|
title: String::from("Hello world"),
|
56
|
71
|
body: String::from("Lorem ipsum dolor sit amet"),
|
57
|
72
|
slug: String::from("hello-world"),
|
58
|
73
|
};
|
|
74
|
+ let config = Config {
|
|
75
|
+ site_name: "Test Site".to_string(),
|
|
76
|
+ };
|
59
|
77
|
|
60
|
|
- write_post(&cwd, &layout, &post_template, &post);
|
|
78
|
+ write_post(&cwd, &layout, &post_template, &post, &config);
|
61
|
79
|
|
62
|
80
|
let content =
|
63
|
81
|
fs::read_to_string(cwd.join("public").join("hello-world").join("index.html")).unwrap();
|
64
|
82
|
|
65
|
83
|
assert_eq!(
|
66
|
|
- "<html><head><title>Test</title></head><body><article><h1>Hello wor\
|
67
|
|
- ld</h1><div><p>Lorem ipsum dolor sit amet</p></div></article></body\
|
68
|
|
- ></html>",
|
|
84
|
+ "<html><head><title>Hello world | Test Site</title></head><body><article><h1>Hello world</h1><div><p>Lorem ipsum dolor sit amet</p></div></article></body></html>",
|
69
|
85
|
content.replace("\n", "")
|
70
|
86
|
);
|
71
|
87
|
|
|
@@ -82,7 +98,8 @@ mod tests {
|
82
|
98
|
let cwd = env::current_dir().unwrap();
|
83
|
99
|
fs::create_dir(cwd.join("public")).unwrap();
|
84
|
100
|
|
85
|
|
- let layout = "<html><head><title>Test</title></head><body>{{ contents }}</body></html>";
|
|
101
|
+ let layout =
|
|
102
|
+ "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>";
|
86
|
103
|
let post_listing_template = "<ul>{{ post_listing }}</ul>";
|
87
|
104
|
let post_item_template = "<li><a href=\"/{{ slug }}\">{{ title }}</a></li>";
|
88
|
105
|
let posts = vec![
|
|
@@ -102,19 +119,20 @@ mod tests {
|
102
|
119
|
slug: String::from("third-post"),
|
103
|
120
|
},
|
104
|
121
|
];
|
|
122
|
+ let config = Config {
|
|
123
|
+ site_name: "Test Site".to_string(),
|
|
124
|
+ };
|
105
|
125
|
write_post_listing(
|
106
|
126
|
&cwd,
|
107
|
127
|
&layout,
|
108
|
128
|
&post_listing_template,
|
109
|
129
|
&post_item_template,
|
110
|
130
|
&posts,
|
|
131
|
+ &config,
|
111
|
132
|
);
|
112
|
133
|
|
113
|
134
|
assert_eq!(
|
114
|
|
- "<html><head><title>Test</title></head><body><ul><li><a href=\"/fir\
|
115
|
|
- st-post\">First post</a></li><li><a href=\"/second-post\">Second po\
|
116
|
|
- st</a></li><li><a href=\"/third-post\">Third post</a></li></ul></bo\
|
117
|
|
- dy></html>",
|
|
135
|
+ "<html><head><title>Test Site</title></head><body><ul><li><a href=\"/first-post\">First post</a></li><li><a href=\"/second-post\">Second post</a></li><li><a href=\"/third-post\">Third post</a></li></ul></body></html>",
|
118
|
136
|
fs::read_to_string(&cwd.join("public").join("index.html"))
|
119
|
137
|
.unwrap()
|
120
|
138
|
.replace("\n", ""),
|