plugin

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2026 License: Apache-2.0, MIT Imports: 23 Imported by: ? modules, 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 plugin is the public extension API of the Crest compiler. Built-in elements and third-party plugins use the same API; there are no private hooks.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dedent

func Dedent(s string) string

Dedent drops leading and trailing blank lines and removes the indentation shared by every non-blank line.

func ImageSize

func ImageSize(format string, data []byte) (w, h int, err error)

ImageSize returns the pixel size of a PNG, JPEG, or GIF, or the declared size of an SVG (width and height, else the viewBox).

Types

type AssetRef

type AssetRef struct {
	Logical   string `json:"logical"`
	Hash      string `json:"hash"`
	Integrity string `json:"integrity"`
}

AssetRef names an emitted artifact. Identical bytes give an identical ref.

type AssetSink

type AssetSink interface {
	Emit(ctx context.Context, kind, ext string, data []byte) (AssetRef, error)
}

AssetSink stores build artifacts. Emit is content-addressed, idempotent, and safe for concurrent use.

func InlineSink

func InlineSink(maxBytes int) AssetSink

InlineSink returns a sink whose refs are data: URIs, for small artifacts or apps that serve no static files. Artifacts over maxBytes are an error.

type AttrSpec

type AttrSpec struct {
	Name     string   `json:"name"`
	Type     AttrType `json:"type"`
	Required bool     `json:"required,omitempty"`
	Enum     []string `json:"enum,omitempty"`
	Default  string   `json:"default,omitempty"`
	Doc      string   `json:"doc,omitempty"`
}

AttrSpec describes one attribute.

type AttrType

type AttrType string

AttrType is the type of a plugin attribute: one of the constants below, or a Go type name such as "images.Ref" that go build checks.

const (
	String AttrType = "string"
	Int    AttrType = "int"
	Bool   AttrType = "bool"
	Enum   AttrType = "enum"
	URL    AttrType = "url"
)

The built-in attribute types.

type BuildCache

type BuildCache interface {
	Do(ctx context.Context, key Key, fn func(context.Context) ([]byte, error)) ([]byte, error)
}

BuildCache runs fn once per key and keeps its output across builds.

type BuildContext

type BuildContext struct {
	File   string
	Sink   AssetSink
	Cache  BuildCache
	Has    func(tag string) bool
	Report func(Diagnostic)
}

BuildContext is what the compiler hands a plugin's Build.

type Diagnostic

type Diagnostic struct {
	Code     string   `json:"code"`
	Severity Severity `json:"severity"`
	File     string   `json:"file"`
	Line     int      `json:"line"`
	Col      int      `json:"col"`
	Message  string   `json:"message"`
	Fix      *Fix     `json:"fix,omitempty"`
	Doc      string   `json:"doc,omitempty"`
}

Diagnostic is the one diagnostic shape shared by crest check --json, fletch check --json, the dev error page, and MCP.

func DiagnosticAt

func DiagnosticAt(code string, sev Severity, pos ast.Pos, msg string) Diagnostic

DiagnosticAt builds a diagnostic positioned at pos.

type DirAssetSink

type DirAssetSink struct {
	// contains filtered or unexported fields
}

DirAssetSink writes artifacts to a directory as <kind>.<hash8>.<ext> and resolves their logical names ("gen/<file>") to URLs under a prefix. It implements crest.AssetResolver, so it can be installed with crest.WithAssetResolver.

func DirSink

func DirSink(dir, urlPrefix string) *DirAssetSink

DirSink returns a sink that writes into dir and serves under urlPrefix.

func (*DirAssetSink) AssetURL

func (s *DirAssetSink) AssetURL(logical string) string

AssetURL maps "gen/<file>" to "<prefix>/<file>"; other names pass through.

func (*DirAssetSink) Emit

func (s *DirAssetSink) Emit(ctx context.Context, kind, ext string, data []byte) (AssetRef, error)

