Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Pretty Print HTML In Rust

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 `tidy`` command line tool like this:

Code

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

line 1 column 1 - Warning: missing <!DOCTYPE> declaration
Info: Document content looks like HTML5
Tidy found 1 warning and 0 errors!


About HTML Tidy: https://github.com/htacg/tidy-html5
Bug reports and comments: https://github.com/htacg/tidy-html5/issues
Official mailing list: https://lists.w3.org/Archives/Public/public-htacg/
Latest HTML specification: http://dev.w3.org/html5/spec-author-view/
Validate your HTML documents: http://validator.w3.org/nu/
Lobby your company to join the W3C: http://www.w3.org/Consortium

Do you speak a language other than English, or a different variant of 
English? Consider helping us to localize HTML Tidy. For details please see 
https://github.com/htacg/tidy-html5/blob/master/README/LOCALIZE.md
[_active_nvim_run:19:5] output = Output {
    status: ExitStatus(
        unix_wait_status(
            256,
        ),
    ),
    stdout: "<!DOCTYPE html>\n<html>\n<head>\n<title>title</title>\n</head>\n<body>\nhere\n</body>\n</html>\n",
    stderr: "",
}

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 `

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