prettyprint

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

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

Go to latest
Published: Oct 19, 2015 License: MIT Imports: 2 Imported by: 1

README

Go PrettyPrint HTML

A simple pretty printer for HTML, written in Go.

Installation
go get github.com/hermanschaaf/prettyprint
Usage
import (
    "github.com/hermanschaaf/prettyprint"
    "fmt"
)

func main() {
    html := `<html><head></head><body><a href="http://test.com">Test link</a><p><br/></p></body></html>`
    // second parameter is the indent to use 
    // (can be anything: spaces, a tab, etc.)
    pretty, err := prettyprint.Prettify(html, "    ")
    if err != nil {
        // do something in case of error
    }
    // print the prettified html:
    fmt.Println(pretty)
}

This will have the following output:

<html>
    <head>
    </head>
    <body>
        <a href="http://test.com">Test link</a>
        <p>
            <br/>
        </p>
    </body>
</html>
Known issues
  • Poor support for self-closing tags, like HTML5 <br>. For now, only explicitly self-closing tags like <br/> will print correctly.
  • No special cases for <pre> and <code> tags
  • Comments may or may not work, they are more or less untested

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Prettify

func Prettify(raw string, indent string) (pretty string, e error)

Prettify takes a string with unformatted HTML content and formats it prettily with indentation supplied through the indent argument.

For example, it takes a string of html like this:

<html><head></head><body><p>Hello</p></body></html>

and formats it into this:

<html>

<head>
</head>
<body>
    <a href="http://test.com">Test link</a>
    <p>
        <br/>
    </p>
</body>

</html>

if the provided indent parameter is a string with four spaces (" ").

It returns the prettified string :pretty:, and possibly an error :e:

Types

This section is empty.

Jump to

Keyboard shortcuts

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