Emit writes data to the sink's directory as <kind>.<hash>.<ext> and returns its reference. Emitting identical bytes again is a no-op.

type DiskCache

type DiskCache struct {
	// contains filtered or unexported fields
}

DiskCache is a BuildCache stored in a directory, with concurrent callers of the same key sharing one run.

func FileCache

func FileCache(dir string) *DiskCache

FileCache returns a BuildCache rooted at dir (".crest/cache" standalone).

func (*DiskCache) Do

func (c *DiskCache) Do(ctx context.Context, key Key, fn func(context.Context) ([]byte, error)) ([]byte, error)

Do returns the output cached on disk for key, or runs fn and caches its output. Concurrent calls for the same key share one run.

type ElementPlugin

type ElementPlugin interface {
	// Spec describes the element. The compiler checks attributes against it
	// before Build runs.
	Spec() ElementSpec
	// Build expands one use of the element. el is a copy whose attributes
	// passed the Spec check and include their defaults. An error is
	// reported as W0340, at the position of an *ast.Error or else at the
	// element. The same input must give the same Result.
	Build(ctx context.Context, bc *BuildContext, el *ast.Element) (Result, error)
}

ElementPlugin compiles one element tag (or tag prefix).

type ElementSpec

type ElementSpec struct {
	Tag         string     `json:"tag"`
	Prefix      bool       `json:"prefix,omitempty"`
	Description string     `json:"description,omitempty"`
	RawBody     bool       `json:"raw_body,omitempty"`
	Mode        Mode       `json:"mode"`
	Version     string     `json:"version"`
	Attrs       []AttrSpec `json:"attrs,omitempty"`
}

ElementSpec describes an element to the compiler, the LSP, and AX.

func (ElementSpec) Matches

func (s ElementSpec) Matches(tag string) bool

Matches reports whether the spec handles tag.

type Fix

type Fix struct {
	Kind    string `json:"kind"`
	File    string `json:"file,omitempty"`
	Line    int    `json:"line,omitempty"`
	Col     int    `json:"col,omitempty"`
	Insert  string `json:"insert,omitempty"`
	Replace string `json:"replace,omitempty"`
	With    string `json:"with,omitempty"`
	Command string `json:"command,omitempty"`
}

Fix is a machine-applicable remedy. Kind is "edit", "command", or "none". An edit either inserts Insert at Line:Col, or replaces the text Replace that starts there with With.

type Key

type Key string

Key is a BuildCache key: a hex SHA-256.

func KeyOf

func KeyOf(spec ElementSpec, el *ast.Element, extra ...[]byte) Key

KeyOf hashes the plugin tag and Version, the element's static attributes in name order, its raw body, and extra option bytes.

type Mode

type Mode uint8

Mode says whether an element accepts dynamic attribute values.

const (
	// StaticOnly elements reject dynamic attributes with W0332.
	StaticOnly Mode = iota
	// StaticOrRuntime elements may answer dynamic attributes with a RuntimeCall.
	StaticOrRuntime
)

func (Mode) MarshalText

func (m Mode) MarshalText() ([]byte, error)

MarshalText encodes m as static_only or static_or_runtime.

type Result

type Result struct {
	// HTML is static markup with no asset references.
	HTML string
	// Crest nodes are compiled as if written in the template; they may
	// contain other plugin elements.
	Crest []ast.Node
	// Call delegates to a runtime helper for dynamic values.
	Call   *RuntimeCall
	Assets []AssetRef
}

Result is a plugin's output. Exactly one of HTML, Crest, or Call is set.

type RuntimeCall

type RuntimeCall struct {
	Imports []string
	Expr    goast.Expr
}

RuntimeCall is a Go expression, evaluated at render time, that must yield a crest.Component. The generated file imports Imports.

type Severity

type Severity string

Severity of a diagnostic.

const (
	SeverityError   Severity = "error"
	SeverityWarning Severity = "warning"
	SeverityInfo    Severity = "info"
)

The severities, most severe first.

Jump to

Keyboard shortcuts

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