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
pub mod processing {
    use std::{
        fs,
        io::{self},
        path::Path,
        path::PathBuf,
    };

    pub fn file_find_markdown(dir: &Path) -> io::Result<Vec<PathBuf>> {
        let mut markdown_files: Vec<PathBuf> = vec![];
        if dir.is_dir() {
            for entry in fs::read_dir(dir)? {
                let entry = entry?;
                let path = entry.path();
                if path.is_dir() {
                    let sub_files = self::file_find_markdown(&path)?;
                    markdown_files.extend(sub_files);
                } else {
                    // check if it ends with .md
                    if let Some(extension) = path.extension() {
                        if extension == "md" {
                            markdown_files.push(path)
                        }
                    }
                }
            }
        }
        Ok(markdown_files)
    }

    pub fn copy_css(src: &Path, dest: &Path) -> io::Result<()> {
        copy_files(src, dest, &["css"], &[])
    }

    pub fn copy_files(src: &Path, dest: &Path, filetype: &[&str], forbidden: &[&Path]) -> io::Result<()> {
        if src.is_dir() {
            if forbidden.contains(&src) {
                return Ok(());
            }

            for entry in fs::read_dir(src)? {
                let entry = entry?;
                let path = entry.path();
                if path.is_dir() {
                    let mut dest_new = PathBuf::new();
                    dest_new.push(dest);
                    dest_new.push(entry.file_name());
                    self::copy_files(&path, &dest_new, filetype, forbidden)?;
                } else {
                    // check if it ends with .css
                    if let Some(extension) = path.extension() {
                        if filetype.contains(&extension.to_str().unwrap_or_default()) {
                            // grab the file

                            let dest = if let Ok(stripped) = path.clone().strip_prefix(src) {
                                Path::new(dest).join(stripped)
                            } else {
                                // if the prefix cannot be stripped then put in the output files beside teh in
                                // though considering that its based of of src this should never be called
                                path.clone()
                            };

                            // make sure it has a folder ot go into
                            if let Some(parent) = dest.parent() {
                                fs::create_dir_all(parent)?
                            }

                            fs::copy(&path, &dest)?;
                        }
                    }
                }
            }
        }
        Ok(())
    }

    // https://stackoverflow.com/a/38406885/11964934
    pub fn uppercase_first_letter(s: &str) -> String {
        let mut c = s.chars();
        match c.next() {
            None => String::new(),
            Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
        }
    }
}