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
extern crate walkdir;
extern crate fs_extra;

use std::{env, fs};
use std::fs::{File, remove_dir_all};
use std::io::{Read, Write};
use std::path::Path;
use fs_extra::dir;
use walkdir::WalkDir;

pub fn mkhtml(config: Config) {
    if Path::new(&config.build_dir).is_dir() {
        remove_dir_all(config.build_dir.clone())
            .expect("Oops! mkhtml couldn't clean build_dir because an error was dropped.");
    }

    for d in [config.pages_dir.clone(), config.parts_dir.clone(), config.static_dir.clone(), config.build_dir.clone()] {
        // for every paths we need
        chk_dir(d);
        // check if it exists, if not, create directory
    };

    let files = WalkDir::new(config.pages_dir).follow_links(true);
    // list files in pages_dir

    for file in files {
        let f = file.unwrap();

        if f.path().is_dir() {
            // for every directory in pages_dir (recursive)

            let base_path = f.path().as_os_str().to_os_string().into_string().unwrap();
            let final_path = str::replace(&base_path, "pages", "builds");
            // create path strings and build final_path from path in pages_dir

            chk_dir(final_path);
            // create path to check in build_dir & checks it
        } else {
            // for every files in pages_dir (recursive)
            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 final_path = str::replace(&base_path, "pages", "builds");
            // create path strings and build final_path from path in pages_dir

            let header = read_file(config.parts_dir.clone() + "/header.html");
            let footer = read_file(config.parts_dir.clone() + "/footer.html");
            // read header and footer files

            let file_body = watermark_str + "\n" +
                &*header + "\n" +
                &*read_file(base_path) + "\n" +
                &*footer;
            // create file body using watermark, header, page content and footer

            write_file(final_path.clone(), file_body);
            // write content to file
        };
    };

    match dir::copy(config.static_dir.clone(), config.build_dir.clone(), &dir::CopyOptions::new()) {
        Err(err) => panic!("Oops! mkhtml couldn't copy {} because an error was dropped:\n{}", config.static_dir, err),
        Ok(_) => (),
    };

    const VERSION: &str = env!("CARGO_PKG_VERSION");
    if VERSION == "dry" {
        remove_dir_all(config.build_dir.clone()).unwrap();
    }
}

fn write_file(path_str: String, content: String) {
    let path = Path::new(&path_str);

    let mut file = match File::create(&path) {
        Err(err) => panic!("Oops! mkhtml couldn't create {} because an error was dropped:\n{}", path_str, err),
        Ok(file) => file,
    };
    // try to create file, handle errors

    match file.write_all(content.as_bytes()) {
        Err(why) => panic!("couldn't write to {} because an error was dropped:\n{}", path_str, why),
        Ok(_) => (),
    };
    // try to write to file, handle errors
}

fn read_file(path_str: String) -> String {
    let path = Path::new(&path_str);

    let mut file = match File::open(&path) {
        Ok(file) => file,
        Err(err) => panic!("Oops! mkhtml couldn't read {} because an error was dropped:\n{}", path_str, err),
    };
    // try to open file, handle errors

    let mut content = "".to_string();
    match file.read_to_string(&mut content) {
        Ok(_) => return content,
        Err(err) => panic!("Oops! mkhtml couldn't read {} because an error was dropped:\n{}", path_str, err),
    };
    // try to read file, handle errors
}

fn chk_dir(path_str: String) {
    let path = Path::new(&path_str);

    if ! path.is_dir() {
        // if path doesn't exist
        match fs::create_dir(path) {
            Ok(_) => return,
            Err(err) => panic!("Oops! mkhtml couldn't create {} because an error was dropped:\n{}", path_str, err),
        };
        // create directory,  handle errors
    };
}

pub struct Config {
    pub pages_dir: String,
    pub parts_dir: String,
    pub static_dir: String,
    pub build_dir: String,
    // Configuration Structure
}

impl Config {
    pub fn new() -> Config {
        let cwd = env::current_dir().unwrap().into_os_string().into_string().unwrap();
        // get execution dir and turns it into a string

        Config {
            pages_dir: cwd.clone() + "/pages",
            parts_dir: cwd.clone() + "/parts",
            static_dir: cwd.clone() + "/static",
            build_dir: cwd + "/builds"
        }
        // Build default configuration
    }
}

#[cfg(test)]
mod tests {

    use std::env;
    use walkdir::WalkDir;
    use ::{read_file, write_file};
    use ::{chk_dir};

    #[test]
    fn test_write_file() {
        write_file(env::current_exe().unwrap().into_os_string().into_string().unwrap() + ".1", "test".to_string());
    }

    #[test]
    #[should_panic]
    fn test_write_file_panic() {
        write_file("/".to_string(), "test".to_string());
    }

    #[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(file.path().canonicalize().unwrap().into_os_string().into_string().unwrap());
                return
            }
        }
    }

    #[test]
    #[should_panic]
    fn test_read_file_panic() {
        read_file("/".to_string());
    }

    #[test]
    fn test_chk_dir() {
        chk_dir(env::current_dir().unwrap().into_os_string().into_string().unwrap());
    }

    #[test]
    #[should_panic]
    fn test_chk_dir_panic() {
        chk_dir("/b3VpCg==/".to_string());
    }
}