소스 검색

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 년 전
부모
커밋
ca3ad587d4
2개의 변경된 파일67개의 추가작업 그리고 6개의 파일을 삭제
  1. 35
    4
      src/commands.rs
  2. 32
    2
      src/write.rs

+ 35
- 4
src/commands.rs 파일 보기

8
 
8
 
9
 use config::Config;
9
 use config::Config;
10
 use post::{parse_post, read_posts_dir};
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
 pub fn build(include_drafts: bool) {
13
 pub fn build(include_drafts: bool) {
14
     let cwd = env::current_dir().expect("Couldn't read current directory");
14
     let cwd = env::current_dir().expect("Couldn't read current directory");
32
     }
32
     }
33
 
33
 
34
     fs::create_dir(cwd.join("public")).expect("Couldn't create public directory");
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
     let layout_template = fs::read_to_string(&cwd.join("templates").join("layout.html"))
37
     let layout_template = fs::read_to_string(&cwd.join("templates").join("layout.html"))
37
         .expect("Couldn't find layout template");
38
         .expect("Couldn't find layout template");
52
         false => read_posts_dir(&cwd.join("posts")),
53
         false => read_posts_dir(&cwd.join("posts")),
53
     };
54
     };
54
 
55
 
56
+    let page_paths = read_posts_dir(&cwd.join("pages"));
57
+
55
     let posts = post_paths
58
     let posts = post_paths
56
         .into_iter()
59
         .into_iter()
57
         .map(|entry| {
60
         .map(|entry| {
61
         })
64
         })
62
         .collect();
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
     write_post_listing(
72
     write_post_listing(
65
         &cwd,
73
         &cwd,
66
         &layout_template,
74
         &layout_template,
89
     )
97
     )
90
     .expect("Could not create casaubon.toml");
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
         fs::create_dir(&project_path.join(&dir))
109
         fs::create_dir(&project_path.join(&dir))
94
             .expect(&format!("Couldn't create {} directory", &dir));
110
             .expect(&format!("Couldn't create {} directory", &dir));
95
     }
111
     }
209
         env::set_current_dir(&project_dir).unwrap();
225
         env::set_current_dir(&project_dir).unwrap();
210
 
226
 
211
         fs::create_dir(project_dir.join("posts")).unwrap();
227
         fs::create_dir(project_dir.join("posts")).unwrap();
228
+        fs::create_dir(project_dir.join("pages")).unwrap();
212
         fs::create_dir(project_dir.join("public")).unwrap();
229
         fs::create_dir(project_dir.join("public")).unwrap();
230
+        fs::create_dir(project_dir.join("public").join("posts")).unwrap();
213
         fs::create_dir(project_dir.join("templates")).unwrap();
231
         fs::create_dir(project_dir.join("templates")).unwrap();
214
         fs::create_dir(project_dir.join("css")).unwrap();
232
         fs::create_dir(project_dir.join("css")).unwrap();
215
         fs::create_dir(project_dir.join("js")).unwrap();
233
         fs::create_dir(project_dir.join("js")).unwrap();
249
             "# First post\n\nThis is the first post\n\nIt has multiple paragraphs",
267
             "# First post\n\nThis is the first post\n\nIt has multiple paragraphs",
250
         )
268
         )
251
         .unwrap();
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
         build(false);
276
         build(false);
255
 
277
 
266
             fs::read_to_string(
288
             fs::read_to_string(
267
                 project_dir
289
                 project_dir
268
                     .join("public")
290
                     .join("public")
291
+                    .join("posts")
269
                     .join("first-post")
292
                     .join("first-post")
270
                     .join("index.html")
293
                     .join("index.html")
271
             )
294
             )
296
 
319
 
297
         fs::create_dir(project_dir.join("drafts")).unwrap();
320
         fs::create_dir(project_dir.join("drafts")).unwrap();
298
         fs::create_dir(project_dir.join("posts")).unwrap();
321
         fs::create_dir(project_dir.join("posts")).unwrap();
322
+        fs::create_dir(project_dir.join("pages")).unwrap();
299
         fs::create_dir(project_dir.join("public")).unwrap();
323
         fs::create_dir(project_dir.join("public")).unwrap();
324
+        fs::create_dir(project_dir.join("public").join("posts")).unwrap();
300
         fs::create_dir(project_dir.join("templates")).unwrap();
325
         fs::create_dir(project_dir.join("templates")).unwrap();
301
         fs::create_dir(project_dir.join("css")).unwrap();
326
         fs::create_dir(project_dir.join("css")).unwrap();
302
         fs::create_dir(project_dir.join("js")).unwrap();
327
         fs::create_dir(project_dir.join("js")).unwrap();
341
             "# First draft\n\nThis is the first draft",
366
             "# First draft\n\nThis is the first draft",
342
         )
367
         )
343
         .unwrap();
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
         build(true);
375
         build(true);
347
 
376
 
355
             fs::read_to_string(
384
             fs::read_to_string(
356
                 project_dir
385
                 project_dir
357
                     .join("public")
386
                     .join("public")
387
+                    .join("posts")
358
                     .join("first-post")
388
                     .join("first-post")
359
                     .join("index.html")
389
                     .join("index.html")
360
             ).unwrap()
390
             ).unwrap()
366
             fs::read_to_string(
396
             fs::read_to_string(
367
                 project_dir
397
                 project_dir
368
                     .join("public")
398
                     .join("public")
399
+                    .join("posts")
369
                     .join("first-draft")
400
                     .join("first-draft")
370
                     .join("index.html")
401
                     .join("index.html")
371
             ).unwrap()
402
             ).unwrap()

+ 32
- 2
src/write.rs 파일 보기

10
     post_template: &str,
10
     post_template: &str,
11
     post: &Post,
11
     post: &Post,
12
     config: &Config,
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
     match fs::create_dir(cwd.join("public").join(&post.slug)) {
38
     match fs::create_dir(cwd.join("public").join(&post.slug)) {
15
         Ok(_) => {}
39
         Ok(_) => {}
64
 
88
 
65
         let cwd = env::current_dir().unwrap();
89
         let cwd = env::current_dir().unwrap();
66
         fs::create_dir(cwd.join("public")).unwrap();
90
         fs::create_dir(cwd.join("public")).unwrap();
91
+        fs::create_dir(cwd.join("public").join("posts")).unwrap();
67
 
92
 
68
         let layout =
93
         let layout =
69
             "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>";
94
             "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>";
79
 
104
 
80
         write_post(&cwd, &layout, &post_template, &post, &config);
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
         assert_eq!(
115
         assert_eq!(
86
             "<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>",
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…
취소
저장