Documentation
¶
Overview ¶
Package dom is a Go port of https://github.com/mcy/strings/tree/main/allman, a high-performance meta-formatting library.
The function Render is the primary entry point. It is given a collection of [Tag]s, which represent various formatting directives, such as indentation and grouping.
The main benefit of using this package is the ability to perform smart line wrapping of code. The Group tag can be used to group a collection of tags together that may be rendered in either a "flat" or "broken" orientation. The layout engine will determine whether this element could be laid out flat without going over a configured column limit, and if it would, the group is marked as broken. This can be combined with conditioned tags (viz. Cond) to insert e.g. line breaks at strategic points.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Cond ¶
type Cond byte
Cond is a condition for a tag.
Tags can be conditioned on whether or not they are rendered if the group that they are being rendered in is flat or broken.
type Options ¶
type Options struct {
// The maximum number of columns to render before triggering
// a break. A value of zero implies an infinite width.
MaxWidth int
// The number of columns a tab character counts as. Defaults to 1.
TabstopWidth int
// If true, prints all of the tags in an HTML-like format. Intended for
// debugging.
HTML bool
// If true, [Render] does not append a trailing newline to ensure the
// output ends with one. Trailing break tags are still flushed, so
// the output ends in whatever explicit whitespace was emitted (or
// none, if the last tag was non-whitespace). Useful for callers that
// render snippets to be concatenated, where the safeguard newline
// would introduce an artifact between adjacent renders.
OmitTrailingNewline bool
}
Options specifies configuration for Render.
func (Options) WithDefaults ¶
WithDefaults replaces any unset (read: zero value) fields of an Options which specify a default value with that default value.
type Sink ¶
type Sink func(...Tag)
Sink is a place to append tags. The given tags will be appended to whatever context the sink was created for.
Many functions in this package take a func(push Sink) as an argument. This callback is executed in the context of that tag, and must not be used after the callback returns.
type Tag ¶
type Tag func(*dom)
Tag is data passed to a rendering function.
The various factory functions in this package can be used to construct tags. See their documentation for more information on what tags are available.
The nil tag is equivalent to Text("").
func Group ¶
Group returns a tag that groups together a collection of child tags.
Each group in a document can be broken or flat. Flat groups contain only other flat groups. Groups are broken when:
1. They contain a tag that contains a newline.
2. They contain a broken group.
func GroupIf ¶
GroupIf is like Group, but with a condition attached.
If the condition does not hold in the containing tag, this tag expands to nothing. The outermost level is treated as always broken.
func Indent ¶
Indent pushes by to the indentation stack for all of the given tags.
The indentation stack consists of strings printed on each new line, if that line is otherwise not empty.
Indent cannot be conditioned, because it already has no effect in a flat group.
func Text ¶
Text returns a tag that emits its text exactly.
If text consists only of spaces (U+0020) or newlines (U+000A), it will be treated specially:
Space tags adjacent to a newline will be deleted, so that lines do not have trailing whitespace.
If two space or newline tags of the same rune are adjacent, the shorter one is deleted.