Browse Source

Introduce pages, move posts under /posts

Currently pages reuse the post template, which isn't ideal but that will
change soon.
master
Dylan Baker 5 years ago
parent
commit
ca3ad587d4
2 changed files with 67 additions and 6 deletions
  1. 35
    4
      src/commands.rs
  2. 32
    2
      src/write.rs

+ 35
- 4
src/commands.rs View File

@@ -8,7 +8,7 @@ use toml::Value;
8 8
 
9 9
 use config::Config;
10 10
 use post::{parse_post, read_posts_dir};
11
-use write::{write_post, write_post_listing};
11
+use write::{write_page, write_post, write_post_listing};
12 12
 
13 13
 pub fn build(include_drafts: bool) {
14 14
     let cwd = env::current_dir().expect("Couldn't read current directory");
@@ -32,6 +32,7 @@ pub fn build(include_drafts: bool) {
32 32
     }
33 33
 
34 34
     fs::create_dir(cwd.join("public")).expect("Couldn't create public directory");
35
+    fs::create_dir(cwd.join("public").join("posts")).expect("Couldn't create posts directory");
35 36
 
36 37
     let layout_template = fs::read_to_string(&cwd.join("templates").join("layout.html"))
37 38
         .expect("Couldn't find layout template");
@@ -52,6 +53,8 @@ pub fn build(include_drafts: bool) {
52 53
         false => read_posts_dir(&cwd.join("posts")),
53 54
     };
54 55
 
56
+    let page_paths = read_posts_dir(&cwd.join("pages"));
57
+
55 58
     let posts = post_paths
56 59
         .into_iter()
57 60
         .map(|entry| {
@@ -61,6 +64,11 @@ pub fn build(include_drafts: bool) {
61 64
         })
62 65
         .collect();
63 66
 
67
+    for entry in page_paths.into_iter() {
68
+        let post = parse_post(entry.path());
69
+        write_page(&cwd, &layout_template, &post_template, &post, &config);
70
+    }
71
+
64 72
     write_post_listing(
65 73
         &cwd,
66 74
         &layout_template,
@@ -89,7 +97,15 @@ pub fn new(name: &str) {
89 97
     )
90 98
     .expect("Could not create casaubon.toml");
91 99
 
92
-    for dir in &["drafts", "posts", "public", "templates", "css", "js"] {
100
+    for dir in &[
101
+        "drafts",
102
+        "posts",
103
+        "pages",
104
+        "public",
105
+        "templates",
106
+        "css",
107
+        "js",
108
+    ] {
93 109
         fs::create_dir(&project_path.join(&dir))
94 110
             .expect(&format!("Couldn't create {} directory", &dir));
95 111
     }
@@ -209,7 +225,9 @@ mod tests {
209 225
         env::set_current_dir(&project_dir).unwrap();
210 226
 
211 227
         fs::create_dir(project_dir.join("posts")).unwrap();
228
+        fs::create_dir(project_dir.join("pages")).unwrap();
212 229
         fs::create_dir(project_dir.join("public")).unwrap();
230
+        fs::create_dir(project_dir.join("public").join("posts")).unwrap();
213 231
         fs::create_dir(project_dir.join("templates")).unwrap();
214 232
         fs::create_dir(project_dir.join("css")).unwrap();
215 233
         fs::create_dir(project_dir.join("js")).unwrap();
@@ -249,7 +267,11 @@ mod tests {
249 267
             "# First post\n\nThis is the first post\n\nIt has multiple paragraphs",
250 268
         )
251 269
         .unwrap();
252
-        fs::write(project_dir.join("casaubon.toml"), "site_name = \"Test Site\"").unwrap();
270
+        fs::write(
271
+            project_dir.join("casaubon.toml"),
272
+            "site_name = \"Test Site\"",
273
+        )
274
+        .unwrap();
253 275
 
254 276
         build(false);
255 277
 
@@ -266,6 +288,7 @@ mod tests {
266 288
             fs::read_to_string(
267 289
                 project_dir
268 290
                     .join("public")
291
+                    .join("posts")
269 292
                     .join("first-post")
270 293
                     .join("index.html")
271 294
             )
@@ -296,7 +319,9 @@ mod tests {
296 319
 
297 320
         fs::create_dir(project_dir.join("drafts")).unwrap();
298 321
         fs::create_dir(project_dir.join("posts")).unwrap();
322
+        fs::create_dir(project_dir.join("pages")).unwrap();
299 323
         fs::create_dir(project_dir.join("public")).unwrap();
324
+        fs::create_dir(project_dir.join("public").join("posts")).unwrap();
300 325
         fs::create_dir(project_dir.join("templates")).unwrap();
301 326
         fs::create_dir(project_dir.join("css")).unwrap();
302 327
         fs::create_dir(project_dir.join("js")).unwrap();
@@ -341,7 +366,11 @@ mod tests {
341 366
             "# First draft\n\nThis is the first draft",
342 367
         )
343 368
         .unwrap();
344
-        fs::write(project_dir.join("casaubon.toml"), "site_name = \"Test Site\"").unwrap();
369
+        fs::write(
370
+            project_dir.join("casaubon.toml"),
371
+            "site_name = \"Test Site\"",
372
+        )
373
+        .unwrap();
345 374
 
346 375
         build(true);
347 376
 
@@ -355,6 +384,7 @@ mod tests {
355 384
             fs::read_to_string(
356 385
                 project_dir
357 386
                     .join("public")
387
+                    .join("posts")
358 388
                     .join("first-post")
359 389
                     .join("index.html")
360 390
             ).unwrap()
@@ -366,6 +396,7 @@ mod tests {
366 396
             fs::read_to_string(
367 397
                 project_dir
368 398
                     .join("public")
399
+                    .join("posts")
369 400
                     .join("first-draft")
370 401
                     .join("index.html")
371 402
             ).unwrap()

+ 32
- 2
src/write.rs View File

@@ -10,6 +10,30 @@ pub fn write_post(
10 10
     post_template: &str,
11 11
     post: &Post,
12 12
     config: &Config,
13
+) {
14
+    match fs::create_dir(cwd.join("public").join("posts").join(&post.slug)) {
15
+        Ok(_) => {}
16
+        Err(err) => match err.kind() {
17
+            std::io::ErrorKind::AlreadyExists => {}
18
+            _ => panic!(err),
19
+        },
20
+    }
21
+    fs::write(
22
+        cwd.join("public")
23
+            .join("posts")
24
+            .join(&post.slug)
25
+            .join("index.html"),
26
+        render_post(layout, post_template, post, config),
27
+    )
28
+    .expect("Unable to write file");
29
+}
30
+
31
+pub fn write_page(
32
+    cwd: &path::PathBuf,
33
+    layout: &str,
34
+    post_template: &str,
35
+    post: &Post,
36
+    config: &Config,
13 37
 ) {
14 38
     match fs::create_dir(cwd.join("public").join(&post.slug)) {
15 39
         Ok(_) => {}
@@ -64,6 +88,7 @@ mod tests {
64 88
 
65 89
         let cwd = env::current_dir().unwrap();
66 90
         fs::create_dir(cwd.join("public")).unwrap();
91
+        fs::create_dir(cwd.join("public").join("posts")).unwrap();
67 92
 
68 93
         let layout =
69 94
             "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>";
@@ -79,8 +104,13 @@ mod tests {
79 104
 
80 105
         write_post(&cwd, &layout, &post_template, &post, &config);
81 106
 
82
-        let content =
83
-            fs::read_to_string(cwd.join("public").join("hello-world").join("index.html")).unwrap();
107
+        let content = fs::read_to_string(
108
+            cwd.join("public")
109
+                .join("posts")
110
+                .join("hello-world")
111
+                .join("index.html"),
112
+        )
113
+        .unwrap();
84 114
 
85 115
         assert_eq!(
86 116
             "<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>",

Loading…
Cancel
Save