htmlbind

package
v0.4.6 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package htmlbind parses typed HTML template sources into an AST.

Index

Constants

View Source
const DefaultActionAttr = "data-tb-action"

DefaultActionAttr is the attribute the lowering writes when GenerateOptions.ServerActionAttr is empty. A framework driving its own client library points it at that library's vocabulary instead.

View Source
const DefaultCSRFFieldName = "_csrf"

DefaultCSRFFieldName is the hidden field an unsafe form carries. It is a generation-time name rather than a render-time one, so the whole tag but its value folds into static bytes.

View Source
const DefaultDataAttributePrefix = "tb"

DefaultDataAttributePrefix names the generated data attributes. A project whose markup already uses this prefix overrides it through GenerateOptions.

View Source
const DefaultPublicURLBase = "/public/generated"

DefaultPublicURLBase is the URL prefix used when a project configures none. Extraction always happens, so a zero-configuration project still gets working asset URLs.

View Source
const ReservedPageFunc = "Load"

ReservedPageFunc is the Go entry point name a route package gives its own page, which is therefore never a server action.

View Source
const ServerActionAttr = "server-action"

ServerActionAttr is the reserved attribute naming a Go handler instead of a URL. It never reaches the output; the compiler replaces it with the attribute that carries the handler's endpoint.

See decision:server-action-lowering. The value is a static handler name because the symbol has to resolve at generation, and only the caller can resolve it: the URL depends on the route the template serves, which the compiler cannot see. That is why lowering takes two passes, with ActionRefs reporting what a module references and GenerateOptions.ServerActions carrying the answers back.

Variables

This section is empty.

Functions

func CleanOutputName added in v0.3.1

func CleanOutputName(name string) (string, error)

CleanOutputName normalizes a produced file name and refuses one that leaves the output root, because a caller writes these files unexamined.

func Generate

func Generate(filename string, source []byte, options GenerateOptions) ([]byte, error)

Generate compiles an HTML template module to Go, discarding the extracted assets. Callers that write files use GenerateModule instead.

func JoinPublicURL added in v0.2.9

func JoinPublicURL(base, name string) string

JoinPublicURL appends a generated file name to the configured URL base. The base is used verbatim, so an absolute URL path and a full CDN URL behave the same and no path segment is ever inferred.

func Printer added in v0.3.1

func Printer() syntax.BodyPrinter

Printer returns the HTML body printer, the printing half of the format parser registered by Parse. It implements rule:html-template-layout.

func RootPrinter added in v0.3.1

func RootPrinter() syntax.RootPrinter

RootPrinter is the registration the shared module printer needs.

func ValidateReferenceHooks added in v0.3.1

func ValidateReferenceHooks(hooks []ReferenceHook) error

ValidateReferenceHooks checks a registration set before any template is read, so a malformed hook fails at the generate command rather than at a template position it has nothing to do with.

Two hooks may share an element and attribute pair; whether they collide depends on the values a template writes, so that is checked at use.

Types

type ActionRef added in v0.2.3

type ActionRef struct {
	// Component is the declaration the reference appears in.
	Component string
	// Handler is the Go function name the attribute named.
	Handler string
	// Element is the element carrying the attribute, such as form or button.
	Element string
	// Pos is the attribute position, for a diagnostic that can quote the source.
	Pos Position
}

ActionRef is one server-action reference found in a template.

func ActionRefs added in v0.2.3

func ActionRefs(filename string, source []byte) ([]ActionRef, error)

ActionRefs parses and analyzes a template module and returns every server-action reference it makes, in source order.

It is the first of the two passes lowering needs: a caller resolves these names against the Go package beside the template, derives an endpoint URL for each, and passes the result to Generate as GenerateOptions.ServerActions.

Like Signatures it runs the same analysis Generate does, so a module that fails to compile fails here with the same diagnostic rather than yielding a partial answer.

type Annotation added in v0.1.16

type Annotation = syntax.Annotation

type Asset added in v0.2.9

type Asset struct {
	Kind AssetKind
	// Base is the file name without its extension.
	Base string
	// Extension is css for a stylesheet and js for a script, without a dot.
	Extension string
	Content   []byte
	// URL is the reference written into the emitted link or script tag.
	URL string
}

Asset is one static file extracted from component head declarations. The content hash is part of the name, so a reference URL is immutably cacheable and identical input regenerates identical names and bytes.

