providers/import: Add ability for templates to read step.meta

This commit is contained in:
Sasha Koshka 2024-12-11 20:40:23 -05:00
parent 06c788d997
commit 461abe6cf4

View File

@ -2,15 +2,17 @@ package impor
import "fmt"
import "errors"
import "slices"
import "strings"
import "html/template"
import "git.tebibyte.media/sashakoshka/step"
var _ step.FuncProviderFor = new(Provider)
var _ step.Configurable = new(Provider)
// Provider provides import functions.
type Provider struct {
config step.Meta
}
// Package fulfills the step.Provider interface.
@ -18,9 +20,14 @@ func (this *Provider) Package () string {
return "import"
}
func (this *Provider) Configure (config step.Meta) error {
return nil
}
// FuncMapFor fulfills the step.FuncProviderFor interface.
func (this *Provider) FuncMapFor (document *step.Document) template.FuncMap {
stat := &state {
config: this.config,
document: document,
}
return template.FuncMap {
@ -31,10 +38,13 @@ func (this *Provider) FuncMapFor (document *step.Document) template.FuncMap {
"execute": stat.funcExecute,
"include": stat.funcInclude,
"includeHTML": stat.funcIncludeHTML,
"config": stat.funcConfig,
"configList": stat.funcConfigList,
}
}
type state struct {
config step.Meta
document *step.Document
}
@ -86,3 +96,14 @@ func (this *state) funcIncludeHTML (name string, data any) (template.HTML, error
if err != nil { return "", err }
return result.Body, nil
}
func (this *state) funcConfig (key string) string {
if this.config == nil { return "" }
return this.config.Get(key)
}
func (this *state) funcConfigList (key string) []string {
if this.config == nil { return nil }
// config is considered immutable, time to enforce it
return slices.Clone(this.config[key])
}