~evan-hoose/SSSSS

fcc366f8ebd38caf51fde2eb60b78afd64f886fc — Evan 1 year, 2 months ago 1c864b1
	modified:   README.md
	modified:   s5-render
	modified:   s5-render.rs
3 files changed, 68 insertions(+), 43 deletions(-)

M README.md
M s5-render
M s5-render.rs
M README.md => README.md +4 -0
@@ 27,6 27,7 @@ commands, you're gonna have a bad time.
---

##About        

This program will take a directory structure consisting of directories with
images and markdown files and render it to an html website.



@@ 154,4 155,7 @@ file you created, along with an index.html file. Go ahead and play around
with changing the template files at the top, and look at how the outputed html
files change.

Presumably if you've found this, you know enough about computers to figure it 
out from here.



M s5-render => s5-render +0 -0
M s5-render.rs => s5-render.rs +64 -43
@@ 3,6 3,10 @@ use std::fs;
use std::path::PathBuf;
use std::process::Command;

//We use lot's of unwrap()'s in this. Mostly because I'm having issues figuring
//out how to write nice looking error handling, and partially because in most
//of these cases if we can't accomplish the thing panicing is not a bad option.

fn main() {
    //This is the path to the file that we use as the document header.
    let document_header = fs::canonicalize("./document-header.html").unwrap();


@@ 47,60 51,77 @@ fn render(directory: PathBuf,mut document_header: PathBuf) {
        let path = PathBuf::from(name.unwrap().path());
        //Let us find the metadata for path, so that we can later test if it's
        //a directory or some other kind of file.
        let file_metadata = fs::metadata(path.as_path()).unwrap();
        let file_type = file_metadata.file_type();
        if fs::metadata(path.as_path()).is_ok() {
            //shouldn't have issues, we've already checked to make sure it 
            //works
            let file_metadata = fs::metadata(path.as_path()).unwrap();
            let file_type = file_metadata.file_type();

        //If it's a dir, continue down. Otherwise, if it's an md file, render
        //it to html, while using the headers and footers at the top.
        //
        //Also, skip the .git directory. It breaks the attempts to look at file
        //metadata. Yes, I really should just write error handling. However,
        //I'm stupid and can't figure it out.
        if file_type.is_dir() && path.clone().as_os_str() != OsStr::new(".git") {
            render(path, document_header.clone()) //If the path is a dir, continue down.
        } else {
            //If it's not a dir, it must be a file
            if path.extension() == Some(OsStr::new("md")) {
                //leads to index.html in the same directory as path
                //We declare it here instead of above, because why use it before we
                //need it.
                let mut write_path = path.clone();
                write_path.set_file_name("index.html");
                let markdown_path = path.as_path();
            //If it's a dir, continue down. Otherwise, if it's an md file, render
            //it to html, while using the headers and footers at the top.
            //
            //Also, skip the .git directory. We don't want to risk modifying
            //anything inside of it.
            if file_type.is_dir() && path.clone().as_os_str() != OsStr::new(".git") {
                render(path, document_header.clone()) //If the path is a dir, continue down.
            } else {
                //If it's not a dir, it must be a file
                if path.extension() == Some(OsStr::new("md")) {
                    //leads to index.html in the same directory as path
                    //We declare it here instead of above, because why use it before we
                    //need it.
                    let mut write_path = path.clone();
                    write_path.set_file_name("index.html");
                    let markdown_path = path.as_path();

                //Shell magic. shell() takes an &str and executes it as if it
                //were being run in the systems /bin/sh. May make it work for
                //Windows some day, but I don't care that much.
                    //shell() takes an &str and executes it as if it
                    //were being run as "/bin/sh -c &str". May make it work for
                    //Windows some day, but I don't care that much.

                //cat ./html-header.html > $write_path
                let append_html_header =
                    format!("cat ./html-header.html > {}", write_path.display());
                shell(&append_html_header);
                //cat ./document-header.html >> $write_path
                let append_doc_header =
                    format!("cat {} >> {}", document_header.display(), write_path.display());
                shell(&append_doc_header);
                //markdown $markdown_path >> $write_path
                let append_markdown = format!(
                    "markdown {} >> {}",
                    markdown_path.display(),
                    write_path.display()
                );
                shell(&append_markdown);
                //cat ./document-footer.html >> $write_path
                let append_footer =
                    format!("cat ./document-footer.html >> {}", write_path.display());
                shell(&append_footer);
                    //cat ./html-header.html > $write_path
                    let append_html_header =
                        format!("cat ./html-header.html > {}", write_path.display());
                    shell(&append_html_header);
                    //cat ./document-header.html >> $write_path
                    let append_doc_header =
                        format!("cat {} >> {}", document_header.display(), write_path.display());
                    shell(&append_doc_header);
                    //markdown $markdown_path >> $write_path
                    let append_markdown = format!(
                        "markdown {} >> {}",
                        markdown_path.display(),
                        write_path.display()
                    );
                    shell(&append_markdown);
                    //cat ./document-footer.html >> $write_path
                    let append_footer =
                        format!("cat ./document-footer.html >> {}", write_path.display());
                    shell(&append_footer);
                }
            }
        }
        } //if fs::metadata(path.as_path() ...
    } //This is where the for loop ends
}

//Taken from git.sr.ht/~evan-hoose/Snippets. Well, I guess it was here first,
//but that's where it lives.
//
//If I update it, that's where the most up to date version will be.
fn shell(command: &str) {
    let _status = Command::new("/bin/sh")
    let status = Command::new("/bin/sh")
        .arg("-c")
        .arg(&command)
        .status()
        .expect("Unable to execute shell");

    //If command passed to shell doesn't return 0, print the command and the 
    //exit status for debugging.
    //
    //The most likely case for this to happen is missing files, which will
    //cause both cat and markdown to spit something out. Gonna leave this
    //here anyways, just in case something else fails.
    if !(status.success()) {
        println!("({}): {}", command, status);
    }

}