templatefmt

package
v0.5.3 Latest Latest
Warning

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

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

Documentation

Overview

Package templatefmt formats tinybind template sources.

It is the library entry point for requirement:template-source-formatting: the fmt subcommand is a thin wrapper over it, so an editor plugin, a build step, or a framework's own tooling needs no CLI process. Formatting is a pure function of a source and a set of options; nothing here reads or writes the filesystem except Dir, which exists because "format this package" is the request everything else is built from.

Index

Constants

View Source
const (
	DefaultHTMLPattern      = "*.tb.html"
	DefaultSQLPattern       = "*.tb.sql"
	DefaultDynamoPattern    = dynamobind.DefaultTemplatePattern
	DefaultFirestorePattern = firestorebind.DefaultTemplatePattern
)

Default file patterns, matching the generator's own discovery defaults.

View Source
const DefaultWidth = syntax.DefaultWidth

DefaultWidth is the soft line width used when Options.Width is zero.

Variables

View Source
var ErrUnknownFormat = errors.New("templatefmt: file name matches no template pattern")

ErrUnknownFormat reports a file name that matches no template pattern.

Functions

func Source

func Source(filename string, source []byte, options Options) ([]byte, error)

Source formats one source, choosing the language from the file name.

func SourceAs

func SourceAs(format Format, filename string, source []byte, options Options) ([]byte, error)

SourceAs formats one source in a named language. It is what a caller with no file name uses, such as an editor filtering a buffer through standard input.

Types

type Format

type Format string

Format identifies one template language.

const (
	// HTML is the .tb.html component language.
	HTML Format = "html"
	// SQL is the .tb.sql statement language.
	SQL Format = "sql"
	// Dynamo is the .tb.dynamo access-pattern language.
	Dynamo Format = "dynamo"
	// Firestore is the .tb.firestore access-pattern language.
	Firestore Format = "firestore"
)

func Identify

func Identify(name string, options Options) (Format, error)

Identify reports which language a file name belongs to. A name matching two patterns is ambiguous and reported rather than guessed, on the same terms generation refuses it.

type Options

type Options struct {
	// Width is the soft line width; zero uses DefaultWidth. It is soft because a
	// construct that cannot break without changing meaning stays long.
	Width int
	// Indent is one indentation level; empty uses two spaces.
	Indent string
	// PreserveWhitespace mirrors the generator option of the same name. With it
	// set, generation no longer collapses static whitespace, so HTML layout is
	// restricted to the positions the HTML parser discards runs from.
	PreserveWhitespace bool
	// HTMLPattern, SQLPattern, DynamoPattern and FirestorePattern are base-name
	// globs used by Dir and by Source when it identifies a file. Empty values
	// use the defaults.
	HTMLPattern      string
	SQLPattern       string
	DynamoPattern    string
	FirestorePattern string
}

Options configures formatting. The zero value is valid and uses every default, so templatefmt.Source(name, src, templatefmt.Options{}) is the whole API for the common case.

type Result

type Result struct {
	Path   string
	Format Format
	// Source is the file as it was read.
	Source []byte
	// Formatted is the canonical form. It is nil when Err is set, because a
	// source that does not parse is left exactly as it is.
	Formatted []byte
	// Changed reports that Formatted differs from Source.
	Changed bool
	// Err is the parse or print failure for this file. Other files in the same
	// run are unaffected.
	Err error
}

Result is the outcome for one file.

func Dir

func Dir(dir string, options Options) ([]Result, error)

Dir formats every template directly inside one directory. Discovery does not descend, matching requirement:configurable-template-file-patterns: a template belongs to the Go package it sits in.

Nothing is written; the caller decides what to do with each Result.

func (Result) Write

func (r Result) Write() error

Write applies a result to disk. It writes only when the file would change, so an already formatted file keeps its timestamp and rule:generation-input-hash sees no reason to regenerate.

Jump to

Keyboard shortcuts

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