Copy original readme to readme_dir
This commit is contained in:
47
src/main.rs
47
src/main.rs
@@ -14,8 +14,10 @@ struct Args {
|
|||||||
// input directory
|
// input directory
|
||||||
input_dir: PathBuf,
|
input_dir: PathBuf,
|
||||||
|
|
||||||
#[clap(short, long, default_value = "out")]
|
#[clap(short, long, default_value = "terminal")]
|
||||||
output_dir: PathBuf,
|
render_dir: PathBuf,
|
||||||
|
#[clap(short, long, default_value = "readmes")]
|
||||||
|
readme_dir: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_html(input: &str) -> String {
|
fn remove_html(input: &str) -> String {
|
||||||
@@ -42,17 +44,29 @@ fn render(input: &str) -> String {
|
|||||||
skin.term_text(input).to_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 markdown: &str = &std::fs::read_to_string(file.path()).unwrap();
|
||||||
|
|
||||||
let html_stripped = &remove_html(markdown);
|
let html_stripped = &remove_html(markdown);
|
||||||
let links_replaced = &replace_links(html_stripped);
|
let links_replaced = &replace_links(html_stripped);
|
||||||
|
|
||||||
let rendered = render(links_replaced);
|
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
|
let basename = file
|
||||||
.path()
|
.path()
|
||||||
.parent()
|
.parent()
|
||||||
@@ -63,15 +77,23 @@ fn write_file(output_dir: PathBuf, file: DirEntry, rendered: &str) -> Result<(),
|
|||||||
.to_str()
|
.to_str()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.to_string();
|
.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;
|
let mut i = 2;
|
||||||
while path.exists() {
|
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;
|
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)
|
fs::write(path, rendered)
|
||||||
}
|
}
|
||||||
@@ -82,15 +104,18 @@ async fn main() {
|
|||||||
|
|
||||||
let input_path = args.input_dir;
|
let input_path = args.input_dir;
|
||||||
|
|
||||||
let output_path = args.output_dir;
|
let render_path = args.render_dir;
|
||||||
fs::create_dir_all(output_path.clone()).expect("Could not create directory");
|
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<task::JoinHandle<_>> = WalkDir::new(input_path)
|
let tasks: Vec<task::JoinHandle<_>> = WalkDir::new(input_path)
|
||||||
.max_depth(2)
|
.max_depth(2)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|e| e.ok())
|
.filter_map(|e| e.ok())
|
||||||
.filter(|file| file.file_name() == "README.md")
|
.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();
|
.collect();
|
||||||
|
|
||||||
let _ = future::try_join_all(tasks).await;
|
let _ = future::try_join_all(tasks).await;
|
||||||
|
|||||||
Reference in New Issue
Block a user