web

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package web provides pure HTML search result parsing, URL normalization, domain filtering, and fetched content formatting (HTML-to-Markdown, HTML-to-Text, and JSON/XML pretty-printing) helpers.

Index

Constants

View Source
const MaxSnippetBytes = 300

MaxSnippetBytes is the maximum byte length of one result snippet.

Variables

View Source
var ErrBotChallenge = errors.New("search engine bot protection triggered; try again later or rephrase the query")

ErrBotChallenge is returned when a search endpoint serves a CAPTCHA or anomaly interstitial instead of search results.

Functions

func BuildSearchURL

func BuildSearchURL(endpoint *url.URL, query string) string

BuildSearchURL appends the encoded query to the search endpoint, preserving any query parameters the endpoint itself carries.

func CleanResultURL

func CleanResultURL(raw string) string

CleanResultURL resolves a search engine result link to its destination. Result pages wrap the real URL in a redirect (/l/?uddg=<encoded>) so absolute, relative, and protocol-relative redirect forms all resolve to the target.

func FormatContent

func FormatContent(rawURL, contentType string, body []byte, format Format) string

FormatContent converts a fetched response body into the requested Format based on its Content-Type (or sniffed MIME type when Content-Type is missing or application/octet-stream).

func HTMLToMarkdown

func HTMLToMarkdown(rawHTML string, baseURL string) string

HTMLToMarkdown converts a full HTML document into clean GitHub-Flavored Markdown without cropping layout containers such as nav, header, footer, or aside. Relative links and images are resolved against baseURL when valid.

func HTMLToText

func HTMLToText(rawHTML string) string

HTMLToText converts a full HTML document into readable plain text without Markdown markup tags, preserving paragraph breaks, headings, and list bullets.

func MatchesAnyDomain

func MatchesAnyDomain(host string, domains []string) bool

MatchesAnyDomain reports whether host equals or is a subdomain of any of the given domain patterns, or any pattern is the "*" wildcard. Leading "*." and "." decorations are tolerated.

func ResultHost

func ResultHost(rawURL string) string

ResultHost returns the lowercase hostname of a result URL, or "" when the URL is unparsable or host-less.

Types

type Format

type Format string

Format identifies the output representation used when formatting fetched web content.

const (
	// FormatMarkdown converts HTML to GitHub-Flavored Markdown and pretty-prints
	// JSON/XML responses. It is the default format.
	FormatMarkdown Format = "markdown"

	// FormatText converts HTML to readable plain text and pretty-prints
	// JSON/XML responses.
	FormatText Format = "text"

	// FormatHTML returns the raw response body unmodified.
	FormatHTML Format = "html"
)

func ParseFormat

func ParseFormat(raw string) (Format, error)

ParseFormat normalizes and validates a requested fetch output format. An empty string defaults to FormatMarkdown.

type Hit

type Hit struct {
	Title   string `json:"title"`
	URL     string `json:"url"`
	Snippet string `json:"snippet,omitempty"`
}

Hit is one ranked web search result.

func FilterHits

func FilterHits(hits []Hit, allowed, blocked []string, limit int) ([]Hit, bool)

FilterHits applies the optional domain allow/block filters and the final result cap. Blocked domains win over allowed domains. When limit > 0, it caps the returned hits at limit and reports whether additional matching hits were dropped beyond the cap.

func ParseSearchHTML

func ParseSearchHTML(r io.Reader) ([]Hit, error)

ParseSearchHTML extracts the ranked results from a search engine results page.

Jump to

Keyboard shortcuts

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