Procházet zdrojové kódy

Use config to dynamically generate page titles

master
Dylan Baker před 5 roky
rodič
revize
45521a78cd
3 změnil soubory, kde provedl 77 přidání a 49 odebrání
  1. 5
    4
      src/main.rs
  2. 41
    32
      src/render.rs
  3. 31
    13
      src/write.rs

+ 5
- 4
src/main.rs Zobrazit soubor

61
         .map(|path| match path {
61
         .map(|path| match path {
62
             Ok(p) => {
62
             Ok(p) => {
63
                 let post = parse_post(p.path());
63
                 let post = parse_post(p.path());
64
-                write_post(&cwd, &layout_template, &post_template, &post);
64
+                write_post(&cwd, &layout_template, &post_template, &post, &config);
65
                 post
65
                 post
66
             }
66
             }
67
             Err(err) => panic!(err),
67
             Err(err) => panic!(err),
73
         &post_listing_template,
73
         &post_listing_template,
74
         &post_item_template,
74
         &post_item_template,
75
         &posts,
75
         &posts,
76
+        &config,
76
     );
77
     );
77
 
78
 
78
     fs_extra::copy_items(
79
     fs_extra::copy_items(
212
         ).unwrap();
213
         ).unwrap();
213
         fs::write(
214
         fs::write(
214
             project_dir.join("templates").join("layout.html"),
215
             project_dir.join("templates").join("layout.html"),
215
-            "<html><head><title>Test</title></head><body>{{ contents }}</body></html>",
216
+            "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>",
216
         ).unwrap();
217
         ).unwrap();
217
         fs::write(
218
         fs::write(
218
             project_dir.join("templates").join("post.html"),
219
             project_dir.join("templates").join("post.html"),
235
         build();
236
         build();
236
 
237
 
237
         assert_eq!(
238
         assert_eq!(
238
-            "<html><head><title>Test</title></head><body><ul><li><a href=\"/fir\
239
+            "<html><head><title>Test Site</title></head><body><ul><li><a href=\"/fir\
239
              st-post\">First post</a></li></ul></body></html>",
240
              st-post\">First post</a></li></ul></body></html>",
240
             fs::read_to_string(project_dir.join("public").join("index.html")).unwrap(),
241
             fs::read_to_string(project_dir.join("public").join("index.html")).unwrap(),
241
         );
242
         );
242
 
243
 
243
         assert_eq!(
244
         assert_eq!(
244
-            "<html><head><title>Test</title></head><body><article><h1>First pos\
245
+            "<html><head><title>First post | Test Site</title></head><body><article><h1>First pos\
245
              t</h1><div><p>This is the first post</p><p>It has multiple paragra\
246
              t</h1><div><p>This is the first post</p><p>It has multiple paragra\
246
              phs</p></div></article></body></html>",
247
              phs</p></div></article></body></html>",
247
             fs::read_to_string(
248
             fs::read_to_string(

+ 41
- 32
src/render.rs Zobrazit soubor

1
 use comrak::{markdown_to_html, ComrakOptions};
1
 use comrak::{markdown_to_html, ComrakOptions};
2
 
2
 
3
+use config::Config;
3
 use post::Post;
4
 use post::Post;
4
 
5
 
5
-pub fn render_post(layout: &str, post_template: &str, post: &Post) -> String {
6
-    layout.replace(
7
-        "{{ contents }}",
8
-        &post_template.replace("{{ title }}", &post.title).replace(
9
-            "{{ body }}",
10
-            &markdown_to_html(&post.body, &ComrakOptions::default()),
11
-        ),
12
-    )
6
+pub fn render_post(layout: &str, post_template: &str, post: &Post, config: &Config) -> String {
7
+    layout
8
+        .replace(
9
+            "{{ page_title }}",
10
+            &format!("{} | {}", post.title, config.site_name),
11
+        ).replace(
12
+            "{{ contents }}",
13
+            &post_template.replace("{{ title }}", &post.title).replace(
14
+                "{{ body }}",
15
+                &markdown_to_html(&post.body, &ComrakOptions::default()),
16
+            ),
17
+        )
13
 }
18
 }
14
 
19
 
15
 pub fn render_post_listing(
20
 pub fn render_post_listing(
17
     post_listing_template: &str,
22
     post_listing_template: &str,
18
     post_item_template: &str,
23
     post_item_template: &str,
19
     posts: &Vec<Post>,
24
     posts: &Vec<Post>,
25
+    config: &Config,
20
 ) -> String {
26
 ) -> String {
21
-    layout.replace(
22
-        "{{ contents }}",
23
-        &post_listing_template.replace(
24
-            "{{ post_listing }}",
25
-            &posts
26
-                .iter()
27
-                .map(|ref post| {
28
-                    post_item_template
29
-                        .replace("{{ slug }}", &post.slug)
30
-                        .replace("{{ title }}", &post.title)
31
-                }).collect::<Vec<String>>()
32
-                .join("\n"),
33
-        ),
34
-    )
27
+    layout
28
+        .replace("{{ page_title }}", &format!("{}", config.site_name))
29
+        .replace(
30
+            "{{ contents }}",
31
+            &post_listing_template.replace(
32
+                "{{ post_listing }}",
33
+                &posts
34
+                    .iter()
35
+                    .map(|ref post| {
36
+                        post_item_template
37
+                            .replace("{{ slug }}", &post.slug)
38
+                            .replace("{{ title }}", &post.title)
39
+                    }).collect::<Vec<String>>()
40
+                    .join("\n"),
41
+            ),
42
+        )
35
 }
43
 }
36
 
44
 
37
 #[cfg(test)]
45
 #[cfg(test)]
38
 mod tests {
46
 mod tests {
39
     #[allow(unused_imports)]
47
     #[allow(unused_imports)]
40
-    use super::{render_post, render_post_listing, Post};
48
+    use super::{render_post, render_post_listing, Config, Post};
41
 
49
 
42
     #[test]
50
     #[test]
43
     fn test_render_post() {
51
     fn test_render_post() {
44
         let output = render_post(
52
         let output = render_post(
45
-            "<html><head><title>Test</title></head><body>{{ contents }}</body></html>",
53
+            "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>",
46
             "<article><h1>{{ title }}</h1><div>{{ body }}</div></article>",
54
             "<article><h1>{{ title }}</h1><div>{{ body }}</div></article>",
47
             &Post {
55
             &Post {
48
                 title: String::from("hello world"),
56
                 title: String::from("hello world"),
49
                 body: String::from("lorem ipsum dolor sit amet"),
57
                 body: String::from("lorem ipsum dolor sit amet"),
50
                 slug: String::from("hello-world"),
58
                 slug: String::from("hello-world"),
51
             },
59
             },
60
+            &Config {
61
+                site_name: "Test Site".to_string(),
62
+            },
52
         ).replace("\n", "");
63
         ).replace("\n", "");
53
 
64
 
54
         assert_eq!(
65
         assert_eq!(
55
-            "<html><head><title>Test</title></head><body><article><h1>hello wo\
56
-             rld</h1><div><p>lorem ipsum dolor sit amet</p></div></article></b\
57
-             ody></html>",
66
+            "<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>",
58
             &output,
67
             &output,
59
         );
68
         );
60
     }
69
     }
79
             },
88
             },
80
         ];
89
         ];
81
         let output = render_post_listing(
90
         let output = render_post_listing(
82
-            "<html><head><title>Test</title></head><body>{{ contents }}</body></html>",
91
+            "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>",
83
             "<ul>{{ post_listing }}</ul>",
92
             "<ul>{{ post_listing }}</ul>",
84
             "<li><a href=\"/{{ slug }}\">{{ title }}</a></li>",
93
             "<li><a href=\"/{{ slug }}\">{{ title }}</a></li>",
85
             &posts,
94
             &posts,
95
+            &Config {
96
+                site_name: "Test Site".to_string(),
97
+            },
86
         ).replace("\n", "");
98
         ).replace("\n", "");
87
 
99
 
88
         assert_eq!(
100
         assert_eq!(
89
-            "<html><head><title>Test</title></head><body><ul><li><a href=\"/fir\
90
-             st-post\">First post</a></li><li><a href=\"/second-post\">Second po\
91
-             st</a></li><li><a href=\"/third-post\">Third post</a></li></ul></bo\
92
-             dy></html>",
101
+            "<html><head><title>Test Site</title></head><body><ul><li><a href=\"/first-post\">First post</a></li><li><a href=\"/second-post\">Second post</a></li><li><a href=\"/third-post\">Third post</a></li></ul></body></html>",
93
             &output,
102
             &output,
94
         );
103
         );
95
     }
104
     }

+ 31
- 13
src/write.rs Zobrazit soubor

1
+use config::Config;
1
 use post::Post;
2
 use post::Post;
2
 use render::{render_post, render_post_listing};
3
 use render::{render_post, render_post_listing};
3
 use std::fs;
4
 use std::fs;
4
 use std::path;
5
 use std::path;
5
 
6
 
6
-pub fn write_post(cwd: &path::PathBuf, layout: &str, post_template: &str, post: &Post) {
7
+pub fn write_post(
8
+    cwd: &path::PathBuf,
9
+    layout: &str,
10
+    post_template: &str,
11
+    post: &Post,
12
+    config: &Config,
13
+) {
7
     match fs::create_dir(cwd.join("public").join(&post.slug)) {
14
     match fs::create_dir(cwd.join("public").join(&post.slug)) {
8
         Ok(_) => {}
15
         Ok(_) => {}
9
         Err(err) => match err.kind() {
16
         Err(err) => match err.kind() {
13
     }
20
     }
14
     fs::write(
21
     fs::write(
15
         cwd.join("public").join(&post.slug).join("index.html"),
22
         cwd.join("public").join(&post.slug).join("index.html"),
16
-        render_post(layout, post_template, post),
23
+        render_post(layout, post_template, post, config),
17
     ).expect("Unable to write file");
24
     ).expect("Unable to write file");
18
 }
25
 }
19
 
26
 
23
     post_listing_template: &str,
30
     post_listing_template: &str,
24
     post_item_template: &str,
31
     post_item_template: &str,
25
     posts: &Vec<Post>,
32
     posts: &Vec<Post>,
33
+    config: &Config,
26
 ) {
34
 ) {
27
     fs::write(
35
     fs::write(
28
         cwd.join("public").join("index.html"),
36
         cwd.join("public").join("index.html"),
29
-        render_post_listing(layout, post_listing_template, post_item_template, posts),
37
+        render_post_listing(
38
+            layout,
39
+            post_listing_template,
40
+            post_item_template,
41
+            posts,
42
+            config,
43
+        ),
30
     ).expect("Unable to write file");
44
     ).expect("Unable to write file");
31
 }
45
 }
32
 
46
 
49
         let cwd = env::current_dir().unwrap();
63
         let cwd = env::current_dir().unwrap();
50
         fs::create_dir(cwd.join("public")).unwrap();
64
         fs::create_dir(cwd.join("public")).unwrap();
51
 
65
 
52
-        let layout = "<html><head><title>Test</title></head><body>{{ contents }}</body></html>";
66
+        let layout =
67
+            "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>";
53
         let post_template = "<article><h1>{{ title }}</h1><div>{{ body }}</div></article>";
68
         let post_template = "<article><h1>{{ title }}</h1><div>{{ body }}</div></article>";
54
         let post = Post {
69
         let post = Post {
55
             title: String::from("Hello world"),
70
             title: String::from("Hello world"),
56
             body: String::from("Lorem ipsum dolor sit amet"),
71
             body: String::from("Lorem ipsum dolor sit amet"),
57
             slug: String::from("hello-world"),
72
             slug: String::from("hello-world"),
58
         };
73
         };
74
+        let config = Config {
75
+            site_name: "Test Site".to_string(),
76
+        };
59
 
77
 
60
-        write_post(&cwd, &layout, &post_template, &post);
78
+        write_post(&cwd, &layout, &post_template, &post, &config);
61
 
79
 
62
         let content =
80
         let content =
63
             fs::read_to_string(cwd.join("public").join("hello-world").join("index.html")).unwrap();
81
             fs::read_to_string(cwd.join("public").join("hello-world").join("index.html")).unwrap();
64
 
82
 
65
         assert_eq!(
83
         assert_eq!(
66
-            "<html><head><title>Test</title></head><body><article><h1>Hello wor\
67
-             ld</h1><div><p>Lorem ipsum dolor sit amet</p></div></article></body\
68
-             ></html>",
84
+            "<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>",
69
             content.replace("\n", "")
85
             content.replace("\n", "")
70
         );
86
         );
71
 
87
 
82
         let cwd = env::current_dir().unwrap();
98
         let cwd = env::current_dir().unwrap();
83
         fs::create_dir(cwd.join("public")).unwrap();
99
         fs::create_dir(cwd.join("public")).unwrap();
84
 
100
 
85
-        let layout = "<html><head><title>Test</title></head><body>{{ contents }}</body></html>";
101
+        let layout =
102
+            "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>";
86
         let post_listing_template = "<ul>{{ post_listing }}</ul>";
103
         let post_listing_template = "<ul>{{ post_listing }}</ul>";
87
         let post_item_template = "<li><a href=\"/{{ slug }}\">{{ title }}</a></li>";
104
         let post_item_template = "<li><a href=\"/{{ slug }}\">{{ title }}</a></li>";
88
         let posts = vec![
105
         let posts = vec![
102
                 slug: String::from("third-post"),
119
                 slug: String::from("third-post"),
103
             },
120
             },
104
         ];
121
         ];
122
+        let config = Config {
123
+            site_name: "Test Site".to_string(),
124
+        };
105
         write_post_listing(
125
         write_post_listing(
106
             &cwd,
126
             &cwd,
107
             &layout,
127
             &layout,
108
             &post_listing_template,
128
             &post_listing_template,
109
             &post_item_template,
129
             &post_item_template,
110
             &posts,
130
             &posts,
131
+            &config,
111
         );
132
         );
112
 
133
 
113
         assert_eq!(
134
         assert_eq!(
114
-            "<html><head><title>Test</title></head><body><ul><li><a href=\"/fir\
115
-             st-post\">First post</a></li><li><a href=\"/second-post\">Second po\
116
-             st</a></li><li><a href=\"/third-post\">Third post</a></li></ul></bo\
117
-             dy></html>",
135
+            "<html><head><title>Test Site</title></head><body><ul><li><a href=\"/first-post\">First post</a></li><li><a href=\"/second-post\">Second post</a></li><li><a href=\"/third-post\">Third post</a></li></ul></body></html>",
118
             fs::read_to_string(&cwd.join("public").join("index.html"))
136
             fs::read_to_string(&cwd.join("public").join("index.html"))
119
                 .unwrap()
137
                 .unwrap()
120
                 .replace("\n", ""),
138
                 .replace("\n", ""),

Načítá se…
Zrušit
Uložit