Browse Source

Split into modules

master
Dylan Baker 5 years ago
parent
commit
87cba00bef
4 changed files with 101 additions and 86 deletions
  1. 7
    86
      src/main.rs
  2. 31
    0
      src/post.rs
  3. 32
    0
      src/render.rs
  4. 31
    0
      src/write.rs

+ 7
- 86
src/main.rs View File

@@ -8,34 +8,13 @@ use std::fs;
8 8
 use std::path;
9 9
 
10 10
 use clap::{App, Arg};
11
-use regex::Regex;
12 11
 
13
-#[derive(Debug)]
14
-struct Post {
15
-    title: String,
16
-    body: String,
17
-    slug: String,
18
-}
19
-
20
-fn parse_post(path: path::PathBuf) -> Post {
21
-    let contents = fs::read_to_string(&path).expect("Couldn't read post file");
22
-
23
-    lazy_static! {
24
-        static ref re: Regex = Regex::new(r"^# (?P<title>.*)\n\n(?P<body>.*)").unwrap();
25
-        static ref slug_re: Regex = Regex::new(r"(?P<slug>\S+).md").unwrap();
26
-    }
12
+use post::parse_post;
13
+use write::{write_post, write_post_listing};
27 14
 
28
-    let title = &re.captures(&contents).expect("Couldn't parse title")["title"];
29
-    let body = &re.captures(&contents).expect("Couldn't parse body")["body"];
30
-
31
-    let filename = &path.file_name().unwrap().to_str().unwrap();
32
-    let slug = &slug_re.captures(filename).expect("Couldn't parse slug")["slug"];
33
-    Post {
34
-        title: String::from(title),
35
-        body: String::from(body),
36
-        slug: String::from(slug),
37
-    }
38
-}
15
+mod post;
16
+mod render;
17
+mod write;
39 18
 
