htmlformat

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2024 License: MIT Imports: 8 Imported by: 1

README

htmlformat

A go library for formatting HTML.

Go Reference

Behaviour and restrictions

  • whitespaces around and inside inline nodes are conserved
  • long lines are never wrapped, and left unchanged
  • attributes too are always on a single line after the tag name
  • script, style, code, pre contents are treated as text, the indentation is only modified to align with the parent node

Why

There are already plenty of existing HTML formatters. I wrote this one because there is a recurring quirk that I see with HTML formatters, not just in go packages, even in JSX formatters too. The recurring quirk is that the whitespaces are not correctly inserted or conserved.

For instance:

<p>
        Whitespaces around, between, in, after texts/nodes
should be <i>conserved</i>. The period from
    the last sentence should not be separated with
    a space, but this one <b>is</b> .
    <em> This has a leading space</em>

        <em>This has a trailing space </em>
    
    [<a>link</a>]
    </p>

Some formatters will output:

<p>
    Whitespaces around, between, in, after texts/nodes
    should be
    <i>
       maintained
    </i>
    . The period from
    the last sentence should not be separated with
    a space, but this one
    <b>
        is
    </b>
    .
    <em>
        This has a leading space
    </em>
    <em>
        This has a trailing space
    </em>
    [
    <a>
        link
    </a>
    ]
</p>

While the HTML may look organized, whitespaces are inserted where it shouldn't be. This will look weird when rendered, such as the period dangling on its own,

Whereas, this library will output:

<p>
    Whitespaces around, between, in, after texts/nodes
    should be <i>maintained</i>. The period from
    the last sentence should not be separated with
    a space, but this one <b>is</b> .
    <em> This has a leading space</em>
    <em>This has a trailing space </em>
    [<a>link</a>]
</p>

TODO

  • create a site that compares output of different libraries

Documentation

Overview

Package htmlformat provides function for cleanly formatting html code, for purposes of making the html code more human-readable.

Cleanly formatted (AKA pretty printed) means: - any excess or unnecessary whitespaces are removed - child nodes are indented and aligned relative to parent node

This package does not handle long lines, no line wrapping is done to break lone lines.

Conserving whitespaces takes priority over formatting and readability. This means this package avoids adding whitespaces where it might alter rendered HTML.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

func Format(htmlStr string) string

Format returns a cleanly formatted html string.

Example
html := `<p   id = "x">hello</p > `
fmt.Println(Format(html))
Output:

<p id="x">hello</p>

func Write

func Write(htmlStr string, w io.Writer)

Format writes the cleanly formatted html string into w.

Example
// Write output directly to stdout
html := `<p   id = "x">hello to stdout</p > `
Write(html, os.Stdout)
Output:

<p id="x">hello to stdout</p>
Example (Second)
// Write output directly to a file
file, err := os.CreateTemp("", "")
if err != nil {
	panic(err)
}
defer os.Remove(file.Name())
html := `<p   id = "x">hello to file</p > `
Write(html, file)
file.Sync()
file.Close()

bytes, err := os.ReadFile(file.Name())
fmt.Println(string(bytes))
Output:

<p id="x">hello to file</p>

Types

This section is empty.

Jump to

Keyboard shortcuts

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