/
templates.go
81 lines (60 loc) · 2.01 KB
/
templates.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package templating
import (
"crypto/sha512"
"embed"
"encoding/json"
"fmt"
"io"
"text/template"
"github.com/Masterminds/sprig/v3"
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v3"
)
//go:embed templates
var templateFs embed.FS
var Templates map[string]*template.Template
type TemplateDefinition interface {
// ID returns a unique identifier for the template.
// The value should match the filename for the template, without the .svg extension.
ID() string
// DecodeConfig decodes the config for the template from the given yaml.Node.
// The yaml.Node will be a mapping node.
DecodeConfig(value *yaml.Node) (any, error)
// TemplateContext returns the data to be passed to the template for a given cover.
TemplateContext(cover Cover) any
}
var templateDefinitions = []TemplateDefinition{
&GradientTemplate{},
}
var TemplateDefinitionMap = map[string]TemplateDefinition{}
func init() {
Templates = make(map[string]*template.Template)
for _, t := range templateDefinitions {
TemplateDefinitionMap[t.ID()] = t
templateFileName := getTemplateFilename(t)
templateBytes, err := templateFs.ReadFile(templateFileName)
if err != nil {
log.Fatal().Err(err).Str("template", t.ID()).Msg("Error reading template file")
}
parsedTemplate, err := template.New(t.ID()).Funcs(sprig.FuncMap()).Parse(string(templateBytes))
if err != nil {
log.Fatal().Err(err).Str("template", t.ID()).Msg("Error parsing template")
}
Templates[t.ID()] = parsedTemplate
}
}
func getTemplateFilename(template TemplateDefinition) string {
return fmt.Sprintf("templates/%s.svg", template.ID())
}
func GetTemplateContextHash(templateContext any) string {
hash := sha512.New()
templateContextJson, _ := json.Marshal(templateContext)
hash.Write(templateContextJson)
return fmt.Sprintf("%x", hash.Sum(nil))
}
func GetTemplateHash(template TemplateDefinition) string {
hash := sha512.New()
templateFile, _ := templateFs.Open(getTemplateFilename(template))
_, _ = io.Copy(hash, templateFile)
return fmt.Sprintf("%x", hash.Sum(nil))
}