|
| 1 | +use std::{ |
| 2 | + error::Error, |
| 3 | + fs, |
| 4 | + path::{Path, PathBuf}, |
| 5 | +}; |
| 6 | + |
| 7 | +static URL_BASE: &str = "https://github.com/TheAlgorithms/Rust/blob/master"; |
| 8 | + |
| 9 | +fn good_filepaths(top_dir: &Path) -> Result<Vec<String>, Box<dyn Error>> { |
| 10 | + let mut good_fs = Vec::new(); |
| 11 | + if top_dir.is_dir() { |
| 12 | + for entry in fs::read_dir(top_dir)? { |
| 13 | + let entry = entry?; |
| 14 | + let path = entry.path(); |
| 15 | + if entry.file_name().to_str().unwrap().starts_with('.') |
| 16 | + || entry.file_name().to_str().unwrap().starts_with('_') |
| 17 | + { |
| 18 | + continue; |
| 19 | + } |
| 20 | + if path.is_dir() { |
| 21 | + let mut other = good_filepaths(&path)?; |
| 22 | + good_fs.append(&mut other); |
| 23 | + } else if entry.file_name().to_str().unwrap().ends_with(".rs") |
| 24 | + && entry.file_name().to_str().unwrap() != "mod.rs" |
| 25 | + { |
| 26 | + good_fs.push( |
| 27 | + path.into_os_string() |
| 28 | + .into_string() |
| 29 | + .unwrap() |
| 30 | + .split_at(2) |
| 31 | + .1 |
| 32 | + .to_string(), |
| 33 | + ); |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + good_fs.sort(); |
| 38 | + Ok(good_fs) |
| 39 | +} |
| 40 | + |
| 41 | +fn md_prefix(indent_count: usize) -> String { |
| 42 | + if indent_count > 0 { |
| 43 | + format!("{}*", " ".repeat(indent_count)) |
| 44 | + } else { |
| 45 | + "\n##".to_string() |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +fn print_path(old_path: String, new_path: String) -> (String, String) { |
| 50 | + let old_parts = old_path |
| 51 | + .split(std::path::MAIN_SEPARATOR) |
| 52 | + .collect::<Vec<&str>>(); |
| 53 | + let mut result = String::new(); |
| 54 | + for (count, new_part) in new_path.split(std::path::MAIN_SEPARATOR).enumerate() { |
| 55 | + if count + 1 > old_parts.len() || old_parts[count] != new_part { |
| 56 | + println!("{} {}", md_prefix(count), to_title(new_part)); |
| 57 | + result.push_str(format!("{} {}\n", md_prefix(count), to_title(new_part)).as_str()); |
| 58 | + } |
| 59 | + } |
| 60 | + (new_path, result) |
| 61 | +} |
| 62 | + |
| 63 | +pub fn build_directory_md(top_dir: &Path) -> Result<String, Box<dyn Error>> { |
| 64 | + let mut old_path = String::from(""); |
| 65 | + let mut result = String::new(); |
| 66 | + for filepath in good_filepaths(top_dir)? { |
| 67 | + let mut filepath = PathBuf::from(filepath); |
| 68 | + let filename = filepath.file_name().unwrap().to_owned(); |
| 69 | + filepath.pop(); |
| 70 | + let filepath = filepath.into_os_string().into_string().unwrap(); |
| 71 | + if filepath != old_path { |
| 72 | + let path_res = print_path(old_path, filepath); |
| 73 | + old_path = path_res.0; |
| 74 | + result.push_str(path_res.1.as_str()); |
| 75 | + } |
| 76 | + let url = format!("{}/{}", old_path, filename.to_string_lossy()); |
| 77 | + let url = get_addr(&url); |
| 78 | + let indent = old_path.matches(std::path::MAIN_SEPARATOR).count() + 1; |
| 79 | + let filename = to_title(filename.to_str().unwrap().split('.').collect::<Vec<&str>>()[0]); |
| 80 | + println!("{} [{}]({})", md_prefix(indent), filename, url); |
| 81 | + result.push_str(format!("{} [{}]({})\n", md_prefix(indent), filename, url).as_str()); |
| 82 | + } |
| 83 | + Ok(result) |
| 84 | +} |
| 85 | + |
| 86 | +fn to_title(name: &str) -> String { |
| 87 | + let mut change = true; |
| 88 | + name.chars() |
| 89 | + .map(move |letter| { |
| 90 | + if change && !letter.is_numeric() { |
| 91 | + change = false; |
| 92 | + letter.to_uppercase().next().unwrap() |
| 93 | + } else if letter == '_' { |
| 94 | + change = true; |
| 95 | + ' ' |
| 96 | + } else { |
| 97 | + if letter.is_numeric() || !letter.is_alphanumeric() { |
| 98 | + change = true; |
| 99 | + } |
| 100 | + letter |
| 101 | + } |
| 102 | + }) |
| 103 | + .collect::<String>() |
| 104 | +} |
| 105 | + |
| 106 | +fn get_addr(addr: &str) -> String { |
| 107 | + if cfg!(windows) { |
| 108 | + format!("{}/{}", URL_BASE, switch_backslash(addr)) |
| 109 | + } else { |
| 110 | + format!("{}/{}", URL_BASE, addr) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +// Function that changes '\' to '/' (for Windows builds only) |
| 115 | +fn switch_backslash(addr: &str) -> String { |
| 116 | + addr.chars() |
| 117 | + .map(|mut symbol| { |
| 118 | + if symbol == '\\' { |
| 119 | + symbol = '/'; |
| 120 | + } |
| 121 | + symbol |
| 122 | + }) |
| 123 | + .collect::<String>() |
| 124 | +} |
0 commit comments