Documentation
¶
Index ¶
- func NewEscapedWriter(w io.Writer) io.Writer
- func Noop(any)
- func Release(r Releaser)
- type Attr
- type AttrModFunc
- type Attrs
- func (a Attrs) AddMod(m Modify)
- func (a Attrs) ApplyMods(ctx context.Context, tag string) error
- func (a Attrs) Clone() Attrs
- func (a Attrs) Find(name string) (Attr, bool)
- func (a Attrs) Get(name string) Attr
- func (a Attrs) Has(name string) bool
- func (a Attrs) Inherit(attrs Attrs)
- func (a Attrs) List() []Attr
- type Comp
- type Cursor
- func (c Cursor) Any(any any) error
- func (c Cursor) AttrMod(mods ...Modify) error
- func (c Cursor) AttrSet(name string, value any) error
- func (c Cursor) Bytes(data []byte) error
- func (c Cursor) Close() error
- func (c Cursor) Comp(comp Comp) error
- func (c Cursor) CompCtx(ctx context.Context, comp Comp) error
- func (c Cursor) Context() context.Context
- func (c Cursor) Editor(editor Editor) error
- func (c Cursor) Fprint(any any) error
- func (c Cursor) Init(tag string) error
- func (c Cursor) InitContainer() error
- func (c Cursor) InitVoid(tag string) error
- func (c Cursor) Many(many ...any) error
- func (c Cursor) NewID() uint64
- func (c Cursor) Raw(text string) error
- func (c Cursor) Send(job Job) error
- func (c Cursor) Submit() error
- func (c Cursor) Templ(templ Templ) error
- func (c Cursor) TemplCtx(ctx context.Context, templ Templ) error
- func (c Cursor) Text(text string) error
- type Editor
- type EditorFunc
- type Elem
- type HeadError
- type HeadKind
- type Job
- type JobBytes
- type JobComp
- type JobError
- type JobFprint
- type JobHeadClose
- type JobHeadOpen
- type JobRaw
- type JobTempl
- type JobText
- type Modify
- type Mutate
- type Output
- type OutputError
- type Printer
- type Proxy
- type ProxyFunc
- type Releaser
- type Templ
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Attr ¶
type Attr = *attr
Attr is a handle to a single attribute entry (name + value).
Attr values are typically obtained from Attrs.Get/Find/List.
func (Attr) IsSet ¶
IsSet reports whether this attribute should be considered present.
Rules:
- nil Attr => false
- stored value is bool => that bool value (true=set, false=unset)
- otherwise => value != nil
func (Attr) OutputName ¶ added in v0.0.38
OutputName writes only the attribute name to w.
This is a low-level helper for custom rendering pipelines. Formatting/escaping are defined by GoX’s internal attribute writer.
func (Attr) OutputValue ¶ added in v0.0.38
OutputValue writes only the attribute value to w.
This is a low-level helper for custom rendering pipelines. Formatting/escaping are defined by GoX’s internal attribute writer.
func (Attr) Set ¶
Set sets the attribute value.
Special case: if value implements Mutate, Set computes the stored value as value.Mutate(prev), where prev is the current stored value.
Setting the value to nil unsets the attribute. Setting a bool false also results in the attribute being considered “unset” (see IsSet), though the stored value is false.
type AttrModFunc ¶ added in v0.1.5
type Attrs ¶
type Attrs = *attrs
Attrs is a mutable collection of element attributes.
Attrs stores attributes keyed by name and keeps entries sorted lexicographically (by attribute name). Lookups are performed with binary search.
Attribute names are case-sensitive. For example, "class" and "Class" are distinct entries and are stored/queried independently.
Attribute presence rules (used by Attr.IsSet and Attrs.Has):
- nil => not set
- bool => set only when true (false means “unset”)
- any other non-nil value => set
Lifecycle notes:
- Attrs is intended to be built and used while constructing an element head, then rendered as part of that head.
- Attr handles returned by Get/Find/List are references to entries inside Attrs; mutating an Attr mutates the owning Attrs.
- Attrs is not safe for concurrent use.
func NewAttrs ¶
func NewAttrs() Attrs
NewAttrs allocates a new attribute set.
The returned Attrs starts empty (no modifiers, no entries).
func (Attrs) AddMod ¶ added in v0.0.12
AddMod queues a modifier to be applied by ApplyMods.
Modifiers are executed in the order they are added.
func (Attrs) ApplyMods ¶ added in v0.0.14
ApplyMods executes all queued modifiers on this attribute set.
Modifiers are executed in the order they were added. Each modifier is called at most once; after successful completion, the modifier queue is cleared.
If a modifier returns an error, ApplyMods stops immediately and returns that error. Modifiers that were already executed are discarded; modifiers that were not yet executed remain queued (until the Attrs is discarded/released).
func (Attrs) Clone ¶
Clone returns an independent copy of the attribute set.
The returned Attrs has:
- a copy of the attribute entries (name/value pairs)
- a shallow copy of the modifier list (slice is copied; modifier values are not deep-copied)
Modifying the returned Attrs does not affect the original.
func (Attrs) Find ¶ added in v0.0.37
Find returns the attribute entry for name and whether it exists.
Find does not create a new entry when the name is missing. If the attribute exists, it may be set or unset; use Attr.IsSet to distinguish.
func (Attrs) Get ¶
Get returns the attribute entry for name, creating it if it does not exist.
Entries are kept sorted lexicographically by name; Get inserts a new entry in the correct position.
The returned Attr is a handle into this Attrs; calling Set/Unset mutates this Attrs.
func (Attrs) Has ¶ added in v0.0.31
Has reports whether an attribute exists and is set (per Attr.IsSet).
func (Attrs) Inherit ¶ added in v0.0.23
Inherit copies all “set” attributes from attrs into a.
For each attribute in attrs:
- if it is not set (per Attr.IsSet), it is ignored
- otherwise, a.Get(name).Set(value) is performed
Note: because this uses Attr.Set, if the target attribute already has a value and the inherited value implements Mutate, the inherited value may be computed from the target’s previous value.
type Comp ¶
type Comp interface {
Main() Elem
}
Comp is the minimal component interface in GoX.
A component produces its content by returning an Elem from Main. Main may return nil to render nothing.
type Cursor ¶
type Cursor = *cursor
Cursor is the low-level rendering cursor used by GoX.
Cursor streams rendering operations to a Printer as a sequence of Jobs. It maintains a stack of active element “heads” to validate nesting and enforce a simple state machine:
Regular element lifecycle:
- Init(tag)
- (optional) AttrSet / AttrMod
- Submit() // emits head-open job
- emit children jobs // Text/Comp/Any/etc.
- Close() // emits head-close job
Void element lifecycle:
- InitVoid(tag)
- (optional) AttrSet / AttrMod
- Submit() // emits head-open job; no children and no Close
Container lifecycle:
- InitContainer() // emits container head-open job immediately
- emit children jobs
- Close() // emits container head-close job
Content state ¶
Several methods require the cursor to be in a content state, meaning:
- no element head is active (top-level), OR
- the current element/container head has already been submitted with Submit, and may accept children.
Cursor is not safe for concurrent use.
func NewCursor ¶
NewCursor constructs a Cursor that emits jobs to printer. ctx is used as the default context for jobs that accept a context.
The returned cursor starts in an “opened” state at top-level: it is valid to emit content immediately, or to begin a new element via Init methods.
func (Cursor) Any ¶ added in v0.0.7
Any renders a value using GoX’s default dynamic dispatch.
Defined types include:
- string / []string
- Elem / []Elem
- Comp / []Comp
- Job / []Job
- Editor
- Templ
- []interface{} (treated as a variadic list)
nil values are ignored. Other types fall back to Fprint.
Any requires the cursor to be in content state.
func (Cursor) AttrMod ¶
AttrMod adds one or more attribute modifiers to the current head.
AttrMod may only be used during initialization state (after Init/InitVoid and before Submit). After Submit, AttrMod returns an error.
Attribute modifiers run right before rendering and can inspect or modify the full attribute set for the element.
func (Cursor) AttrSet ¶
AttrSet sets an attribute on the current head.
AttrSet may only be used during initialization state (after Init/InitVoid and before Submit). After Submit, AttrSet returns an error.
func (Cursor) Bytes ¶ added in v0.0.21
Bytes emits a byte-slice payload job at the current cursor position.
Bytes requires the cursor to be in content state.
func (Cursor) Close ¶ added in v0.0.7
Close emits a closing head job for the current element/container.
Close requires that the current head has already been submitted (i.e., Submit was called successfully). Closing before submitting is an error.
Void elements must not be closed.
func (Cursor) Comp ¶ added in v0.0.7
Comp emits a component job at the current cursor position.
Comp requires the cursor to be in content state.
func (Cursor) CompCtx ¶ added in v0.0.12
CompCtx is like Comp, but uses ctx for the emitted job.
CompCtx requires the cursor to be in content state.
func (Cursor) Context ¶ added in v0.0.19
Context returns the default context associated with this cursor.
func (Cursor) Editor ¶ added in v0.0.26
Editor applies editor to this cursor.
Editor is a hook for advanced rendering that needs direct access to cursor methods.
func (Cursor) Fprint ¶ added in v0.0.7
Fprint emits a formatted-print job for any at the current cursor position.
Fprint requires the cursor to be in content state.
func (Cursor) Init ¶ added in v0.0.7
Init begins a new regular (non-void) element head with the given tag name.
After Init, the element is in “initialization” state:
- attributes may be set via AttrSet/AttrMod,
- node content must not be emitted until Submit is called.
func (Cursor) InitContainer ¶ added in v0.0.12
InitContainer begins a synthetic container head and submits it immediately.
Containers do not emit an HTML tag. They exist to group a sequence of jobs under a distinct head id/kind in the job stream.
After InitContainer, the container head is active and must be closed with Close()
func (Cursor) InitVoid ¶ added in v0.0.7
InitVoid begins a new void (self-closing) element head with the given tag name.
After InitVoid, the element is in “initialization” state (attributes may be set). Call Submit to emit the head-open job. Void elements cannot have children and must not be closed.
func (Cursor) Many ¶ added in v0.0.9
Many renders each value in order using Any.
Many requires the cursor to be in content state fot he most types.
func (Cursor) NewID ¶ added in v0.0.22
NewID returns a globally unique id suitable for associating external state with emitted jobs.
IDs are globally unique across cursors created in the same process and are monotonically increasing per cursor.
func (Cursor) Raw ¶ added in v0.0.7
Raw emits raw (unescaped) text at the current cursor position.
Raw requires the cursor to be in content state.
func (Cursor) Send ¶ added in v0.0.27
Send forwards an already-constructed Job directly to the underlying Printer.
Send does not perform state validation; callers are responsible for ensuring job ordering/nesting is valid for their use case.
func (Cursor) Submit ¶ added in v0.0.7
Submit emits an opening head job for the current element.
Submit transitions the current element from “initialization” state to “opened” state. After Submit succeeds:
- attribute mutation is no longer allowed,
- node-content jobs may be emitted into the element,
- the element must eventually be closed with Close() (except for void elements).
func (Cursor) Templ ¶ added in v0.0.7
Templ emits a templ component job at the current cursor position.
Templ requires the cursor to be in content state.
type Editor ¶ added in v0.0.25
Editor performs advanced rendering by operating directly on a Cursor.
Editor is an escape hatch for low-level control (for example, emitting custom jobs or performing cursor-driven edits).
type EditorFunc ¶ added in v0.0.38
func (EditorFunc) Edit ¶ added in v0.0.38
func (e EditorFunc) Edit(cur Cursor) error
type Elem ¶
Elem is the fundamental renderable value in GoX.
Elem is a function that emits rendering jobs through the provided Cursor. Most generated `.gox` output ultimately compiles to one or more Elem values.
Elem implements Comp (Main returns itself) and also implements Templ-compatible rendering via Render(ctx, w).
type HeadError ¶
type HeadError string
HeadError is returned when Cursor element-head operations are performed in an invalid state (for example, writing node content before submitting the current head, or mutating attributes after submission).
It is used to distinguish "render state machine" errors from other failures.
type HeadKind ¶
type HeadKind int
HeadKind describes the kind of an element head currently being built/rendered.
The kind affects how the head is submitted and whether it can have children.
const ( // KindContainer is a synthetic head used to group a sequence of jobs without // emitting an actual HTML tag. It is submitted immediately. KindContainer HeadKind = iota // KindRegular is a normal, non-void HTML element. KindRegular // KindVoid is a void/self-closing HTML element (e.g. <input>, <br>, etc.). // Void heads are submitted as an open job and then removed from the stack; // they never accept children and must not be closed. KindVoid )
type Job ¶
type Job interface {
// Context returns the context associated with this job.
Context() context.Context
Output
}
Job is a unit of rendering work emitted by Cursor and consumed by a Printer.
Each job carries a context and an Output implementation. Printers may use the context for cancellation/deadlines and for propagating errors through the rendering pipeline.
type JobBytes ¶ added in v0.0.21
JobBytes writes raw bytes.
JobBytes is pooled. It releases itself at the end of Output.
func NewJobBytes ¶ added in v0.0.21
NewJobBytes constructs a JobBytes.
The returned job is pooled and must be treated as single-use.
type JobComp ¶
JobComp renders a GoX component.
Output calls Comp.Main() and, if it returns a non-nil Elem, renders it into w.
JobComp is pooled. It releases itself at the end of Output.
func NewJobComp ¶ added in v0.0.8
NewJobComp constructs a JobComp.
The returned job is pooled and must be treated as single-use.
type JobError ¶
JobError represents a job that fails rendering with a stored error.
Output returns Err as-is.
JobError is pooled. It releases itself at the end of Output.
func NewJobError ¶ added in v0.0.20
NewJobError constructs a JobError.
The returned job is pooled and must be treated as single-use.
type JobFprint ¶
JobFprint formats a value with fmt.Fprint, writing to an escaping writer.
This is the default fallback for values that do not have specialized rendering behavior in Cursor.Any.
JobFprint is pooled. It releases itself at the end of Output.
func NewJobFprint ¶ added in v0.0.8
NewJobFprint constructs a JobFprint.
The returned job is pooled and must be treated as single-use.
type JobHeadClose ¶
type JobHeadClose struct {
// ID is the head identifier associated with this element/container.
// The opening and closing jobs for the same head share the same ID.
ID uint64
// Kind describes how this head should be rendered (regular/void/container).
Kind HeadKind
// Tag is the element tag name. It must be non-empty for regular heads.
Tag string
// Ctx is the context associated with this job.
Ctx context.Context
}
JobHeadClose represents a "close head" job.
When rendered, it emits the closing tag for a regular element. For KindContainer it produces no output. Closing a void element is an error.
JobHeadClose is pooled. It releases itself at the end of Output.
func NewJobHeadClose ¶
NewJobHeadClose constructs a JobHeadClose.
The returned job is pooled and must be treated as single-use.
func (*JobHeadClose) Context ¶
func (j *JobHeadClose) Context() context.Context
Context returns the context associated with this job.
func (*JobHeadClose) Output ¶
func (j *JobHeadClose) Output(w io.Writer) error
Output writes the closing tag to w.
Behavior by kind:
- KindContainer: writes nothing and returns nil
- KindVoid: returns an error (void elements cannot be closed)
- KindRegular: requires Tag to be non-empty and writes `</tag>`
type JobHeadOpen ¶
type JobHeadOpen struct {
// ID is the head identifier associated with this element/container.
// The opening and closing jobs for the same head share the same ID.
ID uint64
// Kind describes how this head should be rendered (regular/void/container).
Kind HeadKind
// Tag is the element tag name. It must be non-empty for regular/void heads.
Tag string
// Ctx is the context used for attribute modifiers and downstream render hooks.
Ctx context.Context
// Attrs is the attribute set associated with this head.
Attrs Attrs
}
JobHeadOpen represents an "open head" job.
When rendered, it emits the opening tag and attributes for a regular/void element. For KindContainer it produces no output.
JobHeadOpen is pooled. It releases itself at the end of Output.
func NewJobHeadOpen ¶
func NewJobHeadOpen(id uint64, kind HeadKind, tag string, ctx context.Context, attrs Attrs) *JobHeadOpen
NewJobHeadOpen constructs a JobHeadOpen.
The returned job is pooled and must be treated as single-use. Typical usage is to send the job to a Printer; the job will release itself after Output.
func (*JobHeadOpen) Context ¶
func (j *JobHeadOpen) Context() context.Context
Context returns the context associated with this job.
type JobRaw ¶
JobRaw writes raw (unescaped) text.
JobRaw is pooled. It releases itself at the end of Output.
func NewJobRaw ¶
NewJobRaw constructs a JobRaw.
The returned job is pooled and must be treated as single-use.
type JobTempl ¶
JobTempl renders a templ component (github.com/a-h/templ compatible).
JobTempl is pooled. It releases itself at the end of Output.
func NewJobTempl ¶ added in v0.0.8
NewJobTempl constructs a JobTempl.
The returned job is pooled and must be treated as single-use.
type JobText ¶
JobText writes escaped text.
JobText is pooled. It releases itself at the end of Output.
func NewJobText ¶ added in v0.0.8
NewJobText constructs a JobText.
The returned job is pooled and must be treated as single-use.
type Modify ¶ added in v0.1.6
Modify can inspect and/or mutate element attributes right before the element is rendered.
Modifiers are executed by Attrs.ApplyMods (typically triggered during head rendering). They run in the order they were added and are one-shot: after execution, they are removed from the modifier queue.
type Mutate ¶ added in v0.0.37
Mutate is implemented by values that want to compute the new attribute value based on the previous value.
Attr.Set has special handling for Mutate: if the provided value implements Mutate, Set calls value.Mutate(attributeName, currentValue) and stores the returned value.
type Output ¶ added in v0.0.37
Output is the low-level output interface used by Jobs.
Implementations write their representation to an io.Writer. Output is aliased from an internal utility type.
type OutputError ¶ added in v0.0.44
type OutputError string
OutputError is returned when a Job cannot be rendered due to invalid state (for example, missing tag name, or attempting to close a void element).
func (OutputError) Error ¶ added in v0.0.44
func (e OutputError) Error() string
type Printer ¶
Printer consumes Jobs produced during rendering.
A Printer defines how the job stream is handled. The default implementation created by NewPrinter writes job output sequentially to an io.Writer, but alternative implementations may buffer, transform, parallelize, or analyze jobs before producing final output.
Printer is not required to be safe for concurrent use unless an implementation explicitly documents otherwise.
func NewPrinter ¶
NewPrinter returns the default Printer implementation that writes job output sequentially to w.
type Proxy ¶
Proxy can intercept and transform an element subtree before it is rendered.
Proxy implementations are invoked with the current Cursor and the target Elem. A proxy may emit its own jobs, modify cursor state via Editor-like operations, wrap/replace the element, or render it conditionally.
