diff options
| author | veradec <glitchmill.dyj3d@simplelogin.co> | 2026-11-01 13:10:00 +0530 |
|---|---|---|
| committer | veradec <glitchmill.dyj3d@simplelogin.co> | 2026-11-01 13:10:00 +0530 |
| commit | 9e992287e9f2ccc061edb3d2b7571abce7c503d1 (patch) | |
| tree | 48b914c5161416a93fce0798e40f6404979a9518 /src/main.rs | |
| parent | c392e6f999ba19f97e4022840bc6b3561fda8798 (diff) | |
add: dir lists is complete
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 97 |
1 files changed, 66 insertions, 31 deletions
diff --git a/src/main.rs b/src/main.rs index 45cd7ca..d94c4d5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,8 +5,18 @@ use std::env; use std::fs; use std::fs::File; use std::path::Path; +use std::path::PathBuf; use walkdir::WalkDir; +macro_rules! string_to_bool { + ($str:expr) => { + match &$str.to_lowercase().as_str()[0..4] { + "true" => true, + _ => false, + } + }; +} + struct HTMLFile { file_name: String, date: String, @@ -17,19 +27,9 @@ struct HTMLFile { content: String, } -fn string_to_bool(str: &str) -> bool { - let sliced = &str[0..4]; - if sliced == "true" { - return true; - } - false -} - fn meta_data_extractor(content: &str) -> (String, String, String, bool, bool) { let lines = content.lines(); - let mut date: String = "".to_string(); - let mut title: String = "".to_string(); - let mut desc: String = "".to_string(); + let (mut date, mut title, mut desc) = (String::new(), String::new(), String::new()); let mut toc: bool = true; let mut header: bool = true; for line in lines { @@ -38,8 +38,8 @@ fn meta_data_extractor(content: &str) -> (String, String, String, bool, bool) { "date" => date = meta_data[1].to_string(), "title" => title = meta_data[1].to_string(), "description" => desc = meta_data[1].to_string(), - "toc" => toc = string_to_bool(meta_data[1].to_string().trim_start()), - "header" => header = string_to_bool(meta_data[1].to_string().trim_start()), + "toc" => toc = string_to_bool!(meta_data[1].to_string().trim_start()), + "header" => header = string_to_bool!(meta_data[1].to_string().trim_start()), "" => (), &_ => println!( "Header not found |{}|", @@ -65,12 +65,7 @@ fn markdown_to_html(path: String, content: String) -> HTMLFile { let end = end_wrapped.unwrap(); let meta_data = &content[3..end + 2]; - let mut date: String; - let mut title: String; - let mut description: String; - let mut toc: bool; - let mut header: bool; - (date, title, description, toc, header) = meta_data_extractor(meta_data); + let (date, title, description, toc, header) = meta_data_extractor(meta_data); let mut html = markdown::to_html(&content[end + 7..]); @@ -106,15 +101,11 @@ fn markdown_to_html(path: String, content: String) -> HTMLFile { }; } -fn get_html_from_file(path: &Path) -> HTMLFile { +fn write_html_from_md(path: &Path) { let contents_read = fs::read_to_string(path).unwrap(); let f: HTMLFile = markdown_to_html(path.display().to_string(), contents_read); - f -} - -fn write_content_to_file(f: HTMLFile) { - let file_path = f.file_name; - let path = Path::new(&file_path); + println!("{}", f.file_name); + let path = Path::new(&f.file_name); if let Some(parent) = path.parent() { fs::create_dir_all(parent); } @@ -135,16 +126,60 @@ fn copy_dir(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> std::io::Result<()> Ok(()) } -fn main() -> Result<()> { - // copy static folder to public folder - copy_dir(Path::new("static"), Path::new("public")); - ////// +fn generate_lists(dir: PathBuf) -> std::io::Result<()> { + let mut dir_names: Vec<String> = vec![]; + for entry in fs::read_dir(&dir)? { + let entry = entry?; + if entry.path().to_string_lossy().ends_with("index.html") { + continue; + } else { + generate_lists(entry.path()); + } + dir_names.push(entry.path().to_string_lossy().to_string()); + } + + // there is a index.html, then skip + // read all the directories inside the directory and save it to index.html + if dir_names.len() > 0 { + if &dir.to_string_lossy() != "public/" { + // println!("{:?} - {:?}", &dir.to_string_lossy(), dir_names); + vec2html(dir.to_string_lossy().to_string(), dir_names); + } + } + Ok(()) +} +fn vec2html(mut path: String, dir_names: Vec<String>) { + path = path + "/index.html"; + let mut content = String::new(); + println!("File: {}", path); + content += "<ul>\n"; + + for dir in dir_names { + let names = dir.split("/").collect::<Vec<&str>>(); + content += &"<li><a href={dir}>{name}</a></li>\n" + .replace("{dir}", &dir[6..]) + .replace("{name}", &names[names.len() - 1].replace("-", " ")); + } + content += "</ul>"; + content = fs::read_to_string("templates/list.html") + .unwrap() + .replace("{{content}}", &content); + fs::write(path, content); +} + +fn main() -> Result<()> { for file in WalkDir::new("content/").into_iter().filter_map(|e| e.ok()) { let file_name = file.file_name().to_string_lossy(); if file_name.ends_with(".md") { - write_content_to_file(get_html_from_file(file.path())); + write_html_from_md(file.path()); } } + + generate_lists("public/".into()); + + // copy static folder to public folder + copy_dir(Path::new("static"), Path::new("public")); + Ok(()) } |