40 19
 fn read_posts_dir(cwd: &path::PathBuf) -> fs::ReadDir {
41 20
     match fs::read_dir(cwd) {
@@ -44,64 +23,6 @@ fn read_posts_dir(cwd: &path::PathBuf) -> fs::ReadDir {
44 23
     }
45 24
 }
46 25
 
47
-fn generate_post(layout: &str, post_template: &str, post: &Post) -> String {
48
-    layout.replace(
49
-        "{{ contents }}",
50
-        &post_template
51
-            .replace("{{ title }}", &post.title)
52
-            .replace("{{ body }}", &post.body),
53
-    )
54
-}
55
-
56
-fn render_post(cwd: &path::PathBuf, layout: &str, post_template: &str, post: &Post) {
57
-    match fs::create_dir(cwd.join("public").join(&post.slug)) {
58
-        Ok(_) => {}
59
-        Err(err) => match err.kind() {
60
-            std::io::ErrorKind::AlreadyExists => {}
61
-            _ => panic!(err),
62
-        },
63
-    }
64
-    fs::write(
65
-        cwd.join("public").join(&post.slug).join("index.html"),
66
-        generate_post(layout, post_template, post),
67
-    ).expect("Unable to write file");
68
-}
69
-
70
-fn generate_post_listing(
71
-    layout: &str,
72
-    post_listing_template: &str,
73
-    post_item_template: &str,
74
-    posts: &Vec<Post>,
75
-) -> String {
76
-    layout.replace(
77
-        "{{ contents }}",
78
-        &post_listing_template.replace(
79
-            "{{ post_listing }}",
80
-            &posts
81
-                .iter()
82
-                .map(|ref post| {
83
-                    post_item_template
84
-                        .replace("{{ slug }}", &post.slug)
85
-                        .replace("{{ title }}", &post.title)
86
-                }).collect::<Vec<String>>()
87
-                .join("\n"),
88
-        ),
89
-    )
90
-}
91
-
92
-fn render_post_listing(
93
-    cwd: &path::PathBuf,
94
-    layout: &str,
95
-    post_listing_template: &str,
96
-    post_item_template: &str,
97
-    posts: &Vec<Post>,
98
-) {
99
-    fs::write(
100
-        cwd.join("public").join("index.html"),
101
-        generate_post_listing(layout, post_listing_template, post_item_template, posts),
102
-    ).expect("Unable to write file");
103
-}
104
-
105 26
 fn build() {
106 27
     let cwd = env::current_dir().expect("Couldn't read current directory");
107 28
 
@@ -129,13 +50,13 @@ fn build() {
129 50
         .map(|path| match path {
130 51
             Ok(p) => {
131 52
                 let post = parse_post(p.path());
132
-                render_post(&cwd, &layout_template, &post_template, &post);
53
+                write_post(&cwd, &layout_template, &post_template, &post);
133 54
                 post
134 55
             }
135 56
             Err(err) => panic!(err),
136 57
         }).collect();
137 58
 
138
-    render_post_listing(
59
+    write_post_listing(
139 60
         &cwd,
140 61
         &layout_template,
141 62
         &post_listing_template,

+ 31
- 0
src/post.rs View File

@@ -0,0 +1,31 @@
1
+use std::fs;
2
+use std::path;
3
+
4
+use regex::Regex;
5
+
6
+#[derive(Debug)]
7
+pub struct Post {
8
+    pub title: String,
9
+    pub body: String,
10
+    pub slug: String,
11
+}
12
+
13
+pub fn parse_post(path: path::PathBuf) -> Post {
14
+    let contents = fs::read_to_string(&path).expect("Couldn't read post file");
15
+
16
+    lazy_static! {
17
+        static ref re: Regex = Regex::new(r"^# (?P<title>.*)\n\n(?P<body>.*)").unwrap();
18
+        static ref slug_re: Regex = Regex::new(r"(?P<slug>\S+).md").unwrap();
19
+    }
20
+
21
+    let title = &re.captures(&contents).expect("Couldn't parse title")["title"];
22
+    let body = &re.captures(&contents).expect("Couldn't parse body")["body"];
23
+
24
+    let filename = &path.file_name().unwrap().to_str().unwrap();
25
+    let slug = &slug_re.captures(filename).expect("Couldn't parse slug")["slug"];
26
+    Post {
27
+        title: String::from(title),
28
+        body: String::from(body),
29
+        slug: String::from(slug),
30
+    }
31
+}

+ 32
- 0
src/render.rs View File

@@ -0,0 +1,32 @@
1
+use post::Post;
2
+
3
+pub fn render_post(layout: &str, post_template: &str, post: &Post) -> String {
4
+    layout.replace(
5
+        "{{ contents }}",
6
+        &post_template
7
+            .replace("{{ title }}", &post.title)
8
+            .replace("{{ body }}", &post.body),
9
+    )
10
+}
11
+
12
+pub fn render_post_listing(
13
+    layout: &str,
14
+    post_listing_template: &str,
15
+    post_item_template: &str,
16
+    posts: &Vec<Post>,
17
+) -> String {
18
+    layout.replace(
19
+        "{{ contents }}",
20
+        &post_listing_template.replace(
21
+            "{{ post_listing }}",
22
+            &posts
23
+                .iter()
24
+                .map(|ref post| {
25
+                    post_item_template
26
+                        .replace("{{ slug }}", &post.slug)
27
+                        .replace("{{ title }}", &post.title)
28
+                }).collect::<Vec<String>>()
29
+                .join("\n"),
30
+        ),
31
+    )
32
+}

+ 31
- 0
src/write.rs View File

@@ -0,0 +1,31 @@
1
+use post::Post;
2
+use render::{render_post, render_post_listing};
3
+use std::fs;
4
+use std::path;
5
+
6
+pub fn write_post(cwd: &path::PathBuf, layout: &str, post_template: &str, post: &Post) {
7
+    match fs::create_dir(cwd.join("public").join(&post.slug)) {
8
+        Ok(_) => {}
9
+        Err(err) => match err.kind() {
10
+            std::io::ErrorKind::AlreadyExists => {}
11
+            _ => panic!(err),
12
+        },
13
+    }
14
+    fs::write(
15
+        cwd.join("public").join(&post.slug).join("index.html"),
16
+        render_post(layout, post_template, post),
17
+    ).expect("Unable to write file");
18
+}
19
+
20
+pub fn write_post_listing(
21
+    cwd: &path::PathBuf,
22
+    layout: &str,
23
+    post_listing_template: &str,
24
+    post_item_template: &str,
25
+    posts: &Vec<Post>,
26
+) {
27
+    fs::write(
28
+        cwd.join("public").join("index.html"),
29
+        render_post_listing(layout, post_listing_template, post_item_template, posts),
30
+    ).expect("Unable to write file");
31
+}

Loading…
Cancel
Save