blocktext

package
v0.1.21 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 6 Imported by: ? 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 blocktext builds the text of a block from the pieces a reader finds, with its spans, in one place for every library.

A span is a byte range of the text, and the text changes as it is built: white space is collapsed, held back until text follows it, taken off before a line break, trimmed at the ends. A reader that records a span's start as a number while that goes on can record the wrong one: go-fast-xml and go-fast-html each did, where a line break took white space that a span's start had counted. Here a reader never holds a position. It opens a span when its element starts and closes it when it ends, and the builder keeps the start true through whatever the text does in between.

What a reader decides is the format's, not the builder's: which white space mode a piece of text is in (Collapse for HTML, OpenDocument and XML; Keep for Word, RTF and the binary formats, and where a format asks to keep it), where its line breaks are, and what its spans say.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

type Builder struct {
	// Flags are what is applied to text besides validity and control
	// characters, which always are: clean.Ligatures for prose, nothing
	// (clean.Verbatim) for code.
	Flags clean.Flags
	// Removed counts the control characters removed, over the builder's
	// life: Reset keeps it.
	Removed int
	// KeepIndent keeps white space at the start of the block, as code
	// keeps its indentation; KeepTrailing keeps it at the end. Without
	// them, both ends are trimmed.
	KeepIndent, KeepTrailing bool
	// contains filtered or unexported fields
}

Builder builds the text of one block at a time. Its zero value is ready to use. After Finish it is ready for the next block, with the spans that were open still open, from the start of that block.

func (*Builder) Append

func (b *Builder) Append(t []byte, m Mode)

Append adds text in mode m. It is cleaned as it is added, by clean.Append with b.Flags: valid UTF-8 (a byte that is no character is U+FFFD), no control characters but line feed and tab, line and page separators as line feeds, and with clean.Ligatures the ligatures as letters. White space is read as m says, and held back until text follows it. t is only read.

func (*Builder) AppendString

func (b *Builder) AppendString(t string, m Mode)

AppendString is Append for a string. The string is read in place, not copied.

func (*Builder) Break

func (b *Builder) Break()

Break adds a hard break: all the white space held back becomes one line feed.

func (*Builder) Close

func (b *Builder) Close(h Span)

Close closes a span: it covers the text written since it was opened, less white space at its start (white space after its text is still held back, not written), and nothing if that is none. A span that starts where the last span recorded ends, and says the same, extends it.

func (*Builder) Empty

func (b *Builder) Empty() bool

Empty reports whether no text has been written since the block began (white space held back is not text).

func (*Builder) Finish

func (b *Builder) Finish() (text string, spans []gofastcore.Span, display gofastcore.Display)

Finish ends the block. It returns the text, trimmed of white space at both ends (but as KeepIndent and KeepTrailing say), its spans and its display reasons. Spans still open give the block the part of their text read so far and stay open, from the start of the next block (an element that a block boundary falls inside). The reasons are placed by hidden.Settle: one that covers all the text is the block's, the others stay on their spans, and display is every reason the block has.

func (*Builder) Len

func (b *Builder) Len() int

Len returns the bytes of text held, white space held back included: what a budget is charged for.

func (*Builder) LineBreak

func (b *Builder) LineBreak()

LineBreak adds a line break the markup writes (HTML's <br>, Word's w:br): spaces held back before it are dropped, and a line feed is held, so that two breaks give two. Like other white space, breaks are dropped at the ends of the block.

func (*Builder) LineEnd

func (b *Builder) LineEnd()

LineEnd ends the line, as a block boundary inside a table cell or a list item does: a LineBreak, unless the text is empty or already ends in a line feed, written or held.

func (*Builder) Open

func (b *Builder) Open(s gofastcore.Span) Span

Open opens a span with s's style, link and display reasons, starting where the next text will go: after white space held back now, which is not the span's. Spans may be open in any number and closed in any order.

func (*Builder) OpenText

func (b *Builder) OpenText(h Span) []byte

OpenText returns the text of an open span written so far, less white space at its start: what a reader stores of a link before it closes. It is the builder's: it is good until the next call.

func (*Builder) Reset

func (b *Builder) Reset()

Reset empties the builder for a new block and forgets the spans open.

func (*Builder) Space

func (b *Builder) Space()

Space adds a boundary of white space, as collapsed white space is: one space, if the text has some and does not already end in white space.

func (*Builder) Text

func (b *Builder) Text() []byte

Text returns the text written so far, without the white space held back. It is the builder's: it is good until the next call.

type Mode

type Mode uint8

Mode says how the white space of a piece of text is read: space, tab, and the line ends (line feed, carriage return, form feed, line tabulation, next line, line and paragraph separators).

const (
	// Collapse reads a run of white space as one space, and none at the
	// start of the block or after a line break: HTML, OpenDocument, XML.
	Collapse Mode = iota
	// Keep keeps white space as written, and each line end (a carriage
	// return and a line feed together are one) as a line feed:
	// Word, RTF, the binary formats, CSS white-space pre, xml:space
	// "preserve".
	Keep
	// KeepLineEnds keeps line ends and collapses the rest (CSS
	// white-space pre-line): a line end is a LineBreak.
	KeepLineEnds
)

type Span

type Span int

Span is a span opened with Open, to be closed with Close.

Jump to

Keyboard shortcuts

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