func (Asset) FileName added in v0.2.9

func (a Asset) FileName() string

FileName is the name the asset is written under, inside the configured public directory.

func (Asset) MediaType added in v0.3.3

func (a Asset) MediaType() string

MediaType is what the reference tag says this asset is.

type AssetKind added in v0.2.9

type AssetKind string

AssetKind classifies one extracted static file.

const (
	// AssetStyle is the stylesheet bundle of one generation unit.
	AssetStyle AssetKind = "style"
	// AssetScript is the script extracted from one component.
	AssetScript AssetKind = "script"
)

type Attribute

type Attribute struct {
	Kind    string          `json:"kind"`
	Pos     Position        `json:"pos"`
	Name    string          `json:"name"`
	Boolean bool            `json:"boolean,omitempty"`
	Value   []AttributePart `json:"value,omitempty"`
}

type AttributePart

type AttributePart struct {
	Kind       string   `json:"kind"`
	Pos        Position `json:"pos"`
	Context    string   `json:"context,omitempty"`
	Text       string   `json:"text,omitempty"`
	Expression Expr     `json:"expression,omitempty"`
}

type AwaitBinding added in v0.1.16

type AwaitBinding = syntax.AwaitBinding

type AwaitNode added in v0.1.16

type AwaitNode = syntax.AwaitNode

type BinaryExpr

type BinaryExpr = syntax.BinaryExpr

type Body

type Body = []Node

Body is the body stored in TemplateDecl.Body.

type BuiltinElement added in v0.3.3

type BuiltinElement struct {
	// Name is the bare kebab-case element name an author writes.
	Name string
	// Params are the declared attributes, in the order a diagnostic lists them.
	Params []ElementParam
	// Context is the rule:template-context-safety insertion context this element
	// may appear in. Empty means html:child.
	Context string
	// Placement is the region that owns it. A head-only element written in the
	// body is a generation error rather than a page that half works.
	Placement ElementPlacement
	// Vary names the request properties this element's output depends on, such
	// as a cookie its provider reads.
	//
	// It is declared rather than derived, because only the implementation knows
	// what its provider reads. An undeclared axis is an invisible dependency: a
	// caller cannot build a Vary header for it and a shared cache cannot key on
	// it, and neither can find out by looking at the template.
	Vary []string
	// Assets are the static files this element requires. They join the required
	// set of every component that writes it, and their reference tags join its
	// head.
	Assets []Asset
	// Shape is how the output is produced. Empty means BuiltinMarkup.
	Shape BuiltinShape
	// Markup is the fixed output template. A hole is written {{.Name}} and names
	// either a declared parameter or a field of the provider's result; {{.}} is
	// the whole provider result, for a provider returning a bare value.
	//
	// Each hole is escaped for its position, and generation refuses a hole
	// anywhere but element text and an attribute value. That is what makes the
	// output unable to inject markup even if a provider returns hostile bytes.
	Markup string
	// Provider supplies the per-request holes. Nil means the element has none,
	// and then it costs nothing at render time: with no expression parameter
	// either, the whole thing folds into static bytes.
	Provider *ElementProvider
}

BuiltinElement is one framework element the generator rewrites.

type BuiltinShape added in v0.3.3

type BuiltinShape string

BuiltinShape names how a builtin element produces its output.

const BuiltinMarkup BuiltinShape = "markup"

BuiltinMarkup lowers a fixed markup template with named holes. It is the only shape this milestone ships.

The opaque shape — a provider returning a trusted value or a fragment, for output whose structure varies rather than only its values — is designed and not built. Its cost is that the trust assertion moves into framework code and the generator can no longer verify the emitted structure, which is why the verifiable shape went first.

type CSRFMode added in v0.3.3

type CSRFMode string

CSRFMode says whether generated forms carry a token.

const (
	// CSRFAuto is the zero value: every unsafe form gets the hidden field, and
	// a component reaching one becomes per-request.
	CSRFAuto CSRFMode = ""
	// CSRFOff emits no field and marks nothing per-request.
	//
	// It is for a deployment that has settled on Origin and Fetch Metadata
	// checks alone. Those are genuinely close to sufficient for a single origin,
	// and turning the token off is what gives such a deployment its cacheable
	// form-bearing components back.
	//
	// What it does not turn off is the origin checking itself, which this module
	// never performs: that is a check on an inbound request before any render, so
	// it belongs to middleware, and a deployment declines it by not wrapping its
	// handlers.
	CSRFOff CSRFMode = "off"
)

