miniflux/vendor/github.com/tdewolff/parse/html
Frédéric Guillot 8ffb773f43 First commit
2017-11-19 22:01:46 -08:00
..
hash.go First commit 2017-11-19 22:01:46 -08:00
hash_test.go First commit 2017-11-19 22:01:46 -08:00
lex.go First commit 2017-11-19 22:01:46 -08:00
lex_test.go First commit 2017-11-19 22:01:46 -08:00
README.md First commit 2017-11-19 22:01:46 -08:00
util.go First commit 2017-11-19 22:01:46 -08:00
util_test.go First commit 2017-11-19 22:01:46 -08:00

HTML GoDoc GoCover

This package is an HTML5 lexer written in Go. It follows the specification at The HTML syntax. The lexer takes an io.Reader and converts it into tokens until the EOF.

Installation

Run the following command

go get github.com/tdewolff/parse/html

or add the following import and run project with go get

import "github.com/tdewolff/parse/html"

Lexer

Usage

The following initializes a new Lexer with io.Reader r:

l := html.NewLexer(r)

To tokenize until EOF an error, use:

for {
	tt, data := l.Next()
	switch tt {
	case html.ErrorToken:
		// error or EOF set in l.Err()
		return
	case html.StartTagToken:
		// ...
		for {
			ttAttr, dataAttr := l.Next()
			if ttAttr != html.AttributeToken {
				break
			}
			// ...
		}
	// ...
	}
}

All tokens:

ErrorToken TokenType = iota // extra token when errors occur
CommentToken
DoctypeToken
StartTagToken
StartTagCloseToken
StartTagVoidToken
EndTagToken
AttributeToken
TextToken

Examples

package main

import (
	"os"

	"github.com/tdewolff/parse/html"
)

// Tokenize HTML from stdin.
func main() {
	l := html.NewLexer(os.Stdin)
	for {
		tt, data := l.Next()
		switch tt {
		case html.ErrorToken:
			if l.Err() != io.EOF {
				fmt.Println("Error on line", l.Line(), ":", l.Err())
			}
			return
		case html.StartTagToken:
			fmt.Println("Tag", string(data))
			for {
				ttAttr, dataAttr := l.Next()
				if ttAttr != html.AttributeToken {
					break
				}

				key := dataAttr
				val := l.AttrVal()
				fmt.Println("Attribute", string(key), "=", string(val))
			}
		// ...
		}
	}
}

License

Released under the MIT license.