|
@@ -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,
|