~adnano/kiln

ref: 7ac653c62085191327b594a7ae1617cd561c2d32 kiln/templates.go -rw-r--r-- 4.1 KiB
7ac653c6Adnan Maolood docs: Tweak wording 7 months ago
                                                                                
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main

import (
	"fmt"
	htemplate "html/template"
	"io"
	"io/fs"
	"io/ioutil"
	"os"
	pathpkg "path"
	"path/filepath"
	"strings"
	"text/template"
	"text/template/parse"
)

// Template represents a template.
type Template interface {
	AddParseTree(*parse.Tree) error
	Execute(io.Writer, interface{}) error
	Tree() *parse.Tree
}

type textTemplate struct {
	tmpl *template.Template
}

func (t textTemplate) AddParseTree(tree *parse.Tree) error {
	_, err := t.tmpl.AddParseTree(t.tmpl.Name(), tree)
	return err
}

func (t textTemplate) Execute(w io.Writer, data interface{}) error {
	return t.tmpl.Execute(w, data)
}

func (t textTemplate) Tree() *parse.Tree {
	return t.tmpl.Tree
}

type htmlTemplate struct {
	tmpl *htemplate.Template
}

func (t htmlTemplate) AddParseTree(tree *parse.Tree) error {
	_, err := t.tmpl.AddParseTree(t.tmpl.Name(), tree)
	return err
}

func (t htmlTemplate) Execute(w io.Writer, data interface{}) error {
	return t.tmpl.Execute(w, data)
}

func (t htmlTemplate) Tree() *parse.Tree {
	return t.tmpl.Tree
}

// Templates contains site templates.
type Templates struct {
	tmpls map[string]Template
	funcs map[string]interface{}
}

// Funcs sets the functions available to newly created templates.
func (t *Templates) Funcs(funcs map[string]interface{}) {
	t.funcs = funcs
}

// LoadTemplate loads a template from the provided filenames.
func (t *Templates) LoadTemplate(name string, filenames ...string) error {
	if t.tmpls == nil {
		t.tmpls = map[string]Template{}
	}
	if ext := pathpkg.Ext(name); ext == ".html" || ext == ".xml" {
		return t.loadHTMLTemplate(name, filenames...)
	}
	return t.loadTextTemplate(name, filenames...)
}

func (t *Templates) loadTextTemplate(name string, filenames ...string) error {
	tmpl := template.New(name).Funcs(t.funcs)
	for i := range filenames {
		b, err := ioutil.ReadFile(filenames[i])
		if err != nil {
			return err
		}
		if _, err := tmpl.Parse(string(b)); err != nil {
			return err
		}
	}
	t.tmpls[name] = textTemplate{tmpl}
	return nil
}

func (t *Templates) loadHTMLTemplate(name string, filenames ...string) error {
	tmpl := htemplate.New(name).Funcs(t.funcs)
	for i := range filenames {
		b, err := ioutil.ReadFile(filenames[i])
		if err != nil {
			return err
		}
		if _, err := tmpl.Parse(string(b)); err != nil {
			return err
		}
	}
	t.tmpls[name] = htmlTemplate{tmpl}
	return nil
}

// Load loads templates from the provided directory.
func (t *Templates) Load(dir string, exts []string) error {
	err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
		if err != nil {
			return err
		}
		if d.Type().IsRegular() {
			name := strings.TrimPrefix(path, dir)
			if err := t.LoadTemplate(name, path); err != nil {
				return err
			}
		}
		return nil
	})
	if err != nil && !os.IsNotExist(err) {
		return err
	}

	// Add base templates
	var extsMap = map[string]struct{}{}
	for _, ext := range exts {
		extsMap[ext] = struct{}{}
	}
	for path := range t.tmpls {
		ext := pathpkg.Ext(path)
		if _, ok := extsMap[ext]; !ok {
			continue
		}
		base := pathpkg.Join(pathpkg.Dir(path), "base"+ext)
		if tmpl, ok := t.tmpls[base]; ok {
			err := t.tmpls[path].AddParseTree(tmpl.Tree())
			if err != nil {
				return err
			}
		}
	}
	return nil
}

// FindTemplate returns the template for the given path.
func (t *Templates) FindTemplate(path string, tmpl string) (Template, bool) {
	tmplPath := pathpkg.Join(path, tmpl)
	if t, ok := t.tmpls[tmplPath]; ok {
		return t, true
	}
	if t, ok := t.tmpls[pathpkg.Join("/_default", tmpl)]; ok {
		return t, true
	}
	// Failed to find template
	return nil, false
}

// FindPartial returns the partial template of the given name.
func (t *Templates) FindPartial(name string) (Template, bool) {
	if t, ok := t.tmpls[pathpkg.Join("/_partials", name)]; ok {
		return t, true
	}
	return nil, false
}

// ExecutePartial executes the partial with the given name.
func (t *Templates) ExecutePartial(name string, data interface{}) (string, error) {
	tmpl, ok := t.FindPartial(name)
	if !ok {
		return "", fmt.Errorf("Error: partial %q not found", name)
	}
	var b strings.Builder
	if err := tmpl.Execute(&b, data); err != nil {
		return "", err
	}
	return b.String(), nil
}