Documentation
¶
Index ¶
- type JSONOptions
- type Parser
- func (p *Parser) HTML() string
- func (p *Parser) Root() *html.Node
- func (p *Parser) String() string
- func (p *Parser) ToJSON() ([]byte, error)
- func (p *Parser) ToJSONWithOptions(opts JSONOptions) ([]byte, error)
- func (p *Parser) ToMap(opts JSONOptions) map[string]any
- func (p *Parser) URL() string
- func (p *Parser) WithSuppressErrors() *Parser
- func (p *Parser) XPath(expr string) (*Selection, error)
- func (p *Parser) XPathAll(expr string) ([]*Selection, error)
- type ReturnType
- type Selection
- func (s *Selection) Attr(name string) string
- func (s *Selection) AttrOr(name, defaultValue string) string
- func (s *Selection) Children() []*Selection
- func (s *Selection) Content(returnType ReturnType) string
- func (s *Selection) Each(fn func(int, *Selection))
- func (s *Selection) EvaluateXPath(expr string) (any, error)
- func (s *Selection) Find(expr string) (*Selection, error)
- func (s *Selection) FindAll(expr string) ([]*Selection, error)
- func (s *Selection) FirstChild() *Selection
- func (s *Selection) HTML() string
- func (s *Selection) InnerHTML() string
- func (s *Selection) LastChild() *Selection
- func (s *Selection) NextSibling() *Selection
- func (s *Selection) Parent() *Selection
- func (s *Selection) PrevSibling() *Selection
- func (s *Selection) Text() string
- func (s *Selection) TextTrimmed() string
- func (s *Selection) ToJSON(opts JSONOptions) ([]byte, error)
- func (s *Selection) ToMap(opts JSONOptions) map[string]any
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type JSONOptions ¶
type JSONOptions struct {
IncludeAttributes bool
IncludeTextContent bool
PrettyPrint bool
TrimWhitespace bool
}
JSONOptions controls the behavior of HTML to JSON conversion.
func DefaultJSONOptions ¶
func DefaultJSONOptions() JSONOptions
DefaultJSONOptions returns the default JSON conversion options.
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser represents an HTML document that can be queried with XPath.
func LoadFromURL ¶
LoadFromURL creates a Parser by fetching HTML from a URL. Deprecated: Use gtmlp.ParseURL() instead, which includes HTTP client functionality.
func (*Parser) ToJSONWithOptions ¶
func (p *Parser) ToJSONWithOptions(opts JSONOptions) ([]byte, error)
ToJSONWithOptions converts the HTML document to JSON with custom options.
func (*Parser) ToMap ¶
func (p *Parser) ToMap(opts JSONOptions) map[string]any
ToMap converts the parser document to a map structure.
func (*Parser) WithSuppressErrors ¶
WithSuppressErrors enables error suppression for XPath queries. When enabled, XPath errors return nil instead of error values.
type ReturnType ¶
type ReturnType string
ReturnType defines how to extract content from nodes.
const ( // ReturnTypeText returns plain text content. ReturnTypeText ReturnType = "text" // ReturnTypeHTML returns HTML content. ReturnTypeHTML ReturnType = "html" )
type Selection ¶
type Selection struct {
// contains filtered or unexported fields
}
Selection represents a selected HTML node.
func SelectionFromNode ¶
SelectionFromNode creates a Selection from an html.Node.
func SelectionsFromNodes ¶
SelectionsFromNodes creates a slice of Selections from a slice of html.Nodes.
func (*Selection) AttrOr ¶
AttrOr returns the value of an attribute, or a default value if not found.
func (*Selection) Content ¶
func (s *Selection) Content(returnType ReturnType) string
Content returns the content based on the specified return type.
func (*Selection) EvaluateXPath ¶
EvaluateXPath evaluates a raw XPath expression and returns the result.
func (*Selection) Find ¶
Find executes an XPath expression relative to the current selection. Returns nil if no match is found.
func (*Selection) FirstChild ¶
FirstChild returns the first child element (not text node).
func (*Selection) InnerHTML ¶
InnerHTML returns the inner HTML of the selected node (excluding the outer tag).
func (*Selection) NextSibling ¶
NextSibling returns the next sibling element.
func (*Selection) PrevSibling ¶
PrevSibling returns the previous sibling element.
func (*Selection) TextTrimmed ¶
TextTrimmed returns the trimmed text content of the selected node.