|
@@ -40,11 +40,8 @@ pub fn build(include_drafts: bool, cwd: &path::Path) {
|
40
|
40
|
}
|
41
|
41
|
};
|
42
|
42
|
|
43
|
|
- match fs::read_dir(cwd.join("public")) {
|
44
|
|
- Ok(_) => {
|
45
|
|
- fs::remove_dir_all(cwd.join("public")).unwrap();
|
46
|
|
- }
|
47
|
|
- Err(_) => {}
|
|
43
|
+ if fs::read_dir(cwd.join("public")).is_ok() {
|
|
44
|
+ fs::remove_dir_all(cwd.join("public")).unwrap();
|
48
|
45
|
}
|
49
|
46
|
|
50
|
47
|
fs::create_dir(cwd.join("public")).expect("Couldn't create public directory");
|
|
@@ -85,18 +82,18 @@ pub fn build(include_drafts: bool, cwd: &path::Path) {
|
85
|
82
|
posts.sort_by(|a, b| b.date.cmp(&a.date));
|
86
|
83
|
|
87
|
84
|
for post in &posts {
|
88
|
|
- write_entry(&cwd, &layout_template, &post_template, &post, &config);
|
|
85
|
+ write_entry(cwd, &layout_template, &post_template, post, &config);
|
89
|
86
|
}
|
90
|
87
|
|
91
|
88
|
for entry in page_paths.into_iter() {
|
92
|
89
|
let path = entry.path();
|
93
|
90
|
let contents = fs::read_to_string(&path).expect("Couldn't read page file");
|
94
|
91
|
let page = parse_entry(EntryKind::Page, &contents, &path);
|
95
|
|
- write_entry(&cwd, &layout_template, &page_template, &page, &config);
|
|
92
|
+ write_entry(cwd, &layout_template, &page_template, &page, &config);
|
96
|
93
|
}
|
97
|
94
|
|
98
|
95
|
write_entry_listing(
|
99
|
|
- &cwd,
|
|
96
|
+ cwd,
|
100
|
97
|
&layout_template,
|
101
|
98
|
&post_listing_template,
|
102
|
99
|
&post_item_template,
|
|
@@ -111,14 +108,15 @@ pub fn build(include_drafts: bool, cwd: &path::Path) {
|
111
|
108
|
)
|
112
|
109
|
.expect("Couldn't copy assets directory");
|
113
|
110
|
|
114
|
|
- let rss = generate_rss(&config, &posts);
|
|
111
|
+ let rss = generate_rss(&config, posts);
|
115
|
112
|
fs::write(cwd.join("public").join("rss.xml"), rss).expect("Couldn't write rss file");
|
116
|
113
|
}
|
117
|
114
|
|
118
|
115
|
pub fn new(name: &str, cwd: &path::Path) {
|
119
|
116
|
let project_path = cwd.join(name);
|
120
|
117
|
|
121
|
|
- fs::create_dir(&project_path).expect(&format!("Couldn't create directory '{}'", &name));
|
|
118
|
+ fs::create_dir(&project_path)
|
|
119
|
+ .unwrap_or_else(|_| panic!("Couldn't create directory '{}'", &name));
|
122
|
120
|
fs::write(
|
123
|
121
|
project_path.join("casaubon.toml"),
|
124
|
122
|
format!(
|
|
@@ -138,7 +136,7 @@ pub fn new(name: &str, cwd: &path::Path) {
|
138
|
136
|
"js",
|
139
|
137
|
] {
|
140
|
138
|
fs::create_dir(&project_path.join(&dir))
|
141
|
|
- .expect(&format!("Couldn't create {} directory", &dir));
|
|
139
|
+ .unwrap_or_else(|_| panic!("Couldn't create {} directory", &dir));
|
142
|
140
|
}
|
143
|
141
|
|
144
|
142
|
let default_layout_template = format!(
|
|
@@ -153,29 +151,25 @@ pub fn new(name: &str, cwd: &path::Path) {
|
153
|
151
|
</html>\n",
|
154
|
152
|
name, name
|
155
|
153
|
);
|
156
|
|
- let default_post_listing_template = format!(
|
157
|
|
- "<div>
|
|
154
|
+ let default_post_listing_template = "<div>
|
158
|
155
|
<h3>Posts</h3>
|
159
|
|
- <ul>{{{{ post_listing }}}}</ul>
|
|
156
|
+ <ul>{{ post_listing }}</ul>
|
160
|
157
|
</div>\n"
|
161
|
|
- );
|
162
|
|
- let default_post_template = format!(
|
163
|
|
- "<article>
|
164
|
|
- <h1>{{{{ title }}}}</h1>
|
165
|
|
- <div>{{{{ body }}}}</div>
|
|
158
|
+ .to_string();
|
|
159
|
+ let default_post_template = "<article>
|
|
160
|
+ <h1>{{ title }}</h1>
|
|
161
|
+ <div>{{ body }}</div>
|
166
|
162
|
</article>\n"
|
167
|
|
- );
|
168
|
|
- let default_page_template = format!(
|
169
|
|
- "<article>
|
170
|
|
- <h1>{{{{ title }}}}</h1>
|
171
|
|
- <div>{{{{ body }}}}</div>
|
|
163
|
+ .to_string();
|
|
164
|
+ let default_page_template = "<article>
|
|
165
|
+ <h1>{{ title }}</h1>
|
|
166
|
+ <div>{{ body }}</div>
|
172
|
167
|
</article>\n"
|
173
|
|
- );
|
174
|
|
- let default_post_listing_item_template = format!(
|
175
|
|
- "<li>
|
176
|
|
- {{ date }} <a href=\"/posts/{{{{ slug }}}}/\">{{{{ title }}}}</a>
|
|
168
|
+ .to_string();
|
|
169
|
+ let default_post_listing_item_template = "<li>
|
|
170
|
+ { date } <a href=\"/posts/{{ slug }}/\">{{ title }}</a>
|
177
|
171
|
</li>\n"
|
178
|
|
- );
|
|
172
|
+ .to_string();
|
179
|
173
|
|
180
|
174
|
for (filename, contents) in &[
|
181
|
175
|
("layout", &default_layout_template),
|
|
@@ -190,11 +184,11 @@ pub fn new(name: &str, cwd: &path::Path) {
|
190
|
184
|
.join(format!("{}.html", filename)),
|
191
|
185
|
&contents,
|
192
|
186
|
)
|
193
|
|
- .expect(&format!("Couldn't write templates/{}.html", filename));
|
|
187
|
+ .unwrap_or_else(|_| panic!("Couldn't write templates/{}.html", filename));
|
194
|
188
|
}
|
195
|
189
|
}
|
196
|
190
|
|
197
|
|
-fn should_rebuild(cwd: &path::Path, path: &path::PathBuf) -> bool {
|
|
191
|
+fn should_rebuild(cwd: &path::Path, path: &path::Path) -> bool {
|
198
|
192
|
let path_string = path.to_str().unwrap().to_string();
|
199
|
193
|
let change_is_from_public = path_string.contains(cwd.join("public").to_str().unwrap());
|
200
|
194
|
let change_is_from_git = path_string.contains(cwd.join(".git").to_str().unwrap());
|
|
@@ -204,14 +198,14 @@ fn should_rebuild(cwd: &path::Path, path: &path::PathBuf) -> bool {
|
204
|
198
|
|
205
|
199
|
pub fn watch(include_drafts: bool, cwd: &path::Path) -> notify::Result<()> {
|
206
|
200
|
let (tx, rx) = channel();
|
207
|
|
- let mut watcher: RecommendedWatcher = try!(Watcher::new(tx, Duration::from_secs(2)));
|
208
|
|
- try!(watcher.watch(&cwd, RecursiveMode::Recursive));
|
|
201
|
+ let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_secs(2))?;
|
|
202
|
+ watcher.watch(&cwd, RecursiveMode::Recursive)?;
|
209
|
203
|
println!("Watching {}", cwd.to_str().unwrap());
|
210
|
204
|
|
211
|
205
|
let handle_event = |path: &path::PathBuf| {
|
212
|
|
- if should_rebuild(&cwd, &path) {
|
|
206
|
+ if should_rebuild(cwd, path) {
|
213
|
207
|
println!("Rebuilding");
|
214
|
|
- build(include_drafts, &cwd);
|
|
208
|
+ build(include_drafts, cwd);
|
215
|
209
|
}
|
216
|
210
|
};
|
217
|
211
|
|
|
@@ -243,26 +237,14 @@ mod tests {
|
243
|
237
|
#[test]
|
244
|
238
|
fn test_should_rebuild() {
|
245
|
239
|
let cwd = env::current_dir().unwrap();
|
246
|
|
- assert_eq!(
|
247
|
|
- false,
|
248
|
|
- should_rebuild(&cwd, &cwd.join("public").join("index.html"))
|
249
|
|
- );
|
250
|
|
- assert_eq!(
|
251
|
|
- false,
|
252
|
|
- should_rebuild(&cwd, &cwd.join(".git").join("index.html"))
|
253
|
|
- );
|
254
|
|
- assert_eq!(
|
255
|
|
- true,
|
256
|
|
- should_rebuild(&cwd, &cwd.join("posts").join("test.md"))
|
257
|
|
- );
|
258
|
|
- assert_eq!(
|
259
|
|
- true,
|
260
|
|
- should_rebuild(&cwd, &cwd.join("drafts").join("test.md"))
|
261
|
|
- );
|
262
|
|
- assert_eq!(
|
263
|
|
- true,
|
264
|
|
- should_rebuild(&cwd, &cwd.join("css").join("style.css"))
|
265
|
|
- );
|
266
|
|
- assert_eq!(true, should_rebuild(&cwd, &cwd.join("js").join("index.js")));
|
|
240
|
+ assert!(!should_rebuild(
|
|
241
|
+ &cwd,
|
|
242
|
+ &cwd.join("public").join("index.html")
|
|
243
|
+ ));
|
|
244
|
+ assert!(!should_rebuild(&cwd, &cwd.join(".git").join("index.html")));
|
|
245
|
+ assert!(should_rebuild(&cwd, &cwd.join("posts").join("test.md")));
|
|
246
|
+ assert!(should_rebuild(&cwd, &cwd.join("drafts").join("test.md")));
|
|
247
|
+ assert!(should_rebuild(&cwd, &cwd.join("css").join("style.css")));
|
|
248
|
+ assert!(should_rebuild(&cwd, &cwd.join("js").join("index.js")));
|
267
|
249
|
}
|
268
|
250
|
}
|