bfom_lib/
utilities.rs

1pub mod processing {
2  use std::{
3    fs,
4    io::{self},
5    path::Path,
6    path::PathBuf,
7  };
8
9  pub fn file_find_markdown(dir: &Path) -> io::Result<Vec<PathBuf>> {
10    let mut markdown_files: Vec<PathBuf> = vec![];
11    if dir.is_dir() {
12      for entry in fs::read_dir(dir)? {
13        let entry = entry?;
14        let path = entry.path();
15        if path.is_dir() {
16          let sub_files = self::file_find_markdown(&path)?;
17          markdown_files.extend(sub_files);
18        } else {
19          // check if it ends with .md
20          if let Some(extension) = path.extension() {
21            if extension == "md" {
22              markdown_files.push(path)
23            }
24          }
25        }
26      }
27    }
28    Ok(markdown_files)
29  }
30
31  pub fn copy_css(src: &Path, dest: &Path) -> io::Result<()> {
32    copy_files(src, dest, &["md"], &[])
33  }
34
35  pub fn copy_files(src: &Path, dest: &Path, forbidden_files: &[&str], forbidden: &[PathBuf]) -> io::Result<()> {
36    if src.is_dir() {
37      if forbidden.contains(&(src.to_path_buf())) {
38        return Ok(());
39      }
40
41      for entry in fs::read_dir(src)? {
42        let entry = entry?;
43        let path = entry.path();
44        if path.is_dir() {
45          let mut dest_new = PathBuf::new();
46          dest_new.push(dest);
47          dest_new.push(entry.file_name());
48          self::copy_files(&path, &dest_new, forbidden_files, forbidden)?;
49        } else {
50          // check if it ends with .css
51          if let Some(extension) = path.extension() {
52            if !forbidden_files.contains(&extension.to_str().unwrap_or_default()) {
53              // grab the file
54
55              let dest = if let Ok(stripped) = path.clone().strip_prefix(src) {
56                Path::new(dest).join(stripped)
57              } else {
58                // if the prefix cannot be stripped then put in the output files beside teh in
59                // though considering that its based of of src this should never be called
60                path.clone()
61              };
62
63              // make sure it has a folder ot go into
64              if let Some(parent) = dest.parent() {
65                fs::create_dir_all(parent)?
66              }
67
68              fs::copy(&path, &dest)?;
69            }
70          }
71        }
72      }
73    }
74    Ok(())
75  }
76
77  // https://stackoverflow.com/a/38406885/11964934
78  pub fn uppercase_first_letter(s: &str) -> String {
79    let mut c = s.chars();
80    match c.next() {
81      None => String::new(),
82      Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
83    }
84  }
85}