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 ¶
- type Builder
- func (b *Builder) Append(t []byte, m Mode)
- func (b *Builder) AppendRune(c rune, m Mode)
- func (b *Builder) AppendString(t string, m Mode)
- func (b *Builder) Break()
- func (b *Builder) Close(h Span)
- func (b *Builder) Empty() bool
- func (b *Builder) Finish() (text string, spans []gofastcore.Span, display gofastcore.Display)
- func (b *Builder) Len() int
- func (b *Builder) LineBreak()
- func (b *Builder) LineEnd()
- func (b *Builder) Open(s gofastcore.Span) Span
- func (b *Builder) OpenText(h Span) []byte
- func (b *Builder) Reset()
- func (b *Builder) Space()
- func (b *Builder) Text() []byte
- type Mode
- type Span
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 ¶
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) AppendRune ¶ added in v0.1.23
AppendRune is Append for one character, for a reader that reads a character at a time (RTF): a printable ASCII character is written as it is, without the work of a run. A carriage return and a line feed added one at a time are two line ends, not a pair.
func (*Builder) AppendString ¶
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 ¶
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 says what the last span recorded says, with only white space between them, extends it.
func (*Builder) Empty ¶
Empty reports whether no text has been written since the block began (white space held back is not text).
func (*Builder) Finish ¶
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 ¶
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 ¶
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 ¶
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.
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 )