gohtml

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

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

Go to latest
Published: Oct 13, 2020 License: MIT Imports: 7 Imported by: 136

README

GoHTML - HTML formatter for Go

wercker status GoDoc

GoHTML is an HTML formatter for Go. You can format HTML source codes by using this package.

Install

go get -u github.com/yosssi/gohtml

Example

Example Go source code:

package main

import (
	"fmt"

	"github.com/yosssi/gohtml"
)

func main() {
	h := `<!DOCTYPE html><html><head><title>This is a title.</title><script type="text/javascript">
alert('aaa');
if (0 < 1) {
	alert('bbb');
}
</script><style type="text/css">
body {font-size: 14px;}
h1 {
	font-size: 16px;
	font-weight: bold;
}
</style></head><body><form><input type="name"><p>AAA<br>BBB></p></form><!-- This is a comment. --></body></html>`
	fmt.Println(gohtml.Format(h))
}

Output:

<!DOCTYPE html>
<html>
  <head>
    <title>
      This is a title.
    </title>
    <script type="text/javascript">
      alert('aaa');
      if (0 < 1) {
      	alert('bbb');
      }
    </script>
    <style type="text/css">
      body {font-size: 14px;}
      h1 {
      	font-size: 16px;
      	font-weight: bold;
      }
    </style>
  </head>
  <body>
    <form>
      <input type="name">
      <p>
        AAA
        <br>
        BBB>
      </p>
    </form>
    <!-- This is a comment. -->
  </body>
</html>

Output Formatted HTML with Line No

You can output formatted HTML source codes with line no by calling FormatWithLineNo:

package main

import (
	"fmt"

	"github.com/yosssi/gohtml"
)

func main() {
	h := `<!DOCTYPE html><html><head><title>This is a title.</title><script type="text/javascript">
alert('aaa');
if (0 < 1) {
	alert('bbb');
}
</script><style type="text/css">
body {font-size: 14px;}
h1 {
	font-size: 16px;
	font-weight: bold;
}
</style></head><body><form><input type="name"><p>AAA<br>BBB></p></form><!-- This is a comment. --></body></html>`
	fmt.Println(gohtml.FormatWithLineNo(h))
}

Output:

 1  <!DOCTYPE html>
 2  <html>
 3    <head>
 4      <title>
 5        This is a title.
 6      </title>
 7      <script type="text/javascript">
 8        alert('aaa');
 9        if (0 < 1) {
10        	alert('bbb');
11        }
12      </script>
13      <style type="text/css">
14        body {font-size: 14px;}
15        h1 {
16        	font-size: 16px;
17        	font-weight: bold;
18        }
19      </style>
20    </head>
21    <body>
22      <form>
23        <input type="name">
24        <p>
25          AAA
26          <br>
27          BBB>
28        </p>
29      </form>
30      <!-- This is a comment. -->
31    </body>
32  </html>

Format Go html/template Package's Template's Execute Result

You can format Go html/template package's template's execute result by passing Writer to the tpl.Execute:

package main

import (
	"os"
	"text/template"

	"github.com/yosssi/gohtml"
)

func main() {

	tpl, err := template.New("test").Parse("<html><head></head><body>{{.Msg}}</body></html>")

	if err != nil {
		panic(err)
	}

	data := map[string]interface{}{"Msg": "Hello!"}

	err = tpl.Execute(gohtml.NewWriter(os.Stdout), data)

	if err != nil {
		panic(err)
	}
}

Output:

<html>
  <head>
  </head>
  <body>
    Hello!
  </body>
</html>

Docs

Documentation

Overview

Package gohtml provides an HTML formatting function.

Index

Constants

This section is empty.

Variables

View Source
var Condense bool

Enable condensing a tag with only inline children onto a single line, or completely inlining it with sibling nodes. Tags to be treated as inline can be set in `InlineTags`. Only inline tags will be completely inlined, while other condensable tags will be given their own dedicated (single) line.

View Source
var InlineTagMaxLength = 40

Maximum length of an opening inline tag before it's un-inlined

View Source
var InlineTags = map[string]bool{
	"a":      true,
	"code":   true,
	"em":     true,
	"span":   true,
	"strong": true,
}

Tags that are considered inline tags. Note: Text nodes are always considered to be inline

View Source
var IsPreformatted = func(token html.Token) bool {
	return token.Data == "pre" || token.Data == "textarea"
}

Function that identifies which tags will be treated as containing preformatted content. Such tags will have the formatting of all its contents preserved unchanged. The opening tag html.Token is passed to this function. By default, only <pre> and <textarea> tags are considered preformatted.

View Source
var LineWrapColumn = 0

Column to wrap lines to (disabled by default)

View Source
var LineWrapMaxSpillover = 5

Maxmimum characters a long word can extend past LineWrapColumn without wrapping

Functions

func AddLineNo

func AddLineNo(s string) string

func Format

func Format(s string) string

Format parses the input HTML string, formats it and returns the result.

func FormatBytes

func FormatBytes(b []byte) []byte

FormatBytes parses input HTML as bytes, formats it and returns the result.

func FormatWithLineNo

func FormatWithLineNo(s string) string

Format parses the input HTML string, formats it and returns the result with line no.

Types

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

A Writer represents a formatted HTML source codes writer.

func NewWriter

func NewWriter(wr io.Writer) *Writer

NewWriter generates a Writer and returns it.

func (*Writer) SetLastElement

func (wr *Writer) SetLastElement(lastElement string) *Writer

SetLastElement set the lastElement to the Writer.

func (*Writer) Write

func (wr *Writer) Write(p []byte) (n int, err error)

Write writes the parameter.

Jump to

Keyboard shortcuts

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