This commit is contained in:
Jonas Zeunert
2024-04-19 20:19:16 +02:00
parent 299cdedd83
commit 9234a4e2d0
3 changed files with 1008 additions and 0 deletions

91
src/main.rs Normal file
View File

@@ -0,0 +1,91 @@
use clap::Parser;
use regex::Regex;
use std::{fs, path::PathBuf};
use termimad::crossterm::style::Color::*;
use termimad::*;
use walkdir::{DirEntry, WalkDir};
use futures::future;
use tokio::task;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
// input directory
input_dir: PathBuf,
#[clap(short, long, default_value = "out")]
output_dir: PathBuf,
}
fn remove_html(input: &str) -> String {
let regex: Regex = Regex::new(r"<.*>").unwrap();
regex.replace_all(input, "").into_owned()
}
fn replace_links(input: &str) -> String {
let intermediate = input.replace("[", "**");
intermediate.replace("]", "** ")
}
fn render(input: &str) -> String {
let mut skin = MadSkin::default();
skin.set_headers_fg(rgb(255, 187, 0));
skin.bold.set_fg(Cyan);
skin.italic.set_fgbg(Magenta, rgb(30, 30, 40));
skin.bullet = StyledChar::from_fg_char(Yellow, '⟡');
skin.bullet.set_bg(Blue);
skin.quote_mark.set_fg(Yellow);
skin.paragraph.set_fg(Blue);
skin.term_text(input).to_string()
}
async fn process_file(file: DirEntry, output_dir: PathBuf) {
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);
}
fn write_file(output_dir: PathBuf, file: DirEntry, rendered: &str) {
let basename = file
.path()
.parent()
.unwrap()
.into_iter()
.last()
.unwrap()
.to_str()
.unwrap()
.to_string();
let name = basename.replace("awesome", "").replace("-", "");
let path = output_dir.clone().join(name);
fs::write(path, rendered).expect("Error writing file!");
}
#[tokio::main]
async fn main() {
let args = Args::parse();
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 tasks: Vec<task::JoinHandle<_>> = 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())))
.collect();
let _ = future::try_join_all(tasks).await;
}