From 3793cc86b49310b2e9aa8a79c5c8109c8f87c230 Mon Sep 17 00:00:00 2001 From: Emile Date: Sat, 8 Apr 2023 20:13:27 +0200 Subject: Periodic commit with a lot of changes I tend to fiddle around with vokobe a lot and I'm just using git as a form of backup, so here's one of these backup commits with all kinds of random stuff --- src/main.rs | 101 ++++++++++++++++++++++++++++-------------------------------- 1 file changed, 47 insertions(+), 54 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 438a24a..e82d081 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,14 @@ -// pull the std into scope and inline it so that we get documentation for it, -// even when running offline +/* +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, BufRead, BufReader}; use std::fs::{self, File}; -use std::{time}; +use std::time; use structopt::StructOpt; #[derive(Debug, StructOpt)] @@ -14,11 +16,11 @@ use structopt::StructOpt; struct Opt { /// Input path #[structopt(parse(from_os_str))] - in_path: PathBuf, + input_path: PathBuf, /// Output path #[structopt(parse(from_os_str))] - out_path: PathBuf, + output_path: PathBuf, /// Site name (e.g. emile.space) site_name: String, @@ -34,11 +36,8 @@ fn main() -> std::io::Result<()> { let opt = Opt::from_args(); - let in_path = opt.in_path; - let out_path = opt.out_path; - - println!("inpath: {}", in_path.display()); - println!("outpath: {}", out_path.display()); + let in_path = opt.input_path; + let output_path = opt.output_path; // read the style let style_path = Path::new(&in_path).join("style.css"); @@ -51,11 +50,9 @@ fn main() -> std::io::Result<()> { // 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()); + println!("Got {} files", pathes.len()); + for path in pathes { let stripped_path = path.strip_prefix(&in_path) .expect(format!( "could not strip the in_path prefix: {:?}", in_path).as_str()); @@ -65,12 +62,11 @@ fn main() -> std::io::Result<()> { // define the source and destination let src = Path::new(&in_path).join(stripped_path); - let dst = Path::new(&out_path).join(stripped_path); + let dst = Path::new(&output_path).join(stripped_path); - // define the destination folder (the dst path without the file) and - // create it + // define the destination folder (the dst path without the file) and create it let mut dst_folder = dst.clone(); - dst_folder.pop(); + dst_folder.pop(); // remove the file itself from the path fs::create_dir_all(dst_folder)?; // copy the file to the destination @@ -78,7 +74,6 @@ fn main() -> std::io::Result<()> { } 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(); @@ -86,20 +81,20 @@ fn main() -> std::io::Result<()> { 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); + let index_path = output_path.join(raw_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 + // - 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)?; + // this is the main block calling all other smaller functions. The + // whole output is compsed here write_header(&mut file, &opt.site_name, &style)?; write_body_start(&mut file, &opt.site_name)?; write_nav(&mut file, in_path.as_path(), raw_path, opt.analytics)?; @@ -116,6 +111,9 @@ fn main() -> std::io::Result<()> { } /// Write the html header including the style file +/// TODO: Don't add the style file into each compiled html output, as the +/// style can be included allowing the user to cache the style file in their +/// browser. fn write_header(file: &mut File, site_name: &String, style: &String) -> std::io::Result<()>{ // write the header including the style file @@ -136,6 +134,8 @@ fn write_header(file: &mut File, site_name: &String, style: &String) -> std::io: Ok(()) } +/// write the start of the html body tag and the header linking back to the +/// site itself. fn write_body_start(file: &mut File, site_name: &String) -> std::io::Result<()>{ file.write_all(format!(r#" @@ -151,10 +151,15 @@ fn write_nav(file: &mut File, in_path: &Path, raw_path: &Path, analytics: bool) -> std::io::Result<()> { if analytics == true { + /* file.write_all(format!(r#"