type CallExpr

type CallExpr = syntax.CallExpr

type CommentNode

type CommentNode struct {
	Kind string   `json:"kind"`
	Pos  Position `json:"pos"`
	Text string   `json:"text"`
}

func (*CommentNode) NodeType

func (n *CommentNode) NodeType() string

type CompileError

type CompileError struct {
	Filename string
	Pos      Position
	Message  string
}

func (*CompileError) Error

func (e *CompileError) Error() string

type ComponentNode

type ComponentNode struct {
	Kind        string      `json:"kind"`
	Pos         Position    `json:"pos"`
	Name        string      `json:"name"`
	Arguments   []Attribute `json:"arguments,omitempty"`
	Children    []Node      `json:"children,omitempty"`
	SelfClosing bool        `json:"selfClosing,omitempty"`
}

func (*ComponentNode) NodeType

func (n *ComponentNode) NodeType() string

type ConditionalExpr

type ConditionalExpr = syntax.ConditionalExpr

type ConversionInputs added in v0.3.1

type ConversionInputs struct {
	// Sources are the authored files the conversion reads. Their contents are
	// digested into the cache key, and they are recorded as build inputs, so an
	// edit to one both invalidates the cached result and regenerates.
	Sources []string
	// Params is everything else the output depends on: the target format, the
	// quality setting, the version of the encoder doing the work. Anything left
	// out here is something a cache hit will silently ignore.
	Params string
}

ConversionInputs is everything one conversion's output depends on, named without doing the conversion.

type Declaration

type Declaration = syntax.Declaration

type DoctypeNode

type DoctypeNode struct {
	Kind string   `json:"kind"`
	Pos  Position `json:"pos"`
	Text string   `json:"text"`
}

func (*DoctypeNode) NodeType

func (n *DoctypeNode) NodeType() string

type DynamicReference added in v0.3.1

type DynamicReference struct {
	Hook      string
	Element   string
	Attribute string
	Pos       Position
}

DynamicReference is an attribute a hook is registered for whose value is a template expression, and therefore does not exist at generation time.

It is reported rather than ignored: a page half rewritten with nothing said about it is the failure mode this seam exists to avoid. Set StrictReferenceHooks to make it a compile error instead.

type ElementNode

type ElementNode struct {
	Kind        string      `json:"kind"`
	Pos         Position    `json:"pos"`
	Name        string      `json:"name"`
	Attributes  []Attribute `json:"attributes,omitempty"`
	Children    []Node      `json:"children,omitempty"`
	SelfClosing bool        `json:"selfClosing,omitempty"`
}

func (*ElementNode) NodeType

func (n *ElementNode) NodeType() string

type ElementParam added in v0.3.3

type ElementParam struct {
	// Name is the attribute an author writes, in kebab-case.
	Name string
	// Type is the template type name: string, int, bool, and the rest of the
	// core types.
	Type string
	// Required refuses a call site that leaves the attribute unset.
	Required bool
}

ElementParam is one declared attribute of a builtin element. Its expression is type-checked at the call site exactly as an ordinary element's attribute is.

type ElementPlacement added in v0.3.3

type ElementPlacement string

ElementPlacement says which region of a document an element belongs in.

const (
	// PlaceEither is the zero value: the element may appear in the head or in
	// the body.
	PlaceEither ElementPlacement = ""
	// PlaceHead restricts an element to a head contribution.
	PlaceHead ElementPlacement = "head"
	// PlaceBody restricts an element to the document body.
	PlaceBody ElementPlacement = "body"
)

type ElementProvider added in v0.3.3

type ElementProvider struct {
	// Package is the import path of the package holding the function. Empty
	// means the generated package's own.
	Package string
	// Alias overrides the import name. Empty uses the last path segment.
	Alias string
	// Name is the function.
	Name string
	// Result names its first result type, qualified the same way. It is needed
	// because a hole closure has to be written down, and Go infers a call's type
	// arguments but never a function literal's parameter types.
	//
	// For a single-hole element whose provider returns a bare value rather than
	// a struct, this is that value's type and the hole is written {{.}}.
	Result string
}

ElementProvider names the Go function supplying a builtin element's per-request values.

The signature is func(context.Context) (V, error). Nothing here checks it: as with a context-taking external, the caller reads its own Go sources and the Go compiler is the thing that rejects a mismatch. A generator that resolved Go symbols would have to load the target package, which is the dependency this package does not take.

