pig

package module
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2023 License: MIT Imports: 6 Imported by: 2

README

pig

Traverse and find HTML nodes. Based on std library's *html.Node, add extension methods to it.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Map

func Map[T any](node Node, f func(n Node) T) []T

Map returns a slice of the results of applying the function f to each node in the node's children.

Types

type Match

type Match func(Node) bool

func And added in v0.3.0

func And(ms ...Match) Match

And returns a Match that returns AND of the given Matches.

func Atom added in v0.9.0

func Atom(a atom.Atom) Match

Atom returns a Match that returns true if the given node's atom is the given atom.

func Cls

func Cls(cls string) Match

Cls returns a Match that returns true if the given node has the given class.

func HasAttr

func HasAttr(attr string) Match

HasAttr returns a Match that returns true if the given node has the given attribute.

func HasAttrVal

func HasAttrVal(attr, val string) Match

HasAttrVal returns a Match that returns true if the given node has the given attribute and the attribute's value is the given value.

func ID added in v0.3.0

func ID(id string) Match

ID returns a Match that returns true if the given node has the given id.

func Or added in v0.3.0

func Or(ms ...Match) Match

Or returns a Match that returns OR of the given Matches.

func Tag

func Tag(tag string) Match

Tag returns a Match that returns true if the given node's tag is the given tag.

func (Match) And added in v0.7.0

func (m Match) And(ms ...Match) Match

And returns a Match that returns AND of the given Matches.

func (Match) Not added in v0.7.0

func (m Match) Not() Match

Not returns a Match that returns negation of the given Match.

func (Match) Or added in v0.7.0

func (m Match) Or(ms ...Match) Match

Or returns a Match that returns OR of the given Matches.

type Node added in v0.2.0

type Node struct{ *html.Node }

Node is a wrapper of html.Node.

func MakeTree added in v0.9.0

func MakeTree(ns ...Node) Node

MakeTree makes a new root node and appends given nodes as its children.

Given nodes are cloned and detached from their original parent. Original nodes are not modified.

func NewRoot added in v0.9.0

func NewRoot() Node

NewRoot returns a new root node.

func Parse

func Parse(r io.Reader) (Node, error)

Parse parses HTML from io.Reader.

func ParseB

func ParseB(b []byte) (Node, error)

ParseB parses HTML from []byte.

func ParseS

func ParseS(s string) (Node, error)

ParseS parses HTML from string.

func (Node) AttrVal added in v0.4.0

func (n Node) AttrVal(attr string) (string, bool)

AttrVal returns the value of the attribute.

func (Node) Children added in v0.2.0

func (n Node) Children() []Node

Children returns a slice of all children of the node.

func (Node) Clone added in v0.9.0

func (n Node) Clone() Node

Clone returns a copy of the node.

It does not a deep copy about the connected nodes. So child, parent, and sibling are same pointers as the original one. Attributes are copied.

func (Node) CloneDetach added in v0.9.0

func (n Node) CloneDetach() Node

CloneDetach returns a copy of the node.

The node is detached from its parent and siblings. It has same children as the original one.

func (Node) Each added in v0.2.0

func (node Node) Each(f func(int, Node))

Each calls the function f for each node in the node's children.

func (Node) EachBreak added in v0.6.0

func (node Node) EachBreak(f func(int, Node) bool)

EachBreak calls the function f for each node in the node's children. If f returns true, the iteration is stopped.

func (Node) Find added in v0.2.0

func (n Node) Find(m Match) Node

Find finds nodes that match the given condition.

It returns first node that matches the condition on each branch. If no node is found, it returns an empty node. Found nodes are bundled in a new root node.

func (Node) FindAll added in v0.9.0

func (n Node) FindAll(m Match) Node

FindAll finds nodes that match the given condition.

It returns all node that matches the condition. If no node is found, it returns an empty node. Found nodes are bundled in a new root node.

func (Node) FindAllWithDepth added in v0.9.0

func (n Node) FindAllWithDepth(m Match, depth int) Node

FindAllWithDepth finds nodes that match the given condition.

If depth is more than 0, it searches descendants up to the depth. If depth is -1, it searches all descendants.

It returns all node that matches the condition. If no node is found, it returns an empty node. Found nodes are bundled in a new root node.

func (Node) FindChild added in v0.5.0

func (n Node) FindChild(ms ...Match) Node

FindChild finds a node that matches the given conditions.

It searches for a node that matches each condition in ms, but only among direct children of the previous matching node. For example, if len(ms) == 2, it searches a node that matches ms[1] that is a direct child of a node that matches ms[0].

It returns first node that matches the condition on each branch. If no node is found, it returns an empty node. Found nodes are bundled in a new root node.

func (Node) FindDescendant added in v0.5.0

func (n Node) FindDescendant(ms ...Match) Node

FindDescendant finds a node that matches the given conditions.

It searches for a node that matches each condition in ms, but only among descendants of the previous matching node. For example, if len(ms) == 2, it searches a node that matches ms[1] that is a descendant of a node that matches ms[0].

It returns first node that matches the condition on each branch. If no node is found, it returns an empty node. Found nodes are bundled in a new root node.

func (Node) FindWithDepth added in v0.9.0

func (n Node) FindWithDepth(m Match, depth int) Node

FindWithDepth finds nodes that match the given condition.

If depth is more than 0, it searches descendants up to the depth. If depth is -1, it searches all descendants.

It returns first node that matches the condition on each branch. If no node is found, it returns an empty node. Found nodes are bundled in a new root node.

func (Node) First added in v0.8.0

func (n Node) First() Node

First returns the first child node.

func (Node) FirstChild added in v0.2.0

func (n Node) FirstChild() Node

FirstChild returns the first child node.

func (Node) Get added in v0.8.0

func (n Node) Get(index int) Node

Get returns the child node at the given index.

You can use negative index same as slices. If the index is out of range, it returns a new empty node and false.

func (Node) GetE added in v0.8.0

func (n Node) GetE(index int) (Node, bool)

GetE returns the child node at the given index.

You can use negative index same as slices. Second return value is true if index is in range. If the index is out of range, it returns a new empty node and false.

func (Node) Html added in v0.8.0

func (n Node) Html() (string, error)

Html returns the HTML representation of the node.

func (Node) Last added in v0.8.0

func (n Node) Last() Node

Last returns the last child node.

func (Node) LastChild added in v0.2.0

func (n Node) LastChild() Node

LastChild returns the last child node.

func (Node) Len added in v0.9.0

func (n Node) Len() int

Len returns the number of children of the node.

func (Node) NextSibling added in v0.2.0

func (n Node) NextSibling() Node

NextSibling returns the next sibling node.

func (Node) Parent added in v0.2.0

func (n Node) Parent() Node

Parent returns the parent node.

func (Node) PrevSibling added in v0.2.0

func (n Node) PrevSibling() Node

PrevSibling returns the previous sibling node.

func (Node) Text added in v0.2.0

func (n Node) Text() string

Text returns the text of the node.

Jump to

Keyboard shortcuts

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