|
@@ -7,111 +7,16 @@ extern crate regex;
|
7
|
7
|
extern crate toml;
|
8
|
8
|
extern crate uuid;
|
9
|
9
|
|
10
|
|
-use std::{env, fs};
|
11
|
|
-
|
12
|
10
|
use clap::{App, Arg};
|
13
|
|
-use fs_extra::dir;
|
14
|
|
-use toml::Value;
|
15
|
11
|
|
16
|
|
-use config::Config;
|
17
|
|
-use post::{parse_post, read_posts_dir};
|
18
|
|
-use write::{write_post, write_post_listing};
|
|
12
|
+use commands::{build, new};
|
19
|
13
|
|
|
14
|
+mod commands;
|
20
|
15
|
mod config;
|
21
|
16
|
mod post;
|
22
|
17
|
mod render;
|
23
|
18
|
mod write;
|
24
|
19
|
|
25
|
|
-fn build() {
|
26
|
|
- let cwd = env::current_dir().expect("Couldn't read current directory");
|
27
|
|
- let config = match fs::read_to_string(cwd.join("tlon.toml")) {
|
28
|
|
- Ok(contents) => match contents.parse::<Value>() {
|
29
|
|
- Ok(config) => Config {
|
30
|
|
- site_name: String::from(config["site_name"].as_str().unwrap()),
|
31
|
|
- },
|
32
|
|
- Err(_) => panic!("Invalid tlon.toml"),
|
33
|
|
- },
|
34
|
|
- Err(_) => {
|
35
|
|
- panic!("Can't find tlon.toml");
|
36
|
|
- }
|
37
|
|
- };
|
38
|
|
-
|
39
|
|
- match fs::read_dir(cwd.join("public")) {
|
40
|
|
- Ok(_) => {
|
41
|
|
- fs::remove_dir_all(cwd.join("public")).unwrap();
|
42
|
|
- }
|
43
|
|
- Err(_) => {}
|
44
|
|
- }
|
45
|
|
-
|
46
|
|
- fs::create_dir(cwd.join("public")).expect("Couldn't create public directory");
|
47
|
|
-
|
48
|
|
- let layout_template = fs::read_to_string(&cwd.join("templates").join("layout.html"))
|
49
|
|
- .expect("Couldn't find layout template");
|
50
|
|
- let post_template = fs::read_to_string(cwd.join("templates").join("post.html"))
|
51
|
|
- .expect("Couldn't find post template");
|
52
|
|
- let post_listing_template = fs::read_to_string(cwd.join("templates").join("post_listing.html"))
|
53
|
|
- .expect("Couldn't find post listing item template");
|
54
|
|
- let post_item_template =
|
55
|
|
- fs::read_to_string(cwd.join("templates").join("post_listing_item.html"))
|
56
|
|
- .expect("Couldn't find post listing item template");
|
57
|
|
-
|
58
|
|
- let post_paths = read_posts_dir(&cwd.join("posts"));
|
59
|
|
- let posts = post_paths
|
60
|
|
- .into_iter()
|
61
|
|
- .map(|path| match path {
|
62
|
|
- Ok(p) => {
|
63
|
|
- let post = parse_post(p.path());
|
64
|
|
- write_post(&cwd, &layout_template, &post_template, &post, &config);
|
65
|
|
- post
|
66
|
|
- }
|
67
|
|
- Err(err) => panic!(err),
|
68
|
|
- }).collect();
|
69
|
|
-
|
70
|
|
- write_post_listing(
|
71
|
|
- &cwd,
|
72
|
|
- &layout_template,
|
73
|
|
- &post_listing_template,
|
74
|
|
- &post_item_template,
|
75
|
|
- &posts,
|
76
|
|
- &config,
|
77
|
|
- );
|
78
|
|
-
|
79
|
|
- fs_extra::copy_items(
|
80
|
|
- &vec![cwd.join("css"), cwd.join("js")],
|
81
|
|
- cwd.join("public"),
|
82
|
|
- &dir::CopyOptions::new(),
|
83
|
|
- ).expect("Couldn't copy css/js directories");
|
84
|
|
-}
|
85
|
|
-
|
86
|
|
-fn new(name: &str) {
|
87
|
|
- let cwd = env::current_dir().expect("Couldn't read current directory");
|
88
|
|
- let project_path = cwd.join(name);
|
89
|
|
-
|
90
|
|
- fs::create_dir(&project_path).expect(&format!("Couldn't create directory '{}'", &name));
|
91
|
|
- fs::write(
|
92
|
|
- project_path.join("tlon.toml"),
|
93
|
|
- format!("site_name = \"{}\"", &name),
|
94
|
|
- ).expect("Could not create tlon.toml");
|
95
|
|
-
|
96
|
|
- for dir in &["posts", "public", "templates", "css", "js"] {
|
97
|
|
- fs::create_dir(&project_path.join(&dir))
|
98
|
|
- .expect(&format!("Couldn't create {} directory", &dir));
|
99
|
|
- }
|
100
|
|
-
|
101
|
|
- fs::write(project_path.join("css").join("style.css"), "")
|
102
|
|
- .expect("Couldn't create css/style.css");
|
103
|
|
- fs::write(project_path.join("js").join("index.js"), "").expect("Couldn't create js/index.js");
|
104
|
|
-
|
105
|
|
- for file in &["layout", "post_listing", "post", "post_listing_item"] {
|
106
|
|
- fs::write(
|
107
|
|
- &project_path
|
108
|
|
- .join("templates")
|
109
|
|
- .join(format!("{}.html", file)),
|
110
|
|
- "",
|
111
|
|
- ).expect(&format!("Couldn't write templates/{}.html", file));
|
112
|
|
- }
|
113
|
|
-}
|
114
|
|
-
|
115
|
20
|
fn main() {
|
116
|
21
|
let matches = App::new("tlon")
|
117
|
22
|
.version("0.1.0")
|
|
@@ -135,135 +40,3 @@ fn main() {
|
135
|
40
|
new(&matches.value_of("name").unwrap());
|
136
|
41
|
}
|
137
|
42
|
}
|
138
|
|
-
|
139
|
|
-#[cfg(test)]
|
140
|
|
-mod tests {
|
141
|
|
- #[allow(unused_imports)]
|
142
|
|
- use super::*;
|
143
|
|
- #[allow(unused_imports)]
|
144
|
|
- use uuid::Uuid;
|
145
|
|
-
|
146
|
|
- #[test]
|
147
|
|
- fn test_new() {
|
148
|
|
- let temp_dir = env::temp_dir();
|
149
|
|
- env::set_current_dir(&temp_dir).unwrap();
|
150
|
|
-
|
151
|
|
- let uuid = Uuid::new_v4().to_string();
|
152
|
|
- let project_dir = temp_dir.join(&uuid);
|
153
|
|
- new(&uuid);
|
154
|
|
-
|
155
|
|
- for dir in &["public", "posts", "templates"] {
|
156
|
|
- fs::read_dir(&project_dir.join(dir)).unwrap();
|
157
|
|
- }
|
158
|
|
-
|
159
|
|
- assert_eq!(
|
160
|
|
- "",
|
161
|
|
- fs::read_to_string(&project_dir.join("templates").join("post_listing.html")).unwrap()
|
162
|
|
- );
|
163
|
|
- assert_eq!(
|
164
|
|
- "",
|
165
|
|
- fs::read_to_string(&project_dir.join("templates").join("layout.html")).unwrap()
|
166
|
|
- );
|
167
|
|
- assert_eq!(
|
168
|
|
- "",
|
169
|
|
- fs::read_to_string(&project_dir.join("templates").join("post_listing_item.html"))
|
170
|
|
- .unwrap()
|
171
|
|
- );
|
172
|
|
- assert_eq!(
|
173
|
|
- "",
|
174
|
|
- fs::read_to_string(&project_dir.join("templates").join("post.html")).unwrap()
|
175
|
|
- );
|
176
|
|
- assert_eq!(
|
177
|
|
- "",
|
178
|
|
- fs::read_to_string(&project_dir.join("css").join("style.css")).unwrap()
|
179
|
|
- );
|
180
|
|
- assert_eq!(
|
181
|
|
- "",
|
182
|
|
- fs::read_to_string(&project_dir.join("js").join("index.js")).unwrap()
|
183
|
|
- );
|
184
|
|
- assert_eq!(
|
185
|
|
- format!("site_name = \"{}\"", &uuid),
|
186
|
|
- fs::read_to_string(&project_dir.join("tlon.toml")).unwrap()
|
187
|
|
- );
|
188
|
|
-
|
189
|
|
- fs::remove_dir_all(project_dir).unwrap();
|
190
|
|
- }
|
191
|
|
-
|
192
|
|
- #[test]
|
193
|
|
- fn test_build() {
|
194
|
|
- let temp_dir = env::temp_dir();
|
195
|
|
- let uuid = Uuid::new_v4().to_string();
|
196
|
|
- let project_dir = temp_dir.join(&uuid);
|
197
|
|
- fs::create_dir(&project_dir).unwrap();
|
198
|
|
- env::set_current_dir(&project_dir).unwrap();
|
199
|
|
-
|
200
|
|
- fs::create_dir(project_dir.join("posts")).unwrap();
|
201
|
|
- fs::create_dir(project_dir.join("public")).unwrap();
|
202
|
|
- fs::create_dir(project_dir.join("templates")).unwrap();
|
203
|
|
- fs::create_dir(project_dir.join("css")).unwrap();
|
204
|
|
- fs::create_dir(project_dir.join("js")).unwrap();
|
205
|
|
-
|
206
|
|
- fs::write(
|
207
|
|
- project_dir.join("css").join("style.css"),
|
208
|
|
- "body { background: blue; }",
|
209
|
|
- ).unwrap();
|
210
|
|
- fs::write(
|
211
|
|
- project_dir.join("js").join("index.js"),
|
212
|
|
- "window.onload = function () { alert() }",
|
213
|
|
- ).unwrap();
|
214
|
|
- fs::write(
|
215
|
|
- project_dir.join("templates").join("layout.html"),
|
216
|
|
- "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>",
|
217
|
|
- ).unwrap();
|
218
|
|
- fs::write(
|
219
|
|
- project_dir.join("templates").join("post.html"),
|
220
|
|
- "<article><h1>{{ title }}</h1><div>{{ body }}</div></article>",
|
221
|
|
- ).unwrap();
|
222
|
|
- fs::write(
|
223
|
|
- project_dir.join("templates").join("post_listing.html"),
|
224
|
|
- "<ul>{{ post_listing }}</ul>",
|
225
|
|
- ).unwrap();
|
226
|
|
- fs::write(
|
227
|
|
- project_dir.join("templates").join("post_listing_item.html"),
|
228
|
|
- "<li><a href=\"/{{ slug }}\">{{ title }}</a></li>",
|
229
|
|
- ).unwrap();
|
230
|
|
- fs::write(
|
231
|
|
- project_dir.join("posts").join("first-post.md"),
|
232
|
|
- "# First post\n\nThis is the first post\n\nIt has multiple paragraphs",
|
233
|
|
- ).unwrap();
|
234
|
|
- fs::write(project_dir.join("tlon.toml"), "site_name = \"Test Site\"").unwrap();
|
235
|
|
-
|
236
|
|
- build();
|
237
|
|
-
|
238
|
|
- assert_eq!(
|
239
|
|
- "<html><head><title>Test Site</title></head><body><ul><li><a href=\"/fir\
|
240
|
|
- st-post\">First post</a></li></ul></body></html>",
|
241
|
|
- fs::read_to_string(project_dir.join("public").join("index.html")).unwrap(),
|
242
|
|
- );
|
243
|
|
-
|
244
|
|
- assert_eq!(
|
245
|
|
- "<html><head><title>First post | Test Site</title></head><body><article><h1>First pos\
|
246
|
|
- t</h1><div><p>This is the first post</p><p>It has multiple paragra\
|
247
|
|
- phs</p></div></article></body></html>",
|
248
|
|
- fs::read_to_string(
|
249
|
|
- project_dir
|
250
|
|
- .join("public")
|
251
|
|
- .join("first-post")
|
252
|
|
- .join("index.html")
|
253
|
|
- ).unwrap()
|
254
|
|
- .replace("\n", ""),
|
255
|
|
- );
|
256
|
|
-
|
257
|
|
- assert_eq!(
|
258
|
|
- "body { background: blue; }",
|
259
|
|
- fs::read_to_string(project_dir.join("public").join("css").join("style.css")).unwrap()
|
260
|
|
- );
|
261
|
|
-
|
262
|
|
- assert_eq!(
|
263
|
|
- "window.onload = function () { alert() }",
|
264
|
|
- fs::read_to_string(project_dir.join("public").join("js").join("index.js")).unwrap()
|
265
|
|
- );
|
266
|
|
-
|
267
|
|
- fs::remove_dir_all(project_dir).unwrap();
|
268
|
|
- }
|
269
|
|
-}
|