1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#![crate_name = "mkhtmllib"]
extern crate fs_extra;
extern crate walkdir;
use fs_extra::dir::{copy, CopyOptions};
use std::env::current_dir;
use std::fs::{create_dir, remove_dir_all, File};
use std::io::{Read, Write};
use std::path::PathBuf;
use walkdir::WalkDir;
use Error::{CopyFailed, ReadFailed, RemoveFailed, WriteFailed};
pub fn mkhtml(config: Config) -> Result<(), Error> {
if config.clone().get_build_dir().is_dir() {
let rm = remove_dir_all(config.clone().get_build_dir());
if rm.is_err() {
return Err(RemoveFailed);
}
}
for d in config.clone().iter() {
chk_dir(d)?;
}
let files = WalkDir::new(config.clone().get_pages_dir()).follow_links(true);
for file in files {
let f = file.unwrap();
if f.path().is_dir() {
let base_path = f.path().as_os_str().to_os_string().into_string().unwrap();
let from = config.clone().get_pages_dir().canonicalize().unwrap().into_os_string().into_string().unwrap();
let to = config.clone().get_pages_dir().canonicalize().unwrap().into_os_string().into_string().unwrap();
let final_path = str::replace(&base_path, &from, &to);
chk_dir(PathBuf::from(final_path))?;
} else {
let watermark_str =
"<!-- Built with mkhtml 3 (https://github.com/jusdepatate/mkhtml) -->".to_string();
let base_path = f.path().as_os_str().to_os_string().into_string().unwrap();
let from = config.clone().get_pages_dir().canonicalize().unwrap().into_os_string().into_string().unwrap();
let to = config.clone().get_build_dir().canonicalize().unwrap().into_os_string().into_string().unwrap();
let final_path = str::replace(&base_path, &from, &to);
let header = read_file(config.clone().get_parts_dir().join("header.html"))?;
let footer = read_file(config.clone().get_parts_dir().join("footer.html"))?;
let file_body = watermark_str + "\n" +
&*header + "\n" +
&*read_file(PathBuf::from(base_path))? + "\n" +
&*footer;
write_file(PathBuf::from(final_path.clone()), file_body)?;
};
};
let copy = copy(config.clone().get_static_dir(), config.clone().get_build_dir(), &CopyOptions::new());
if copy.is_err() {
return Err(CopyFailed)
}
const VERSION: &str = env!("CARGO_PKG_VERSION");
if VERSION == "dry" {
remove_dir_all(config.get_build_dir()).unwrap();
}
Ok(())
}
fn write_file(path: PathBuf, content: String) -> Result<(), Error> {
let create = File::create(&path);
if create.is_err() {
return Err(WriteFailed)
}
let mut file = create.unwrap();
let write = file.write_all(content.as_bytes());
if write.is_err() {
return Err(WriteFailed)
}
Ok(())
}
fn read_file(path: PathBuf) -> Result<String, Error> {
let open = File::open(path);
if open.is_err() {
return Err(ReadFailed)
}
let mut file = open.unwrap();
let mut content = "".to_string();
return match file.read_to_string(&mut content) {
Ok(_) => Ok(content),
Err(_) => Err(ReadFailed),
}
}
fn chk_dir(path: PathBuf) -> Result<(), Error>{
if !path.is_dir() {
let create = create_dir(path);
if create.is_err() {
return Err(WriteFailed);
}
}
Ok(())
}
#[derive(Debug)]
pub enum Error {
WriteFailed,
RemoveFailed,
CopyFailed,
ReadFailed,
}
#[derive(Clone)]
pub struct Config {
pages_dir: PathBuf,
parts_dir: PathBuf,
static_dir: PathBuf,
build_dir: PathBuf,
}
impl Config {
pub fn new() -> Config {
let cwd = current_dir().unwrap();
Config {
pages_dir: cwd.join("pages"),
parts_dir: cwd.join("parts"),
static_dir: cwd.join("static"),
build_dir: cwd.join("builds"),
}
}
pub fn iter(self) -> [PathBuf; 4] {
return [self.pages_dir, self.parts_dir, self.static_dir, self.build_dir]
}
pub fn get_pages_dir(self) -> PathBuf { return self.pages_dir }
pub fn get_parts_dir(self) -> PathBuf { return self.parts_dir }
pub fn get_static_dir(self) -> PathBuf { return self.static_dir }
pub fn get_build_dir(self) -> PathBuf { return self.build_dir }
pub fn set_pages_dir(&mut self, path: PathBuf) { self.pages_dir = path }
pub fn set_parts_dir(&mut self, path: PathBuf) { self.parts_dir = path }
pub fn set_static_dir(&mut self, path: PathBuf) { self.static_dir = path }
pub fn set_build_dir(&mut self, path: PathBuf) { self.build_dir = path }
}
#[cfg(test)]
mod tests {
use {chk_dir, read_file, write_file};
use std::env::current_dir;
use std::fs::remove_file;
use std::path::PathBuf;
use walkdir::WalkDir;
#[test]
fn test_write_file() {
write_file(current_dir().unwrap().join("a"), "test".to_string()).unwrap();
remove_file(current_dir().unwrap().join("a")).unwrap();
}
#[test]
#[should_panic]
fn test_write_file_panic() {
write_file(PathBuf::from("/"), "test".to_string()).unwrap();
}
#[test]
fn test_read_file() {
for file in WalkDir::new("..").into_iter().filter_map(|file| file.ok()) {
if file.metadata().unwrap().is_file() {
read_file(PathBuf::from(file.path())).unwrap();
return
}
}
}
#[test]
#[should_panic]
fn test_read_file_panic() {
read_file(PathBuf::from("/")).unwrap();
}
#[test]
fn test_chk_dir() {
chk_dir(current_dir().unwrap()).unwrap();
}
#[test]
#[should_panic]
fn test_chk_dir_panic() {
chk_dir(PathBuf::from("/b3VpCg==/")).unwrap();
}
}