type EnumDecl

type EnumDecl = syntax.EnumDecl

type EnumMember

type EnumMember = syntax.EnumMember

type Expr

type Expr = syntax.Expr

type ExpressionNode

type ExpressionNode = syntax.ExpressionNode

type ExternalDecl

type ExternalDecl = syntax.ExternalDecl

type Field

type Field = syntax.Field

type ForNode

type ForNode = syntax.ForNode

type GenerateOptions

type GenerateOptions struct {
	// Package overrides the template package/module declaration.
	Package string
	// DataAttributePrefix names the generated update protocol data attributes.
	// Empty uses DefaultDataAttributePrefix.
	DataAttributePrefix string
	// Unit names the generation unit whose assets are extracted. Empty derives
	// it from the template file name.
	Unit string
	// PublicURLBase prefixes generated asset file names in head references.
	// Empty uses DefaultPublicURLBase. The value is used verbatim, so an
	// absolute URL path and a full CDN URL behave the same.
	PublicURLBase string
	// ContextExternals names the external functions whose Go implementation
	// takes a leading context.Context. Those calls receive the boundary's
	// context; every other external is called as an ordinary function.
	//
	// The caller discovers this by reading the package's Go sources, so the
	// template declaration stays the same either way and the choice belongs to
	// whoever writes the implementation.
	ContextExternals map[string]bool
	// PreserveWhitespace turns off requirement:static-whitespace-normalization,
	// so static output keeps the authoring indentation and newlines byte for
	// byte. It exists for a project comparing generated markup against
	// pre-existing golden files.
	PreserveWhitespace bool
	// ServerActions maps each handler name a template reaches through
	// ServerActionAttr to the endpoint URL the lowering writes. The caller
	// resolves it, because the URL depends on the route the template serves and
	// the compiler cannot see that; [ActionRefs] reports what needs resolving.
	//
	// A reference with no entry here is a compile error, so a template naming a
	// handler nobody resolved never silently emits a dead element.
	ServerActions map[string]string
	// ServerActionResolver answers a name ServerActions does not hold. It is what
	// lets a framework address a handler from its own route table, for a template
	// that sits outside the tree route discovery walks.
	//
	// The map wins, so configuring a resolver cannot retarget an action a
	// discovered package already declares.
	ServerActionResolver func(name string) (url string, ok bool)
	// ServerActionAttr is the attribute the lowering writes. Empty uses
	// [DefaultActionAttr]. A framework driving an existing client library points
	// it at that library's vocabulary, such as hx-post.
	ServerActionAttr string
	// ReferenceHooks rewrite the static values of the attributes they are
	// registered for, before analysis and before asset extraction, and declare
	// the conversions those rewrites depend on. They are how a build converts a
	// file the template points at, such as an image to a modern format or a
	// TypeScript entry point to JavaScript, without this package holding a
	// converter or a naming rule.
	//
	// A hook converts and returns the bytes, so it may decide the rewrite from
	// how the conversion turned out. A hook declaring a CacheKey lets the caller
	// reuse a stored result instead of converting again.
	//
	// Registering none leaves output byte-identical.
	ReferenceHooks []ReferenceHook
	// CSRFMode turns the automatic CSRF field off. Empty is [CSRFAuto], which
	// puts the hidden field in every unsafe form.
	CSRFMode CSRFMode
	// CSRFFieldName renames the hidden field. Empty uses
	// [DefaultCSRFFieldName]. It has to agree with whatever middleware reads the
	// token back out.
	CSRFFieldName string
	// BuiltinElements are the hyphenated elements a framework contributes, each
	// rewritten at generation time into plan steps. See [BuiltinElement].
	BuiltinElements []BuiltinElement
	// PassthroughElements are the hyphenated elements an application uses and
	// this package emits verbatim: its Web Components, named exactly or by a
	// prefix glob such as "sl-*".
	//
	// Registering neither leaves the hyphenated space closed and empty, so every
	// hyphenated element in a template is a generation error naming the file,
	// line, and column. That is the one behavior change for an existing project,
	// and it is the point: an unrecognized hyphenated element emitted unchanged
	// renders nothing and reports nothing.
	PassthroughElements []PassthroughElement
	// StrictReferenceHooks turns an expression-valued attribute at a registered
	// element and attribute pair into a compile error. It is off by default,
	// because a project may legitimately mix authored references with
	// user-supplied ones; [Result.DynamicReferences] reports them either way.
	StrictReferenceHooks bool
}

