// pull the std into scope and inline it so that we get documentation for it, // even when running offline #[doc(inline)] pub use std; use std::path::{Path, PathBuf}; use std::io::{self, Read, Write}; use std::fs::{self, File}; use std::time; fn main() -> std::io::Result<()> { // the input and output pathes let in_path = Path::new("../emile.space/in").to_path_buf(); let out_path = Path::new("../emile.space/out").to_path_buf(); println!("inpath: {}", in_path.display()); println!("outpath: {}", out_path.display()); // read the style let mut style_file = File::open("./style.css")?; let mut style = String::new(); style_file.read_to_string(&mut style)?; // read all dirs in the input path let pathes = recursive_read_dir(&in_path, false)?; println!("---"); for path in pathes { println!("\n"); println!("[i] {}", path.as_os_str().to_str().unwrap()); let stripped_path = path.strip_prefix(&in_path) .expect(format!( "could not strip the in_path prefix: {:?}", in_path).as_str()); // copy images and other files to the output folder if path.is_file() { // define the source and destination let src = Path::new(&in_path).join(stripped_path); let dst = Path::new(&out_path).join(stripped_path); // define the destination folder (the dst path without the file) and // create it let mut dst_folder = dst.clone(); dst_folder.pop(); fs::create_dir_all(dst_folder)?; // copy the file to the destination std::fs::copy(src, dst.as_path())?; } if stripped_path.ends_with("README.md") { println!("\tstripped_path: {:?}", stripped_path); // define the "raw" path (no infile prefix, no file) let mut ancestors = stripped_path.ancestors(); ancestors.next(); let raw_path = ancestors.next() .expect("could not extract next ancestor"); println!("\traw_path: {:?}", raw_path); // out + rawpath let index_path = out_path.join(raw_path); println!("\tindex_path: {:?}", index_path); // (out + rawpath) + "index.html" let index_file = index_path.join("index.html"); println!("\tindex_file: {:?}", index_file); // - create the dir for the index.html as well as the index.html itself fs::create_dir_all(index_path)?; let mut file = File::create(&index_file)?; write_header(&mut file, &style)?; write_body_start(&mut file)?; write_nav(&mut file, in_path.as_path(), raw_path)?; write_same_level(&mut file, in_path.as_path(), raw_path)?; write_readme_content(&mut file, in_path.as_path(), raw_path)?; write_footer(&mut file)?; file.write_all("".as_bytes())?; } } Ok(()) } /// Write the html header including the style file fn write_header(file: &mut File, style: &String) -> std::io::Result<()>{ // write the header including the style file file.write_all(format!(r#" emile.space "#, style).as_bytes())?; Ok(()) } fn write_body_start(file: &mut File) -> std::io::Result<()>{ file.write_all(format!(r#"
emile.space
"#).as_bytes())?; Ok(()) } /// Write the navigation section to the given file fn write_nav(file: &mut File, in_path: &Path, raw_path: &Path) -> std::io::Result<()> { //////////////////////////////////////////////////////////////////////////// file.write_all(format!(r#" "#).as_bytes())?; //////////////////////////////////////////////////////////////////////////// Ok(()) } fn write_same_level(file: &mut File, in_path: &Path, raw_path: &Path) -> std::io::Result<()> { let search_path = Path::new(in_path).join(raw_path); println!("\tsame_level:"); println!("\t\t{:?}", search_path); let mut dirs: Vec = Vec::new(); let mut files: Vec = Vec::new(); let mut vertical: bool = false; for entry in fs::read_dir(search_path)? { let path = &entry?.path(); if path.is_dir() { dirs.push(path.to_path_buf()); println!("\t\t\t{:?}", path); } if path.is_file() { files.push(path.to_path_buf()); if path.file_name().unwrap() == "vertical" { vertical = true; } println!("\t\t\t{:?}", path); } } dirs.sort(); files.sort(); let in_path = in_path.canonicalize()?; if vertical == true { file.write_all(format!(r#"