shrw

package module
v0.0.0-...-7e2f65b Latest Latest
Warning

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

Go to latest
Published: May 9, 2021 License: Unlicense Imports: 2 Imported by: 0

README

Simple Recursive Html Walker

Using a simple matching interface to find nodes in a *html.Node tree

interface Matcher {
	// Match returns the node if it matches
	// it will otherwise return nil
	Match(*html.Node) *html.Node
}

// Implements the Matcher interface
type (
	Id		string
	Class		string	// "one" or "two" will match class="one two"
	ClassFull	string	// only "one two" will match class="one two"
	Tag		string
	Text		string
	TextNoTrim	string
)

There are 4 flavors of walkers

// Get the first node matching
func Walk(*html.Node, Matcher)

// Get all nodes matching
func WalkAll(chan *html.Node, *html.Node, Matcher)

// Get first grandchild node matching the pattern
func WalkPattern(*html.Node, ...Matcher)

// Get all grandchild nodes matching the pattern
func WalkPatternAll(chan *html.Node, *html.Node, ...Matcher)

Examples

Get node with id "horse"

node = shrw.Walk(node, shrw.Id("horse"))

Get all nodes with the "thumbnail" class

ch := make(chan *html.Node)

go shrw.WalkAll(ch, node, shrw.Class("thumbnail"))

for node := range ch {
	// Use node
}

Get node following a pattern

// This will get you the text node 'This Title' of
//	<div id="section">
//		<div class="message>
//			<h1>
//				This Title
//			</h1>
//		</div>
//	</div>
node = shrw.WalkPattern(
		node,
		shrw.Id("section"),
		shrw.Class("message"),
		shrw.Tag("h1"),
		shrw.Text("This Title"),
)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Walk

func Walk(node *html.Node, m Matcher) *html.Node

Get the first node matching m

func WalkAll

func WalkAll(ch chan *html.Node, node *html.Node, m Matcher)

Get all nodes matching m

func WalkPattern

func WalkPattern(node *html.Node, m ...Matcher) *html.Node

Get first grandchild node matching the m pattern

func WalkPatternAll

func WalkPatternAll(ch chan *html.Node, node *html.Node, m ...Matcher)

Get all grandchild nodes matching the m pattern

Types

type Class

type Class string // "one" or "two" will match class="one two"

Implements the matcher interface

func (Class) Match

func (m Class) Match(node *html.Node) *html.Node

type ClassFull

type ClassFull string // only "one two" will match class="one two"

Implements the matcher interface

func (ClassFull) Match

func (m ClassFull) Match(node *html.Node) *html.Node

type Id

type Id string

Implements the matcher interface

func (Id) Match

func (m Id) Match(node *html.Node) *html.Node

type Matcher

type Matcher interface {
	// Match returns the node if it matches
	// it will otherwise return nil
	Match(*html.Node) *html.Node
}

type Tag

type Tag string

Implements the matcher interface

func (Tag) Match

func (m Tag) Match(node *html.Node) *html.Node

type Text

type Text string

Implements the matcher interface

func (Text) Match

func (m Text) Match(node *html.Node) *html.Node

type TextNoTrim

type TextNoTrim string

Implements the matcher interface

func (TextNoTrim) Match

func (m TextNoTrim) Match(node *html.Node) *html.Node

Jump to

Keyboard shortcuts

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