GenerateOptions controls the generated Go file and the static files extracted alongside it.

type HeadEntry added in v0.3.5

type HeadEntry struct {
	// Element is link, script, or style. Nothing else is accepted: a hook
	// naming a title, a base, or a meta charset would be rewriting the document
	// rather than loading what it produced.
	Element string
	// Attributes are the attribute values, keyed by name. A value is written as
	// a static attribute and escaped for its own position, so a transform hands
	// over a plain URL and never a pre-escaped one.
	Attributes map[string]string
}

HeadEntry is one tag a conversion needs in the head of the component that referenced it.

type HeadNode added in v0.1.15

type HeadNode struct {
	Kind     string   `json:"kind"`
	Pos      Position `json:"pos"`
	Children []Node   `json:"children,omitempty"`
}

HeadNode is a head element declared outside the document shell. Its children are hoisted into the merged document head instead of being emitted in place.

func (*HeadNode) NodeType added in v0.1.15

func (n *HeadNode) NodeType() string

type HookError added in v0.3.1

type HookError struct {
	Hook    string
	Message string
}

HookError is a registration failure, which has no template position because nothing has been read yet.

func (*HookError) Error added in v0.3.1

func (e *HookError) Error() string

type IdentifierExpr

type IdentifierExpr = syntax.IdentifierExpr

type IfNode

type IfNode = syntax.IfNode

type ImportDecl

type ImportDecl = syntax.ImportDecl

type IndexExpr

type IndexExpr = syntax.IndexExpr

type LiteralExpr

type LiteralExpr = syntax.LiteralExpr

type MemberExpr

type MemberExpr = syntax.MemberExpr

type Module

type Module = syntax.Module

func Parse

func Parse(filename string, source []byte) (*Module, error)

Parse parses one template source using the shared declarations and the HTML component root declaration.

type Node

type Node = syntax.Node

type PackageDecl

type PackageDecl = syntax.PackageDecl

type Parameter

type Parameter = syntax.Parameter

type ParseError

type ParseError = syntax.ParseError

type PassthroughElement added in v0.3.3

type PassthroughElement struct {
	Name string
}

PassthroughElement is a hyphenated element emitted verbatim.

Name is either an exact element name or a prefix glob such as "sl-*", so a component library is declared once rather than per element.

type Position

type Position = syntax.Position

type ProducedFile added in v0.3.1

type ProducedFile struct {
	// Name is the file name relative to the output root. It may carry directory
	// separators and may not escape the root.
	Name string
	// MediaType is what the file should be served as, or empty.
	MediaType string
	Content   []byte
}

ProducedFile is one file a conversion produced, ready for the caller to write.

type ReferenceHook added in v0.3.1

type ReferenceHook struct {
	// Name identifies the hook in diagnostics and in the rewrite report.
	Name string
	// Element is the exact lowercase element name, such as img. A hyphenated
	// name is out of scope: that space belongs to the builtin element
	// whitelist.
	Element string
	// Attribute is the exact attribute name, such as src.
	Attribute string
	// Match reports whether this hook claims a static value. A nil Match claims
	// every static value written at the pair.
	Match func(value string) bool
	// CacheKey names what a conversion of this value depends on, cheaply and
	// without converting anything. A caller holding a store of previous results
	// uses it to answer without calling Transform at all.
	//
	// A nil CacheKey means every build converts, which is correct and slow.
	//
	// The key is only as honest as what it names: an encoder upgrade that no
	// Params string mentions serves stale bytes, and that is the caller's to
	// state because only the caller knows what its converter depends on.
	CacheKey func(ReferenceRequest) (ConversionInputs, error)
	// Transform converts one claimed value and returns both the rewrite and the
	// files it produced, so the rewrite may depend on how the conversion turned
	// out.
	//
	// It is called once per distinct value in the template module being
	// compiled, so a file referenced twenty times on one page is converted once.
	// One module is the widest scope this package has, since it compiles them
	// one at a time; a caller compiling several wraps the transform in its own
	// memo, which is what the generator does.
	//
	// It must be a pure function of what it reads plus its own settings. The
	// file and position on a request are for a diagnostic; deciding an output
	// from them breaks that contract, and any cache built on CacheKey with it.
	//
	// It is called from one goroutine unless the caller asks for more: the
	// generator converts ahead of its compile when configured to, and calls
	// several transforms at once when it does. Being pure is necessary and not
	// sufficient for that - a transform holding a shared scratch buffer is pure
	// by the definition above and unsafe by this one - which is why the caller
	// opts in rather than getting concurrency by default.
	Transform func(ReferenceRequest) (ReferenceResult, error)
}

