diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-02-09 18:29:49 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-02-14 09:39:46 +0100 |
commit | b0a9cf0a759e63309ac7e37bb90de161b3ebc051 (patch) | |
tree | ad82cf595ea8d4e4ff9ac8cba33458bfcf1ab3ae /tpl/tplimpl/template.go | |
parent | 9433cc25622035cde114931f5610f5ff2aecc207 (diff) |
tpl: Use go:embed to load internal templates
Fixes #8297
Diffstat (limited to 'tpl/tplimpl/template.go')
-rw-r--r-- | tpl/tplimpl/template.go | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/tpl/tplimpl/template.go b/tpl/tplimpl/template.go index 3ef815e24..66ef7fc21 100644 --- a/tpl/tplimpl/template.go +++ b/tpl/tplimpl/template.go @@ -14,7 +14,10 @@ package tplimpl import ( + "bytes" + "embed" "io" + "io/fs" "os" "path/filepath" "reflect" @@ -40,8 +43,6 @@ import ( "github.com/gohugoio/hugo/hugofs/files" "github.com/pkg/errors" - "github.com/gohugoio/hugo/tpl/tplimpl/embedded" - htmltemplate "github.com/gohugoio/hugo/tpl/internal/go_templates/htmltemplate" texttemplate "github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate" @@ -664,12 +665,29 @@ func (t *templateHandler) extractIdentifiers(line string) []string { return identifiers } +//go:embed embedded/templates/* +var embededTemplatesFs embed.FS + func (t *templateHandler) loadEmbedded() error { - for _, kv := range embedded.EmbeddedTemplates { - name, templ := kv[0], kv[1] + return fs.WalkDir(embededTemplatesFs, ".", func(path string, d fs.DirEntry, err error) error { + if d == nil || d.IsDir() { + return nil + } + + templb, err := embededTemplatesFs.ReadFile(path) + if err != nil { + return err + } + + // Get the newlines on Windows in line with how we had it back when we used Go Generate + // to write the templates to Go files. + templ := string(bytes.ReplaceAll(templb, []byte("\r\n"), []byte("\n"))) + name := strings.TrimPrefix(filepath.ToSlash(path), "embedded/templates/") + if err := t.AddTemplate(internalPathPrefix+name, templ); err != nil { return err } + if aliases, found := embeddedTemplatesAliases[name]; found { // TODO(bep) avoid reparsing these aliases for _, alias := range aliases { @@ -679,8 +697,9 @@ func (t *templateHandler) loadEmbedded() error { } } } - } - return nil + + return nil + }) } func (t *templateHandler) loadTemplates() error { |