templates

package
v0.0.0-...-ac0725a Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2023 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SymbolsKey      xo.ContextKey = "symbols"
	GenTypeKey      xo.ContextKey = "gen-type"
	TemplateTypeKey xo.ContextKey = "template-type"
	SuffixKey       xo.ContextKey = "suffix"
	SrcKey          xo.ContextKey = "src"
	OutKey          xo.ContextKey = "out"
)

Context keys.

Variables

This section is empty.

Functions

func BaseFuncs

func BaseFuncs() template.FuncMap

BaseFuncs returns base template funcs.

func Errors

func Errors(ctx context.Context) ([]error, error)

Errors returns errors collected during file generation.

func Flags

func Flags(name string) []xo.FlagSet

Flags returns flag options and context for the template sets for the specified command name.

These should be added to the invocation context for any call to a template set func.

func For

func For(typ, name string) bool

For returns true if the template is available for the command name.

func GenType

func GenType(ctx context.Context) string

GenType returns the the gen-type option from the context.

func Out

func Out(ctx context.Context) string

Out returns out option from the context.

func Process

func Process(ctx context.Context, doAppend bool, single string, v *xo.XO) error

Process processes emitted templates for a template set.

func Register

func Register(typ string, set *TemplateSet)

Register registers a template set.

func Src

func Src(ctx context.Context) fs.FS

Src returns src option from the context.

func Suffix

func Suffix(ctx context.Context) string

Suffix returns suffix option from the context.

func Symbols

func Symbols(ctx context.Context) map[string]map[string]reflect.Value

Symbols returns the symbols option from the context.

func TemplateType

func TemplateType(ctx context.Context) string

TemplateType returns type option from the context.

func Types

func Types() []string

Types returns the registered type names of template sets.

func Write

func Write(ctx context.Context) error

Write performs post processing of emitted templates to a template set, writing to disk the processed content.

func WriteFiles

func WriteFiles(ctx context.Context) error

WriteFiles writes the generated files to disk.

func WriteRaw

func WriteRaw(ctx context.Context) error

WriteRaw writes the raw templates for a template set.

Types

type EmittedTemplate

type EmittedTemplate struct {
	Template *Template
	Buf      []byte
	File     string
	Err      []error
}

EmittedTemplate wraps a template with its content and file name.

type ErrPostFailed

type ErrPostFailed struct {
	File string
	Err  error
}

ErrPostFailed is the post failed error.

func (*ErrPostFailed) Error

func (err *ErrPostFailed) Error() string

Error satisfies the error interface.

func (*ErrPostFailed) Unwrap

func (err *ErrPostFailed) Unwrap() error

Unwrap satisfies the unwrap interface.

type Template

type Template struct {
	Set      string
	Template string
	Type     string
	Name     string
	Data     interface{}
	Extra    map[string]interface{}
}

Template wraps other templates.

func (*Template) File

func (tpl *Template) File() string

File returns the file name for the template.

type TemplateSet

type TemplateSet struct {
	// Files are the embedded templates.
	Files embed.FS
	// For are the command names the template set is available for.
	For []string
	// FileExt is the file extension added to out files.
	FileExt string
	// AddType will be called when a new type is encountered.
	AddType func(string)
	// Flags are additional template flags.
	Flags []xo.Flag
	// Order in which to process templates.
	Order []string
	// HeaderTemplate is the name of the header template.
	HeaderTemplate func(context.Context) *Template
	// PackageTemplates returns package templates.
	PackageTemplates func(context.Context) []*Template
	// Funcs provides template funcs for use by templates.
	Funcs func(context.Context) template.FuncMap
	// FileName determines the out file name templates.
	FileName func(context.Context, *Template) string
	// BuildContext provides a way for template sets to inject additional,
	// global context values, prior to template processing.
	BuildContext func(context.Context) context.Context
	// Process performs the preprocessing and the order to load files.
	Process func(context.Context, bool, *TemplateSet, *xo.XO) error
	// Post performs post processing of generated content.
	Post func(context.Context, []byte) ([]byte, error)
	// contains filtered or unexported fields
}

TemplateSet is a template set.

func (*TemplateSet) AddCustomFuncs

func (set *TemplateSet) AddCustomFuncs(ctx context.Context) error

AddCustomFuncs adds funcs from the template's funcs.go.tpl to the set of template funcs.

Uses the github.com/traefik/yaegi interpretter to evaluate the funcs.go.tpl, adding anything returned by the file's defined `func Init(context.Context) (template.FuncMap, error)` to the built template funcs.

See existing templates for implementation examples.

func (*TemplateSet) BuildFuncs

func (set *TemplateSet) BuildFuncs(ctx context.Context) error

BuildFuncs builds the template funcs.

func (*TemplateSet) Emit

func (set *TemplateSet) Emit(ctx context.Context, tpl *Template) error

Emit emits a template to the template set.

func (*TemplateSet) Exec

func (set *TemplateSet) Exec(ctx context.Context, tpl *Template) ([]byte, error)

Exec loads and executes a template.

func (*TemplateSet) Load

func (set *TemplateSet) Load(ctx context.Context, tpl *Template) (*template.Template, error)

Load loads a template.

func (*TemplateSet) LoadFile

func (set *TemplateSet) LoadFile(ctx context.Context, file string, doAppend bool) ([]byte, error)

LoadFile loads a file.

Directories

Path Synopsis
Package gotpl provides a Go template for xo.
Package gotpl provides a Go template for xo.

Jump to

Keyboard shortcuts

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