ReferenceHook matches one element and attribute pair and rewrites the static values written there.

Registration is per generate command, so a project registering none regenerates byte-identical output and pays nothing.

func (ReferenceHook) MarshalJSON added in v0.3.1

func (h ReferenceHook) MarshalJSON() ([]byte, error)

MarshalJSON gives a hook a stable identity for a caller that hashes its options to decide whether a run can be skipped.

A func value cannot be marshalled at all, so without this the whole options value becomes unhashable and registering one hook would silently turn the incremental skip off. What is emitted is the registration, not the behavior: a transform's behavior is covered by the hash of the generator executable that contains it, so adding, removing, or repointing a hook regenerates and recompiling the command does too.

type ReferenceRequest added in v0.3.1

type ReferenceRequest struct {
	// Hook is the name of the hook that claimed the value.
	Hook string
	// Element and Attribute are the pair that matched.
	Element   string
	Attribute string
	// Value is the attribute value exactly as the template writes it.
	Value string
	// File and Pos locate the first occurrence, for a diagnostic the transform
	// returns.
	File string
	Pos  Position
}

ReferenceRequest is one claimed attribute occurrence handed to a transform.

func CollectReferences added in v0.3.5

func CollectReferences(filename string, source []byte, hooks []ReferenceHook) ([]ReferenceRequest, error)

CollectReferences reports every distinct value the registered hooks would claim in one module, without calling a single transform.

It exists so a caller can convert ahead of compiling instead of inside it. A conversion is seconds and a compile is microseconds, and the compile is sequential because each rewritten value has to fold into the module being compiled; converting what the compile will ask for before it asks takes that cost off a single goroutine without moving the decision, which still belongs to the transform and still depends on the bytes it produced.

The walk is the one the rewrite uses, so a value this misses is a value the rewrite would have missed too. Nothing is validated and nothing is reported: an overlap, a dynamic reference, and a strict-mode failure all belong to the compiling pass, which reports them at the same position in the same order whether or not this ran.

type ReferenceResult added in v0.3.1

type ReferenceResult struct {
	// Value replaces the attribute value. It is ignored when Skip is set.
	Value string
	// Skip leaves the attribute exactly as written.
	Skip bool
	// Reason explains a skip in the rewrite report.
	Reason string
	// Files are the files this conversion produced. They may outnumber the
	// rewrite: a source map is produced and no attribute names it.
	Files []ProducedFile
	// Read lists files the transform read beyond the sources its CacheKey
	// named, such as the modules a TypeScript entry point imports. What is
	// named by neither is not hashed, and an edit to it will not regenerate.
	Read []string
	// Head declares tags the component's head needs because of this conversion.
	//
	// A rewrite replaces one attribute on an element that already exists, so a
	// conversion producing a file nothing names has no way to get it loaded: a
	// TypeScript entry point importing a CSS module emits a companion
	// stylesheet, and no rewritten src can introduce its link. That is what
	// this is for.
	//
	// An entry joins the contributions of the component whose template held the
	// matched attribute, after everything that component's author wrote, so a
	// component rendered on one page in forty contributes to that page only. A
	// skip contributes nothing, because a conversion that declined produced no
	// file to load.
	Head []HeadEntry
}

ReferenceResult is what a transform returns for one value.

Skip and Value are the two outcomes: a skip leaves the markup alone and says why, which is how a transform declines a conversion that was not worth it - an encode larger than its source, a format already at the target, a vector image. Declining is not an error and is not a silent no-op, and it is cached like any other outcome, so the losing encode runs once and never again.

type Result added in v0.2.9

