htmlpretty
The codeberg.org/derat/htmlpretty
Go package pretty-prints an already-parsed
HTML5 document, adding and removing whitespace to make it easier for humans to
read. <script>
and <style>
tags and attribute values are (hopefully) left
untouched.
The significance of whitespace in HTML is a very complicated topic, e.g.
This package's handling of it is rudimentary, and I'm certain that I've gotten
things wrong in places. As a trivial example, some lines may end up with an
unnecessary trailing space.
There are a few existing Go packages that appear to provide similar
functionality. I haven't tried them since I figured I'd run into edge cases and
I wanted to understand the code well so I can tweak it as needed, but they're
probably worth checking out.
Usage
import (
"golang.org/x/net/html"
"codeberg.org/derat/htmlpretty"
)
// ...
node, err := html.Parse(r)
if err != nil {
// ...
}
if err := htmlpretty.Print(w, node, " " /* indent */, 120 /* wrap */); err != nil {
// ...
}
A trivial command-line program named htmlpretty that
reads a document from stdin and writes it to stdout is also provided:
% go install codeberg.org/derat/htmlpretty/cmd/htmlpretty
% htmlpretty -indent=' ' -wrap=80 <old.html >new.html