blocks

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 11 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 blocks turns an HTML document tree into the blocks of the shared model: headings, paragraphs, list items, tables, code, quotes, captions, images, comments and form values, with white space collapsed as CSS white-space: normal does, links and styles as spans, and the display reasons the page declares.

Index

Constants

View Source
const (
	NoteAttribute = gofastcore.NoteAttribute
	AttrAttribute = "attribute"
	// AttrChecked is set on a list item that begins with a checkbox (a
	// task-list item): "true" when checked, "false" when not.
	AttrChecked = "checked"
)

The note an attribute's text becomes says so in Attrs: type "attribute" (core's NoteAttribute), and the attribute's name under "attribute".

View Source
const MaxAnchor = 1024

MaxAnchor bounds the length of an id used as Loc.Anchor; a longer id is not used.

View Source
const MaxLang = 64

MaxLang bounds the length of the html element's lang and of a code block's language; a longer one is left out.

View Source
const MaxMeta = 64 << 10

MaxMeta bounds the length of one metadata value.

View Source
const MaxMetaList = 256

MaxMetaList bounds the author and the keyword declarations read.

View Source
const StoredStylesheet = gofastcore.StoredStylesheet

StoredStylesheet is the Attrs["stored"] value of the text of a style element in the stored part.

Variables

This section is empty.

Functions

func Clean

func Clean(s string) (string, int)

Clean makes a string read from outside a page (EPUB metadata, a table of contents) into prose: invalid UTF-8 replaced, white space collapsed, control characters dropped, ligatures written as letters. It returns the number of control characters dropped.

Types

type Config

type Config struct {
	Spans  bool
	Images gofastcore.ImageMode
	Budget *gofastcore.TextBudget
	Decode *gofastcore.DecodeBudget
	// Source maps an offset in the decoded text to one in the source.
	Source func(int) int64
	// Image resolves the address of an image inside the container the
	// page is in; Internal reports an address inside it, which is not
	// counted as an external reference. Both are nil for a page alone.
	Image    func(src string) (ImageRef, bool)
	Internal func(ref string) bool
	// MaxDepth bounds element nesting, counted through iframe srcdoc
	// documents into the page; zero means dom.DefaultMaxDepth.
	MaxDepth int
}

Config says what to produce.

type ImageRef

type ImageRef struct {
	ID, MIME string
	// Data reads the image's bytes, within the decode budget.
	Data func() ([]byte, bool)
}

ImageRef is an image a page refers to that is inside the container the page is in (an EPUB).

type Result

type Result struct {
	Blocks   []gofastcore.Block
	Meta     gofastcore.Metadata
	Title    bool // a <title> was found
	Cut      bool // the text budget ran out; the blocks stop there
	Active   int  // scripts, event handler attributes, javascript: links
	External int  // resources the page loads from elsewhere
	// ImageData counts images given as data: URIs whose bytes could not
	// be decoded.
	ImageData int
	// ImageBudget counts images given as data: URIs that were not
	// decoded, their bytes passing the decode budget: they have no ID
	// and no data.
	ImageBudget int
	// MetaCut counts the metadata values longer than MaxMeta, cut there;
	// TitleCut reports that the title was one of them.
	MetaCut  int
	TitleCut bool
	// AuthorsLeftOut and KeywordsLeftOut count the author and keyword
	// declarations past MaxMetaList, left out.
	AuthorsLeftOut, KeywordsLeftOut int
	// LangLeftOut reports an html lang longer than MaxLang, left out.
	LangLeftOut bool
	// AnchorsLeftOut counts the elements whose id (or an a's name) is
	// longer than MaxAnchor, not used as Loc.Anchor.
	AnchorsLeftOut int
	// LanguagesLeftOut counts the code blocks whose language is longer
	// than MaxLang, left out.
	LanguagesLeftOut int
	// Controls counts the control characters with no meaning as text
	// dropped from the text: of blocks, notes, alt text and metadata
	// values. White space, line ends and tabs are not counted, nor what
	// is dropped from addresses and names (anchors, links, languages).
	Controls int
	// Images counts img elements, whether or not blocks were made.
	Images int
	// Stylesheets counts style elements and linked stylesheets, whose
	// rules are not evaluated.
	Stylesheets int
	// Stored holds what the page keeps and shows nowhere: script and
	// style text, hidden form values. It goes to the stored part.
	Stored []gofastcore.Block
	// FrameDepth: an iframe srcdoc document was not read, being nested
	// at the depth limit. FrameFlattened: one was nested deeper than the
	// limit inside and was read side by side. FrameDecode: the decode
	// budget ran out in a srcdoc document, which was cut there.
	FrameDepth, FrameFlattened, FrameDecode bool
	// FrameCaps reports the bounds of the tree builder reached in iframe
	// srcdoc documents.
	FrameCaps dom.Caps
}

Result is what a walk produced.

func Walk

func Walk(ctx context.Context, root *dom.Node, cfg Config) (*Result, error)

Walk produces the blocks of the tree under root.

Jump to

Keyboard shortcuts

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