/* 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 structopt::StructOpt; #[derive(Debug, StructOpt)] #[structopt(name = "vokobe", about = "A static site generator")] struct Opt { /// Input path #[structopt(parse(from_os_str))] input_path: PathBuf, /// Output path #[structopt(parse(from_os_str))] output_path: PathBuf, /// Site name (e.g. emile.space) site_name: String, /// Activate sending analytics to stats.emile.space // -a and --analytics will be generated // analytics are sent to stats.emile.space #[structopt(short, long)] analytics: bool, } fn main() -> std::io::Result<()> { let opt = Opt::from_args(); 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"); let mut style_file = File::open(style_path) .expect("could not open style file"); let mut style = String::new(); style_file.read_to_string(&mut style) .expect("could not read style file to string"); // read all dirs in the input path let pathes = recursive_read_dir(&in_path, false)?; 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()); // 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(&output_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(); // remove the file itself from the path 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") { // 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"); // out + rawpath let index_path = output_path.join(raw_path); // (out + rawpath) + "index.html" let index_file = index_path.join("index.html"); // - 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)?; 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 /// 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 file.write_all(format!(r#" {} "#, site_name, style).as_bytes())?; 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#"
{}
"#, site_name).as_bytes())?; Ok(()) } /// Write the navigation section to the given file 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#"