type Result struct {
	GoSource []byte
	Assets   []Asset
	// Produced holds the files the hooks' conversions created, sorted by name.
	// They may outnumber the rewrites, because a conversion may write a file no
	// attribute names, and the caller writes them so they join the run's
	// declared outputs rather than appearing behind it.
	Produced []ProducedFile
	// Rewrites reports what the hooks did, including what they declined and
	// why. A build-time rewrite is invisible in the template, so the build is
	// the only place it can be seen.
	Rewrites []Rewrite
	// ReadSet lists the files the transforms reported reading beyond the sources
	// their cache keys named. What is named by neither is not hashed, and an
	// edit to it will not regenerate.
	ReadSet []string
	// DynamicReferences are the attributes a hook was registered for whose
	// value is an expression, and so could not be rewritten.
	DynamicReferences []DynamicReference
}

Result is one compiled template module: the generated Go source and the static files requirement:static-asset-extraction pulled out of it.

func GenerateModule added in v0.2.9

func GenerateModule(filename string, source []byte, options GenerateOptions) (Result, error)

GenerateModule parses, validates, and compiles an HTML template module to Go plus its extracted stylesheet and script files.

Each component becomes an immutable render plan: an instruction list typed by its parameter struct, executed by the shared htmlbind coordinator. Generated code owns no response concerns, so it depends on neither net/http nor any content negotiation.

type Rewrite added in v0.3.1

type Rewrite struct {
	Hook      string
	Element   string
	Attribute string
	// From is the authored value and To is what replaced it. To equals From for
	// a skip.
	From string
	To   string
	// Occurrences counts the attributes this one conversion rewrote. A transform
	// call count is a count of distinct values, not of elements.
	Occurrences int
	// Skipped and Reason report a declined rewrite.
	Skipped bool
	Reason  string
	// Pos is the first occurrence in the template.
	Pos Position
}

Rewrite records one distinct value a hook handled, for the build report. An author cannot see a build-time rewrite by reading the template, so the build is the only place it is visible.

type Signature added in v0.2.0

type Signature struct {
	// Name is the declaration name as written in the template.
	Name string
	// Exported reports the export modifier.
	Exported bool
	// Parameters are in declaration order.
	Parameters []SignatureParam
}

Signature is one declaration's contract stated in Go terms.

It exists so a caller that generates code around a template, such as a filesystem router, can read what a component takes without reimplementing the template type system. The Go types here are exactly the ones the generated parameter struct declares.

func Lookup added in v0.2.0

func Lookup(signatures []Signature, name string) (Signature, bool)

Lookup returns the signature with the given name.

func Signatures added in v0.2.0

func Signatures(filename string, source []byte) ([]Signature, error)

Signatures parses and analyzes a template module and returns the Go-typed signature of every component it declares, in declaration order.

It runs the same analysis Generate does, so a module that fails to compile fails here with the same diagnostic rather than yielding a partial answer.

type SignatureParam added in v0.2.0

type SignatureParam struct {
	// Name is the parameter name as written in the template.
	Name string
	// GoType is the Go type of the generated parameter struct field. An async
	// parameter is already wrapped, so it reads htmlbind.Pending[T].
	GoType string
	// TemplateType is the type as written in the template, kept for diagnostics
	// that should quote the source rather than its lowering.
	TemplateType string
	// Async marks a parameter the caller settles through htmlbind.Pending.
	Async bool
	// Slot marks an html parameter, which a wrapper fills rather than a caller
	// passing data.
	Slot bool
}

SignatureParam is one declared parameter of a Signature.

type SlotNode added in v0.1.15

type SlotNode struct {
	Kind     string   `json:"kind"`
	Pos      Position `json:"pos"`
	Name     string   `json:"name,omitempty"`
	Required bool     `json:"required,omitempty"`
	Default  []Node   `json:"default,omitempty"`
}

SlotNode marks where a bound html parameter is inserted. Name is empty for the reserved children parameter. Default holds the content rendered when the bound argument is absent.

func (*SlotNode) NodeType added in v0.1.15

func (n *SlotNode) NodeType() string

func (*SlotNode) Parameter added in v0.1.15

func (n *SlotNode) Parameter() string

Parameter reports the component parameter this slot binds to.

type TemplateDecl

type TemplateDecl = syntax.TemplateDecl

type TextNode

type TextNode struct {
	Kind string   `json:"kind"`
	Pos  Position `json:"pos"`
	Text string   `json:"text"`
}

func (*TextNode) NodeType

func (n *TextNode) NodeType() string

type TypeDecl

type TypeDecl = syntax.TypeDecl

type TypeRef

type TypeRef = syntax.TypeRef

type UnaryExpr

type UnaryExpr = syntax.UnaryExpr

Jump to

Keyboard shortcuts

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