Browse Source

Use config to dynamically generate page titles

master
Dylan Baker 5 years ago
parent
commit
45521a78cd
3 changed files with 77 additions and 49 deletions
  1. 5
    4
      src/main.rs
  2. 41
    32
      src/render.rs
  3. 31
    13
      src/write.rs

+ 5
- 4
src/main.rs View File

@@ -61,7 +61,7 @@ fn build() {
61 61
         .map(|path| match path {
62 62
             Ok(p) => {
63 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 65
                 post
66 66
             }
67 67
             Err(err) => panic!(err),
@@ -73,6 +73,7 @@ fn build() {
73 73
         &post_listing_template,
74 74
         &post_item_template,
75 75
         &posts,
76
+        &config,
76 77
     );
77 78
 
78 79
     fs_extra::copy_items(
@@ -212,7 +213,7 @@ mod tests {
212 213
         ).unwrap();
213 214
         fs::write(
214 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 217
         ).unwrap();
217 218
         fs::write(
218 219
             project_dir.join("templates").join("post.html"),
@@ -235,13 +236,13 @@ mod tests {
235 236
         build();
236 237
 
237 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 240
              st-post\">First post</a></li></ul></body></html>",
240 241
             fs::read_to_string(project_dir.join("public").join("index.html")).unwrap(),
241 242
         );
242 243
 
243 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 246
              t</h1><div><p>This is the first post</p><p>It has multiple paragra\
246 247
              phs</p></div></article></body></html>",
247 248
             fs::read_to_string(

+ 41
- 32
src/render.rs View File

@@ -1,15 +1,20 @@
1 1
 use comrak::{markdown_to_html, ComrakOptions};
2 2
 
3
+use config::Config;
3 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 20
 pub fn render_post_listing(
@@ -17,44 +22,48 @@ pub fn render_post_listing(
17 22
     post_listing_template: &str,
18 23
     post_item_template: &str,
19 24
     posts: &Vec<Post>,
25
+    config: &Config,
20 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 45
 #[cfg(test)]
38 46
 mod tests {
39 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 50
     #[test]
43 51
     fn test_render_post() {
44 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 54
             "<article><h1>{{ title }}</h1><div>{{ body }}</div></article>",
47 55
             &Post {
48 56
                 title: String::from("hello world"),
49 57
                 body: String::from("lorem ipsum dolor sit amet"),
50 58
                 slug: String::from("hello-world"),
51 59
             },
60
+            &Config {
61
+                site_name: "Test Site".to_string(),
62
+            },
52 63
         ).replace("\n", "");
53 64
 
54 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 67
             &output,
59 68
         );
60 69
     }
@@ -79,17 +88,17 @@ mod tests {
79 88
             },
80 89
         ];
81 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 92
             "<ul>{{ post_listing }}</ul>",
84 93
             "<li><a href=\"/{{ slug }}\">{{ title }}</a></li>",
85 94
             &posts,
95
+            &Config {
96
+                site_name: "Test Site".to_string(),
97
+            },
86 98
         ).replace("\n", "");
87 99
 
88 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 102
             &output,
94 103
         );
95 104
     }

+ 31
- 13
src/write.rs View File

@@ -1,9 +1,16 @@
1
+use config::Config;
1 2
 use post::Post;
2 3
 use render::{render_post, render_post_listing};
3 4
 use std::fs;
4 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 14
     match fs::create_dir(cwd.join("public").join(&post.slug)) {
8 15
         Ok(_) => {}
9 16
         Err(err) => match err.kind() {
@@ -13,7 +20,7 @@ pub fn write_post(cwd: &path::PathBuf, layout: &str, post_template: &str, post:
13 20
     }
14 21
     fs::write(
15 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 24
     ).expect("Unable to write file");
18 25
 }
19 26
 
@@ -23,10 +30,17 @@ pub fn write_post_listing(
23 30
     post_listing_template: &str,
24 31
     post_item_template: &str,
25 32
     posts: &Vec<Post>,
33
+    config: &Config,
26 34
 ) {
27 35
     fs::write(
28 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 44
     ).expect("Unable to write file");
31 45
 }
32 46
 
@@ -49,23 +63,25 @@ mod tests {
49 63
         let cwd = env::current_dir().unwrap();
50 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 68
         let post_template = "<article><h1>{{ title }}</h1><div>{{ body }}</div></article>";
54 69
         let post = Post {
55 70
             title: String::from("Hello world"),
56 71
             body: String::from("Lorem ipsum dolor sit amet"),
57 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 80
         let content =
63 81
             fs::read_to_string(cwd.join("public").join("hello-world").join("index.html")).unwrap();
64 82
 
65 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 85
             content.replace("\n", "")
70 86
         );
71 87
 
@@ -82,7 +98,8 @@ mod tests {
82 98
         let cwd = env::current_dir().unwrap();
83 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 103
         let post_listing_template = "<ul>{{ post_listing }}</ul>";
87 104
         let post_item_template = "<li><a href=\"/{{ slug }}\">{{ title }}</a></li>";
88 105
         let posts = vec![
@@ -102,19 +119,20 @@ mod tests {
102 119
                 slug: String::from("third-post"),
103 120
             },
104 121
         ];
122
+        let config = Config {
123
+            site_name: "Test Site".to_string(),
124
+        };
105 125
         write_post_listing(
106 126
             &cwd,
107 127
             &layout,
108 128
             &post_listing_template,
109 129
             &post_item_template,
110 130
             &posts,
131
+            &config,
111 132
         );
112 133
 
113 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 136
             fs::read_to_string(&cwd.join("public").join("index.html"))
119 137
                 .unwrap()
120 138
                 .replace("\n", ""),

Loading…
Cancel
Save