Documentation
¶
Overview ¶
Package text provides functionalities to manipulate texts.
Index ¶
- Constants
- type BlockReader
- type Index
- type Lines
- type LinesInput
- type MultilineValue
- type MultilineValueBuilder
- func (b *MultilineValueBuilder) AddIndex(i Index)
- func (b *MultilineValueBuilder) AddSegment(seg Segment)
- func (b *MultilineValueBuilder) Build() MultilineValue
- func (b *MultilineValueBuilder) Collection() []Index
- func (b *MultilineValueBuilder) IsCollection() bool
- func (b *MultilineValueBuilder) IsOwned() bool
- func (b *MultilineValueBuilder) IsSingle() bool
- func (b *MultilineValueBuilder) SetString(s string)
- func (b *MultilineValueBuilder) Single() Index
- type MultilineValueInput
- type Reader
- type Segment
- func (t Segment) Between(other Segment) Segment
- func (t Segment) Bytes(buffer []byte) []byte
- func (t Segment) IsEmpty() bool
- func (t Segment) Len() int
- func (t Segment) Str(buffer []byte) string
- func (t Segment) TrimLeftSpace(buffer []byte) Segment
- func (t Segment) TrimLeftSpaceWidth(width int, buffer []byte) Segment
- func (t Segment) TrimRightSpace(buffer []byte) Segment
- func (t Segment) WithStart(v int) Segment
- func (t Segment) WithStop(v int) Segment
- type Value
- type ValueInput
Constants ¶
const EOF = byte(0xff)
EOF indicates the end of file.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BlockReader ¶
type BlockReader interface {
Reader
// Reset resets current state and sets new segments to the reader.
Reset(segs []Segment)
}
A BlockReader interface is a reader that is optimized for Blocks.
func NewBlockReader ¶
func NewBlockReader(source []byte, segs []Segment) BlockReader
NewBlockReader returns a new BlockReader.
type Index ¶
An Index represents a position range in a source byte slice. Unlike Segment, Index does not carry parsing metadata such as Padding or ForceNewline.
func NewIndexFromSegment ¶
NewIndexFromSegment returns a new Index derived from the given Segment, dropping any padding and ignoring ForceNewline.
type Lines ¶
type Lines struct {
// contains filtered or unexported fields
}
A Lines value holds the raw content of a block node. It is either an owned string or a sequence of Segment values copied from the parser's source-segment list.
Lines is used for block nodes whose rendered content is taken directly from the source (CodeBlock, FencedCodeBlock, HTMLBlock) so that the content is self-contained and can be serialised without access to the original source.
func NewLines ¶
func NewLines[T LinesInput](v T) Lines
NewLines returns a Lines from the given input, which may be a string, byte slice, or slice of Segment.
func NewSegmentsLines ¶
NewSegmentsLines returns a Lines backed by source segments.
func NewStringLines ¶
NewStringLines returns a Lines backed by an owned string.
func (*Lines) AppendSegment ¶
AppendSegment appends a Segment to the internal segment list.
func (Lines) Bytes ¶
Bytes returns the concatenated byte content of all segments, or the owned string. The returned byte slice is read-only and should not be modified.
func (Lines) IsOwned ¶
IsOwned returns true if this value is an owned string not derived from the source byte slice.
type LinesInput ¶
LinesInput is a type constraint for types that can be converted to a Lines.
type MultilineValue ¶
type MultilineValue struct {
// contains filtered or unexported fields
}
A MultilineValue holds a potentially multiline inline value that is either an owned string or a set of source position ranges. Use MultilineValue for data that may span multiple lines per the CommonMark spec (e.g. link labels).
When constructing the byte value from source positions, the ranges are concatenated and any trailing newline of each range is replaced with a single space, matching CommonMark's line-folding rules.
func NewIndexMultilineValue ¶
func NewIndexMultilineValue(i Index) MultilineValue
NewIndexMultilineValue returns a MultilineValue backed by a single source position.
func NewIndicesMultilineValue ¶
func NewIndicesMultilineValue(indices []Index) MultilineValue
NewIndicesMultilineValue returns a MultilineValue backed by source positions.
func NewMultilineValue ¶
func NewMultilineValue[T MultilineValueInput](v T) MultilineValue
NewMultilineValue returns a MultilineValue from the given input, which may be a string, byte slice, Index, or slice of Index.
func NewMultilineValueFromSegments ¶
func NewMultilineValueFromSegments(segs []Segment) MultilineValue
NewMultilineValueFromSegments returns a MultilineValue backed by source positions derived from the given segments. Segment padding is dropped since MultilineValue is used for inline content where padding does not apply.
func NewStringMultilineValue ¶
func NewStringMultilineValue(s string) MultilineValue
NewStringMultilineValue returns a MultilineValue backed by an owned string.
func (MultilineValue) Bytes ¶
func (v MultilineValue) Bytes(source []byte) []byte
Bytes returns the byte representation of this value. When backed by source positions:
- a single index: returns the source sub-slice as-is
- two or more indices: the first range is returned as-is, and each subsequent range has leading whitespace trimmed before concatenation
If the value is owned, source is ignored. The returned byte slice is read-only and should not be modified.
func (MultilineValue) IsOwned ¶
func (v MultilineValue) IsOwned() bool
IsOwned returns true if this value is an owned string not derived from the source byte slice.
func (MultilineValue) Str ¶
func (v MultilineValue) Str(source []byte) string
Str returns the string representation of this value. The returned string is read-only and should not be modified. See Bytes() for details on how the string is constructed from source positions.
type MultilineValueBuilder ¶
type MultilineValueBuilder struct {
// contains filtered or unexported fields
}
MultilineValueBuilder is a helper for building a MultilineValue. It optimises for the common case of a single Index by storing it directly in the struct and only allocating a slice if multiple indices are added.
func (*MultilineValueBuilder) AddIndex ¶
func (b *MultilineValueBuilder) AddIndex(i Index)
AddIndex adds an Index to the builder.
func (*MultilineValueBuilder) AddSegment ¶
func (b *MultilineValueBuilder) AddSegment(seg Segment)
AddSegment adds an Index derived from the given Segment to the builder.
func (*MultilineValueBuilder) Build ¶
func (b *MultilineValueBuilder) Build() MultilineValue
Build returns a MultilineValue from the accumulated state. If SetString was called, the result is backed by that string. Otherwise, the result is backed by the accumulated indices.
func (*MultilineValueBuilder) Collection ¶
func (b *MultilineValueBuilder) Collection() []Index
Collection returns the slice of Index values in the builder. The result is meaningful only when IsCollection() returns true.
func (*MultilineValueBuilder) IsCollection ¶
func (b *MultilineValueBuilder) IsCollection() bool
IsCollection returns true if the builder contains multiple Indices.
func (*MultilineValueBuilder) IsOwned ¶
func (b *MultilineValueBuilder) IsOwned() bool
IsOwned returns true if the builder contains an owned string value.
func (*MultilineValueBuilder) IsSingle ¶
func (b *MultilineValueBuilder) IsSingle() bool
IsSingle returns true if the builder contains at most one Index.
func (*MultilineValueBuilder) SetString ¶
func (b *MultilineValueBuilder) SetString(s string)
SetString sets an owned string value. When Build is called, the resulting MultilineValue will be backed by this string rather than any accumulated indices.
func (*MultilineValueBuilder) Single ¶
func (b *MultilineValueBuilder) Single() Index
Single returns the single Index in the builder. The result is meaningful only when IsSingle() returns true.
type MultilineValueInput ¶
type MultilineValueInput interface {
string | []byte | Index | []Index | MultilineValue
}
MultilineValueInput is a type constraint for types that can be converted to a MultilineValue.
type Reader ¶
type Reader interface {
io.RuneReader
// Source returns a source of the reader.
Source() []byte
// ResetPosition resets positions.
ResetPosition()
// Peek returns a byte at current position without advancing the internal pointer.
Peek() byte
// PeekLine returns the current line without advancing the internal pointer.
PeekLine() ([]byte, Segment)
// PrecedingCharacter returns a character just before current internal pointer.
PrecedingCharacter() rune
// ValueBetween returns a MultilineValue covering the given [start, stop)
// byte range within the source.
ValueBetween(start, stop int) MultilineValue
// LineOffset returns a distance from the line head to current position.
LineOffset() int
// Position returns current line number and position.
Position() (int, Segment)
// SetPosition sets current line number and position.
SetPosition(int, Segment)
// SetPadding sets padding to the reader.
SetPadding(int)
// Advance advances the internal pointer.
Advance(int)
// AdvanceAndSetPadding advances the internal pointer and add padding to the
// reader.
AdvanceAndSetPadding(int, int)
// AdvanceToEOL advances the internal pointer to the end of line.
// If the line ends with a newline, it will be included in the segment.
// If the line ends with EOF, it will not be included in the segment.
AdvanceToEOL()
// AdvanceLine advances the internal pointer to the next line head.
AdvanceLine()
// SkipSpaces skips space characters and returns a non-blank line.
// If it reaches EOF, returns false.
SkipSpaces() (Segment, int, bool)
// SkipSpaces skips blank lines and returns a non-blank line.
// If it reaches EOF, returns false.
SkipBlankLines() (Segment, int, bool)
// Match performs regular expression matching to current line.
Match(reg *regexp.Regexp) bool
// Match performs regular expression searching to current line.
FindSubMatch(reg *regexp.Regexp) [][]byte
}
A Reader interface provides abstracted method for reading text.
type Segment ¶
type Segment struct {
// Start is a start position of the segment.
Start int
// Stop is a stop position of the segment.
// This value should be excluded.
Stop int
// Padding is a padding length of the segment.
Padding int
// ForceNewline is true if the segment should be ended with a newline.
// Some elements(i.e. CodeBlock, FencedCodeBlock) does not trim trailing
// newlines. Spec defines that EOF is treated as a newline, so we need to
// add a newline to the end of the segment if it is not empty.
//
// i.e.:
//
// “`go
// const test = "test"
//
// This code does not close the code block and ends with EOF. In this case,
// we need to add a newline to the end of the last line like `const test = "test"\n`.
ForceNewline bool
}
A Segment struct holds information about source positions.
func NewSegmentPadding ¶
NewSegmentPadding returns a new Segment with the given padding.
func (Segment) TrimLeftSpace ¶
TrimLeftSpace returns a new segment by slicing off all leading space characters including padding.
func (Segment) TrimLeftSpaceWidth ¶
TrimLeftSpaceWidth returns a new segment by slicing off leading space characters until the given width.
func (Segment) TrimRightSpace ¶
TrimRightSpace returns a new segment by slicing off all trailing space characters.
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
A Value holds a single-line inline value that is either an owned string or a position range within a source byte slice. Use Value for data that must not contain newlines per the CommonMark spec (e.g. link destinations, fenced code block info strings).
func NewIndexValue ¶
NewIndexValue returns a Value backed by a source position.
func NewStringValue ¶
NewStringValue returns a Value backed by an owned string.
func NewValue ¶
func NewValue[T ValueInput](v T) Value
NewValue returns a Value from the given input, which may be a string, byte slice, or Index.
func (Value) Bytes ¶
Bytes returns the byte representation of this value. If the value is owned, source is ignored. The returned byte slice is read-only and should not be modified.
func (Value) Index ¶
Index returns the source position of this value. The result is meaningful only when IsOwned() returns false.