aboutsummaryrefslogtreecommitdiffstats
path: root/lib/templates/template.go
blob: 4d96472dd1a31f2b40bc4ebf8ac64fcc03867c7b (plain) (blame)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package templates

import (
	"bytes"
	"fmt"
	"io"
	"os"
	"reflect"
	"text/template"

	"git.sr.ht/~rjarry/aerc/lib/xdg"
	"git.sr.ht/~rjarry/aerc/models"
)

func findTemplate(templateName string, templateDirs []string) (string, error) {
	for _, dir := range templateDirs {
		templateFile := xdg.ExpandHome(dir, templateName)
		if _, err := os.Stat(templateFile); os.IsNotExist(err) {
			continue
		}
		return templateFile, nil
	}

	return "", fmt.Errorf(
		"Can't find template %q in any of %v ", templateName, templateDirs)
}

func ParseTemplateFromFile(
	name string, dirs []string, data models.TemplateData,
) (io.Reader, error) {
	templateFile, err := findTemplate(name, dirs)
	if err != nil {
		return nil, err
	}
	emailTemplate, err := template.New(name).
		Funcs(templateFuncs).ParseFiles(templateFile)
	if err != nil {
		return nil, err
	}

	var body bytes.Buffer
	if err := Render(emailTemplate, &body, data); err != nil {
		return nil, err
	}
	return &body, nil
}

func ParseTemplate(name, content string) (*template.Template, error) {
	return template.New(name).Funcs(templateFuncs).Parse(content)
}

func Render(t *template.Template, w io.Writer, data models.TemplateData) error {
	return t.Execute(w, data)
}

// builtins is a slice of keywords and functions built into the Go standard
// library for templates. Since they are not exported, they are hardcoded here.
var builtins = []string{
	// from the Go standard library: src/text/template/parse/lex.go
	"block",
	"break",
	"continue",
	"define",
	"else",
	"end",
	"if",
	"range",
	"nil",
	"template",
	"with",

	// from the Go standard library: src/text/template/funcs.go
	"and",
	"call",
	"html",
	"index",
	"slice",
	"js",
	"len",
	"not",
	"or",
	"print",
	"printf",
	"println",
	"urlquery",
	"eq",
	"ge",
	"gt",
	"le",
	"lt",
	"ne",
}

func Terms() []string {
	var s []string
	t := reflect.TypeOf((*models.TemplateData)(nil)).Elem()
	for i := 0; i < t.NumMethod(); i++ {
		s = append(s, "."+t.Method(i).Name)
	}
	for fnStr := range templateFuncs {
		s = append(s, fnStr)
	}
	s = append(s, builtins...)
	return s
}