html

package module
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package html reads HTML payloads using github.com/PuerkitoBio/goquery.

The reader extracts visible text from an HTML document. Two modes:

  • Whole-document mode (default): a single *document.Document containing the body text with title / description / canonical URL stamped as metadata.
  • Selector mode (opt in via ReaderConfig.Selector): emits one document per element matched by the CSS selector — useful for scraping blog post lists, search results, etc.

Example:

r, _ := html.NewReader(strings.NewReader(htmlSrc), html.ReaderConfig{})
docs, _ := r.Read(ctx)

r, _ := html.NewReader(strings.NewReader(htmlSrc),
    html.ReaderConfig{Selector: "article"})
docs, _ := r.Read(ctx) // one doc per <article>

Index

Constants

View Source
const (
	MetadataTitle       = "html.title"
	MetadataDescription = "html.description"
	MetadataCanonical   = "html.canonical"
	MetadataSelector    = "html.selector"
	MetadataSourceName  = "html.source"
)

These keys name the reader-derived metadata attached to every emitted document, so a downstream splitter or retriever can rely on them being present rather than re-deriving them from content.

Variables

This section is empty.

Functions

This section is empty.

Types

type Reader

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

Reader extracts documents from HTML.

func NewReader

func NewReader(source io.Reader, config ReaderConfig) (*Reader, error)

NewReader compiles the selector at construction so an invalid one fails where it is configured rather than on every document. Unlike PDF, the budget is enforced while reading because an io.Reader cannot report its length in advance.

func (*Reader) Read

func (r *Reader) Read(ctx context.Context) ([]*document.Document, error)

Read parses the source and emits documents according to the configuration. Context cancellation is honored around parsing and between matches.

type ReaderConfig

type ReaderConfig struct {
	Selector           string
	SourceName         string
	PreserveWhitespace bool
	Metadata           coremetadata.Map
	SourceBudget       etl.SourceBudget
}

ReaderConfig controls HTML extraction. By default whitespace runs are collapsed; PreserveWhitespace retains the source spacing instead. Metadata is cloned by NewReader, and reader-derived html.* keys take precedence on conflict. A zero SourceBudget uses etl.DefaultMaxSourceBytes.

Jump to

Keyboard shortcuts

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