|
@@ -10,7 +10,7 @@ use config::Config;
|
10
|
10
|
use post::{parse_post, read_posts_dir};
|
11
|
11
|
use write::{write_post, write_post_listing};
|
12
|
12
|
|
13
|
|
-pub fn build() {
|
|
13
|
+pub fn build(include_drafts: bool) {
|
14
|
14
|
let cwd = env::current_dir().expect("Couldn't read current directory");
|
15
|
15
|
let config = match fs::read_to_string(cwd.join("tlon.toml")) {
|
16
|
16
|
Ok(contents) => match contents.parse::<Value>() {
|
|
@@ -43,16 +43,21 @@ pub fn build() {
|
43
|
43
|
fs::read_to_string(cwd.join("templates").join("post_listing_item.html"))
|
44
|
44
|
.expect("Couldn't find post listing item template");
|
45
|
45
|
|
46
|
|
- let post_paths = read_posts_dir(&cwd.join("posts"));
|
|
46
|
+ let post_paths = match include_drafts {
|
|
47
|
+ true => {
|
|
48
|
+ let mut posts = read_posts_dir(&cwd.join("posts"));
|
|
49
|
+ posts.append(&mut read_posts_dir(&cwd.join("drafts")));
|
|
50
|
+ posts
|
|
51
|
+ }
|
|
52
|
+ false => read_posts_dir(&cwd.join("posts")),
|
|
53
|
+ };
|
|
54
|
+
|
47
|
55
|
let posts = post_paths
|
48
|
56
|
.into_iter()
|
49
|
|
- .map(|path| match path {
|
50
|
|
- Ok(p) => {
|
51
|
|
- let post = parse_post(p.path());
|
52
|
|
- write_post(&cwd, &layout_template, &post_template, &post, &config);
|
53
|
|
- post
|
54
|
|
- }
|
55
|
|
- Err(err) => panic!(err),
|
|
57
|
+ .map(|entry| {
|
|
58
|
+ let post = parse_post(entry.path());
|
|
59
|
+ write_post(&cwd, &layout_template, &post_template, &post, &config);
|
|
60
|
+ post
|
56
|
61
|
}).collect();
|
57
|
62
|
|
58
|
63
|
write_post_listing(
|
|
@@ -81,7 +86,7 @@ pub fn new(name: &str) {
|
81
|
86
|
format!("site_name = \"{}\"", &name),
|
82
|
87
|
).expect("Could not create tlon.toml");
|
83
|
88
|
|
84
|
|
- for dir in &["posts", "public", "templates", "css", "js"] {
|
|
89
|
+ for dir in &["drafts", "posts", "public", "templates", "css", "js"] {
|
85
|
90
|
fs::create_dir(&project_path.join(&dir))
|
86
|
91
|
.expect(&format!("Couldn't create {} directory", &dir));
|
87
|
92
|
}
|
|
@@ -100,7 +105,7 @@ pub fn new(name: &str) {
|
100
|
105
|
}
|
101
|
106
|
}
|
102
|
107
|
|
103
|
|
-pub fn watch() -> notify::Result<()> {
|
|
108
|
+pub fn watch(include_drafts: bool) -> notify::Result<()> {
|
104
|
109
|
let cwd = env::current_dir().expect("Couldn't read current directory");
|
105
|
110
|
let (tx, rx) = channel();
|
106
|
111
|
let mut watcher: RecommendedWatcher = try!(Watcher::new(tx, Duration::from_secs(2)));
|
|
@@ -109,7 +114,7 @@ pub fn watch() -> notify::Result<()> {
|
109
|
114
|
loop {
|
110
|
115
|
match rx.recv() {
|
111
|
116
|
Ok(_) => {
|
112
|
|
- build();
|
|
117
|
+ build(include_drafts);
|
113
|
118
|
}
|
114
|
119
|
Err(e) => println!("watch error: {:?}", e),
|
115
|
120
|
}
|
|
@@ -213,7 +218,7 @@ mod tests {
|
213
|
218
|
).unwrap();
|
214
|
219
|
fs::write(project_dir.join("tlon.toml"), "site_name = \"Test Site\"").unwrap();
|
215
|
220
|
|
216
|
|
- build();
|
|
221
|
+ build(false);
|
217
|
222
|
|
218
|
223
|
assert_eq!(
|
219
|
224
|
"<html><head><title>Test Site</title></head><body><ul><li><a href=\"/fir\
|
|
@@ -246,4 +251,85 @@ mod tests {
|
246
|
251
|
|
247
|
252
|
fs::remove_dir_all(project_dir).unwrap();
|
248
|
253
|
}
|
|
254
|
+
|
|
255
|
+ #[test]
|
|
256
|
+ fn test_build_drafts() {
|
|
257
|
+ let temp_dir = env::temp_dir();
|
|
258
|
+ let uuid = Uuid::new_v4().to_string();
|
|
259
|
+ let project_dir = temp_dir.join(&uuid);
|
|
260
|
+ fs::create_dir(&project_dir).unwrap();
|
|
261
|
+ env::set_current_dir(&project_dir).unwrap();
|
|
262
|
+
|
|
263
|
+ fs::create_dir(project_dir.join("drafts")).unwrap();
|
|
264
|
+ fs::create_dir(project_dir.join("posts")).unwrap();
|
|
265
|
+ fs::create_dir(project_dir.join("public")).unwrap();
|
|
266
|
+ fs::create_dir(project_dir.join("templates")).unwrap();
|
|
267
|
+ fs::create_dir(project_dir.join("css")).unwrap();
|
|
268
|
+ fs::create_dir(project_dir.join("js")).unwrap();
|
|
269
|
+
|
|
270
|
+ fs::write(
|
|
271
|
+ project_dir.join("css").join("style.css"),
|
|
272
|
+ "body { background: blue; }",
|
|
273
|
+ ).unwrap();
|
|
274
|
+ fs::write(
|
|
275
|
+ project_dir.join("js").join("index.js"),
|
|
276
|
+ "window.onload = function () { alert() }",
|
|
277
|
+ ).unwrap();
|
|
278
|
+ fs::write(
|
|
279
|
+ project_dir.join("templates").join("layout.html"),
|
|
280
|
+ "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>",
|
|
281
|
+ ).unwrap();
|
|
282
|
+ fs::write(
|
|
283
|
+ project_dir.join("templates").join("post.html"),
|
|
284
|
+ "<article><h1>{{ title }}</h1><div>{{ body }}</div></article>",
|
|
285
|
+ ).unwrap();
|
|
286
|
+ fs::write(
|
|
287
|
+ project_dir.join("templates").join("post_listing.html"),
|
|
288
|
+ "<ul>{{ post_listing }}</ul>",
|
|
289
|
+ ).unwrap();
|
|
290
|
+ fs::write(
|
|
291
|
+ project_dir.join("templates").join("post_listing_item.html"),
|
|
292
|
+ "<li><a href=\"/{{ slug }}\">{{ title }}</a></li>",
|
|
293
|
+ ).unwrap();
|
|
294
|
+ fs::write(
|
|
295
|
+ project_dir.join("posts").join("first-post.md"),
|
|
296
|
+ "# First post\n\nThis is the first post",
|
|
297
|
+ ).unwrap();
|
|
298
|
+ fs::write(
|
|
299
|
+ project_dir.join("drafts").join("first-draft.md"),
|
|
300
|
+ "# First draft\n\nThis is the first draft",
|
|
301
|
+ ).unwrap();
|
|
302
|
+ fs::write(project_dir.join("tlon.toml"), "site_name = \"Test Site\"").unwrap();
|
|
303
|
+
|
|
304
|
+ build(true);
|
|
305
|
+
|
|
306
|
+ assert_eq!(
|
|
307
|
+ "<html><head><title>Test Site</title></head><body><ul><li><a href=\"/first-post\">First post</a></li><li><a href=\"/first-draft\">First draft</a></li></ul></body></html>",
|
|
308
|
+ fs::read_to_string(project_dir.join("public").join("index.html")).unwrap().replace("\n", ""),
|
|
309
|
+ );
|
|
310
|
+
|
|
311
|
+ assert_eq!(
|
|
312
|
+ "<html><head><title>First post | Test Site</title></head><body><article><h1>First post</h1><div><p>This is the first post</p></div></article></body></html>",
|
|
313
|
+ fs::read_to_string(
|
|
314
|
+ project_dir
|
|
315
|
+ .join("public")
|
|
316
|
+ .join("first-post")
|
|
317
|
+ .join("index.html")
|
|
318
|
+ ).unwrap()
|
|
319
|
+ .replace("\n", ""),
|
|
320
|
+ );
|
|
321
|
+
|
|
322
|
+ assert_eq!(
|
|
323
|
+ "<html><head><title>First draft | Test Site</title></head><body><article><h1>First draft</h1><div><p>This is the first draft</p></div></article></body></html>",
|
|
324
|
+ fs::read_to_string(
|
|
325
|
+ project_dir
|
|
326
|
+ .join("public")
|
|
327
|
+ .join("first-draft")
|
|
328
|
+ .join("index.html")
|
|
329
|
+ ).unwrap()
|
|
330
|
+ .replace("\n", ""),
|
|
331
|
+ );
|
|
332
|
+
|
|
333
|
+ fs::remove_dir_all(project_dir).unwrap();
|
|
334
|
+ }
|
249
|
335
|
}
|