htmlpretty

package module
v0.0.0-...-d5db81f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 17, 2023 License: BSD-3-Clause Imports: 5 Imported by: 0

README

htmlpretty

GoDoc Build Status

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

Documentation

Overview

Package htmlpretty pretty-prints HTML5 documents.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Print

func Print(w io.Writer, root *html.Node, indent string, wrap int) error

Print pretty-prints the supplied HTML document to w. The supplied indent string is used for a single level of indenting. If wrap is positive, lines will be wrapped at that many bytes where possible.

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL