Browse Source

Implement drafts

master
Dylan Baker 5 years ago
parent
commit
346794e194
3 changed files with 113 additions and 20 deletions
  1. 99
    13
      src/commands.rs
  2. 11
    4
      src/main.rs
  3. 3
    3
      src/post.rs

+ 99
- 13
src/commands.rs View File

@@ -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
 }

+ 11
- 4
src/main.rs View File

@@ -28,19 +28,26 @@ fn main() {
28 28
                 .required(true)
29 29
                 .possible_values(&["build", "new", "watch"])
30 30
                 .index(1),
31
-        ).arg(
31
+        ).arg(Arg::with_name("drafts").short("d").long("drafts"))
32
+        .arg(
32 33
             Arg::with_name("name")
33 34
                 .required_if("command", "new")
34 35
                 .index(2),
35 36
         ).get_matches();
36 37
 
38
+    let include_drafts = match matches.occurrences_of("drafts") {
39
+        0 => false,
40
+        1 => true,
41
+        _ => true,
42
+    };
43
+
37 44
     let command = matches.value_of("command").unwrap();
38 45
     if command == "build" {
39
-        build();
46
+        build(include_drafts);
40 47
     } else if command == "new" {
41 48
         new(&matches.value_of("name").unwrap());
42 49
     } else if command == "watch" {
43
-        build();
44
-        watch().expect("Error while watching posts directory");
50
+        build(include_drafts);
51
+        watch(include_drafts).expect("Error while watching posts directory");
45 52
     }
46 53
 }

+ 3
- 3
src/post.rs View File

@@ -10,9 +10,9 @@ pub struct Post {
10 10
     pub slug: String,
11 11
 }
12 12
 
13
-pub fn read_posts_dir(cwd: &path::PathBuf) -> fs::ReadDir {
13
+pub fn read_posts_dir(cwd: &path::PathBuf) -> Vec<fs::DirEntry> {
14 14
     match fs::read_dir(cwd) {
15
-        Ok(posts) => posts,
15
+        Ok(posts) => posts.into_iter().map(|post| post.unwrap()).collect(),
16 16
         Err(err) => panic!(err),
17 17
     }
18 18
 }
@@ -83,7 +83,7 @@ mod tests {
83 83
         expected_paths.sort();
84 84
         let mut actual_paths: Vec<String> = read_posts_dir(&cwd.join("posts"))
85 85
             .into_iter()
86
-            .map(|dir_entry| String::from(dir_entry.unwrap().path().to_str().unwrap()))
86
+            .map(|dir_entry| String::from(dir_entry.path().to_str().unwrap()))
87 87
             .collect();
88 88
         actual_paths.sort();
89 89
 

Loading…
Cancel
Save