Home
Head's Up: I'm in the middle of upgrading my site. Most things are in place, but there are something missing and/or broken including image alt text. Please bear with me while I'm getting things fixed.

Pretty Print HTML In Rust

rust
use std::io::Write;
use std::process::{Command, Stdio};

fn main() {
    let args = vec!["--tidy-mark", "false", "--doctype", "html5"];
    let mut child = Command::new("tidy")
        .args(args)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()
        .expect("Failed to spawn child process");
    let mut stdin = child.stdin.take().expect("Failed to open stdin");
    std::thread::spawn(move || {
        stdin
            .write_all("<html><head><title>title</title></head><body>here</body></html>".as_bytes())
            .expect("Failed to write to stdin");
    });
    let output = child.wait_with_output().expect("Failed to read stdout");
    dbg!(output);
}
results start

I haven't found a good crate for pretty printing HTML in rust. There are some, but they are all old and have very few downloads. I don't want to sort through that. For now, I'm shelling out to the [TODO: Code shorthand span ] command line tool like this :

The tidy command is designed to clean up HTML documents. That includes altering them to make fixes when it can. For example, it adds a [TODO: Code shorthand span ] and will reorder nested tags to make them match properly.

I was originally planning to use this to test output from Neopoligen, but that won't work since this can change the code.