diff --git a/src/main.rs b/src/main.rs index 6e1324d..18cf96a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,8 +14,10 @@ struct Args { // input directory input_dir: PathBuf, - #[clap(short, long, default_value = "out")] - output_dir: PathBuf, + #[clap(short, long, default_value = "terminal")] + render_dir: PathBuf, + #[clap(short, long, default_value = "readmes")] + readme_dir: PathBuf, } fn remove_html(input: &str) -> String { @@ -42,17 +44,29 @@ fn render(input: &str) -> String { skin.term_text(input).to_string() } -async fn process_file(file: DirEntry, output_dir: PathBuf) -> Result<(), std::io::Error> { +async fn process_file( + file: DirEntry, + output_dir: PathBuf, + readme_dir: PathBuf, +) -> Result<(), std::io::Error> { let markdown: &str = &std::fs::read_to_string(file.path()).unwrap(); let html_stripped = &remove_html(markdown); let links_replaced = &replace_links(html_stripped); let rendered = render(links_replaced); - write_file(output_dir, file, &rendered) + copy_readme(&file, readme_dir); + write_file(output_dir, &file, &rendered) } -fn write_file(output_dir: PathBuf, file: DirEntry, rendered: &str) -> Result<(), std::io::Error> { +fn copy_readme(file: &DirEntry, readme_dir: PathBuf) { + let file_name = get_file_name(&file); + let readme_path = get_output_path(readme_dir, &file_name); + + let _ = std::fs::copy(file.path(), readme_path); +} + +fn get_file_name(file: &DirEntry) -> String { let basename = file .path() .parent() @@ -63,15 +77,23 @@ fn write_file(output_dir: PathBuf, file: DirEntry, rendered: &str) -> Result<(), .to_str() .unwrap() .to_string(); - let name = basename.replace("awesome", "").replace("-", ""); + basename.replace("awesome", "").replace("-", "") +} - let mut path = output_dir.clone().join(name.clone()); +fn get_output_path(output_dir: PathBuf, file_name: &String) -> PathBuf { + let mut path = output_dir.clone().join(file_name.clone()); let mut i = 2; while path.exists() { - path.set_file_name(name.clone() + &i.to_string()); + path.set_file_name(file_name.clone() + &i.to_string()); i = i + 1; } + path +} + +fn write_file(output_dir: PathBuf, file: &DirEntry, rendered: &str) -> Result<(), std::io::Error> { + let file_name = get_file_name(&file); + let path = get_output_path(output_dir, &file_name); fs::write(path, rendered) } @@ -82,15 +104,18 @@ async fn main() { let input_path = args.input_dir; - let output_path = args.output_dir; - fs::create_dir_all(output_path.clone()).expect("Could not create directory"); + let render_path = args.render_dir; + fs::create_dir_all(render_path.clone()).expect("Could not create directory"); + + let readme_path = args.readme_dir; + fs::create_dir_all(readme_path.clone()).expect("Could not create directory"); let tasks: Vec> = WalkDir::new(input_path) .max_depth(2) .into_iter() .filter_map(|e| e.ok()) .filter(|file| file.file_name() == "README.md") - .map(|file| task::spawn(process_file(file, output_path.clone()))) + .map(|file| task::spawn(process_file(file, render_path.clone(), readme_path.clone()))) .collect(); let _ = future::try_join_all(tasks).await;