dom

package
v0.38.1 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: Apache-2.0 Imports: 9 Imported by: 1 module, 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 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

func Render

func Render(options Options, content func(push Sink)) string

Render renders a document consisting of the given sequence of tags.

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.

const (
	Always Cond = iota
	Flat        // Render only in a flat group.
	Broken      // Render only in a broken group.
)

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

func (o Options) WithDefaults() Options

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

func Group(maxWidth int, content func(push Sink)) Tag

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.

  1. Laid out flat, the group would end past column maxWidth (a value of zero implies no limit). This lets a group keep a tighter limit than the one in Options.

  2. If the group was laid out flat, the current line would exceed the maximum configured column length in Options.

func GroupIf

func GroupIf(cond Cond, maxWidth int, content func(push Sink)) Tag

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

func Indent(by string, content func(push Sink)) Tag

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

func Text(text string) Tag

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.

func TextIf

func TextIf(cond Cond, text string) Tag

TextIf is like Text, 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 Unindent

func Unindent(content func(push Sink)) Tag

Unindent pops the last Indent for all of the given tags.

Unindent cannot be conditioned, because it already has no effect in a flat group.

Jump to

Keyboard shortcuts

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