providers/import: Add ability for templates to read step.meta
This commit is contained in:
parent
06c788d997
commit
461abe6cf4
@ -2,15 +2,17 @@ package impor
|
|||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
import "errors"
|
import "errors"
|
||||||
|
import "slices"
|
||||||
import "strings"
|
import "strings"
|
||||||
import "html/template"
|
import "html/template"
|
||||||
import "git.tebibyte.media/sashakoshka/step"
|
import "git.tebibyte.media/sashakoshka/step"
|
||||||
|
|
||||||
var _ step.FuncProviderFor = new(Provider)
|
var _ step.FuncProviderFor = new(Provider)
|
||||||
|
var _ step.Configurable = new(Provider)
|
||||||
|
|
||||||
// Provider provides import functions.
|
// Provider provides import functions.
|
||||||
type Provider struct {
|
type Provider struct {
|
||||||
|
config step.Meta
|
||||||
}
|
}
|
||||||
|
|
||||||
// Package fulfills the step.Provider interface.
|
// Package fulfills the step.Provider interface.
|
||||||
@ -18,9 +20,14 @@ func (this *Provider) Package () string {
|
|||||||
return "import"
|
return "import"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *Provider) Configure (config step.Meta) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// FuncMapFor fulfills the step.FuncProviderFor interface.
|
// FuncMapFor fulfills the step.FuncProviderFor interface.
|
||||||
func (this *Provider) FuncMapFor (document *step.Document) template.FuncMap {
|
func (this *Provider) FuncMapFor (document *step.Document) template.FuncMap {
|
||||||
stat := &state {
|
stat := &state {
|
||||||
|
config: this.config,
|
||||||
document: document,
|
document: document,
|
||||||
}
|
}
|
||||||
return template.FuncMap {
|
return template.FuncMap {
|
||||||
@ -31,10 +38,13 @@ func (this *Provider) FuncMapFor (document *step.Document) template.FuncMap {
|
|||||||
"execute": stat.funcExecute,
|
"execute": stat.funcExecute,
|
||||||
"include": stat.funcInclude,
|
"include": stat.funcInclude,
|
||||||
"includeHTML": stat.funcIncludeHTML,
|
"includeHTML": stat.funcIncludeHTML,
|
||||||
|
"config": stat.funcConfig,
|
||||||
|
"configList": stat.funcConfigList,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type state struct {
|
type state struct {
|
||||||
|
config step.Meta
|
||||||
document *step.Document
|
document *step.Document
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,3 +96,14 @@ func (this *state) funcIncludeHTML (name string, data any) (template.HTML, error
|
|||||||
if err != nil { return "", err }
|
if err != nil { return "", err }
|
||||||
return result.Body, nil
|
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])
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user