/
spitz.go
277 lines (241 loc) · 5.82 KB
/
spitz.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright (C) 2012 Numerotron Inc.
// Use of this source code is governed by an MIT-style license
// that can be found in the LICENSE file.
package spitz // import "stathat.com/c/spitz"
import (
"bytes"
"errors"
"fmt"
"html/template"
"io"
"log"
"os"
"path"
"time"
)
var Verbose bool
type tfile struct {
filename string
modTime time.Time
}
type tfiles []*tfile
func singleFile(filename string) tfiles {
return tfiles{&tfile{filename: filename}}
}
func multiFile(filenames []string) tfiles {
tf := make(tfiles, len(filenames))
for i, v := range filenames {
tf[i] = &tfile{filename: v}
}
return tf
}
func (tf tfiles) name() string {
if len(tf) == 0 {
return ""
}
return path.Base(tf[0].filename)
}
func (tf tfiles) filenames() []string {
names := make([]string, len(tf))
for i, v := range tf {
names[i] = v.filename
}
return names
}
func (tf tfiles) setModTimes() error {
for _, v := range tf {
fi, err := os.Stat(v.filename)
if err != nil {
return err
}
v.modTime = fi.ModTime()
}
return nil
}
func (tf tfiles) modifiedSince(t time.Time) bool {
for _, v := range tf {
fi, err := os.Stat(v.filename)
if err == nil && fi.ModTime().After(t) {
return true
}
}
return false
}
type tmpl struct {
name string
files tfiles
plate *template.Template
parsedAt time.Time
leftDelim string
rightDelim string
}
type layout struct {
header string
footer string
}
type Pool struct {
contentDir string
reload bool
tmpls map[string]*tmpl
layouts map[string]*layout
}
func New(contentDir string, reload bool) *Pool {
pool := &Pool{contentDir: contentDir, reload: reload}
pool.tmpls = make(map[string]*tmpl)
pool.layouts = make(map[string]*layout)
return pool
}
// Register adds the template at ContentDirectory + name + .tmpl.html to the set
// of registered templates.
func (p *Pool) Register(name, leftDelim, rightDelim string) error {
ename := fmt.Sprintf("%s.tmpl.html", name)
filename := path.Join(p.contentDir, ename)
nt := &tmpl{name: name, files: singleFile(filename), leftDelim: leftDelim, rightDelim: rightDelim}
err := nt.parse()
if err != nil {
return err
}
p.tmpls[name] = nt
return nil
}
func (p *Pool) RegisterLayout(name, header, footer string, leftDelim, rightDelim string) error {
err := p.Register(header, leftDelim, rightDelim)
if err != nil {
return err
}
err = p.Register(footer, leftDelim, rightDelim)
if err != nil {
return err
}
p.layouts[name] = &layout{header, footer}
return nil
}
func (p *Pool) RegisterString(name, content string) error {
t, err := template.New(name).Funcs(funcMap).Parse(content)
if err != nil {
return err
}
p.tmpls[name] = &tmpl{plate: t}
return nil
}
func (p *Pool) RegisterMulti(name, leftDelim, rightDelim string, filenames ...string) error {
paths := make([]string, len(filenames))
for i, v := range filenames {
paths[i] = path.Join(p.contentDir, fmt.Sprintf("%s.tmpl.html", v))
}
files := multiFile(paths)
nt := &tmpl{name: name, files: files, leftDelim: leftDelim, rightDelim: rightDelim}
err := nt.parse()
if err != nil {
return err
}
p.tmpls[name] = nt
return nil
}
func (p *Pool) Render(layout, name string, data interface{}, wr io.Writer) error {
lout, ok := p.layouts[layout]
if !ok {
return errors.New(fmt.Sprintf("layout %q not found", layout))
}
err := p.templateRun(lout.header, data, wr)
if err != nil {
return err
}
err = p.templateRun(name, data, wr)
if err != nil {
return err
}
err = p.templateRun(lout.footer, data, wr)
return err
}
func (p *Pool) RenderMulti(layout, name, innerName string, data interface{}, wr io.Writer) error {
lout, ok := p.layouts[layout]
if !ok {
return errors.New(fmt.Sprintf("layout %q not found", layout))
}
err := p.templateRun(lout.header, data, wr)
if err != nil {
return err
}
err = p.templateRunInner(name, innerName, data, wr)
if err != nil {
return err
}
err = p.templateRun(lout.footer, data, wr)
return err
}
func (p *Pool) RenderNoLayout(name string, data interface{}, wr io.Writer) error {
return p.templateRun(name, data, wr)
}
func (p *Pool) RenderToString(layout, template string, data interface{}) (string, error) {
var b bytes.Buffer
err := p.Render(layout, template, data, &b)
if err != nil {
return "", err
}
return string(b.Bytes()), nil
}
func (p *Pool) RenderNoLayoutToString(template string, data interface{}) (string, error) {
var b bytes.Buffer
err := p.RenderNoLayout(template, data, &b)
if err != nil {
return "", err
}
return string(b.Bytes()), nil
}
func (p *Pool) Len() int {
return len(p.tmpls)
}
func (p *Pool) templateRun(name string, data interface{}, wr io.Writer) error {
t, ok := p.tmpls[name]
if !ok {
return errors.New(fmt.Sprintf("template %q not found", name))
}
if p.reload {
t.checkReload()
}
return t.plate.Execute(wr, data)
}
func (p *Pool) templateRunInner(name, innerName string, data interface{}, wr io.Writer) error {
t, ok := p.tmpls[name]
if !ok {
return errors.New(fmt.Sprintf("template %q not found", name))
}
if p.reload {
t.checkReload()
}
return t.plate.ExecuteTemplate(wr, innerName, data)
}
func (t *tmpl) parse() error {
if len(t.files) == 0 {
return errors.New(fmt.Sprintf("no files for template %q", t.name))
}
tm, err := template.New(t.files.name()).Delims(t.leftDelim, t.rightDelim).Funcs(funcMap).ParseFiles(t.files.filenames()...)
if err != nil {
return err
}
t.plate = tm
t.parsedAt = time.Now()
return t.files.setModTimes()
}
func (t *tmpl) checkReload() {
if len(t.files) == 0 {
if Verbose {
log.Printf("no files")
}
return
}
if t.files.modifiedSince(t.parsedAt) {
if Verbose {
log.Printf("template %s changed. reparsing...", t.name)
}
t.parse()
}
}
func (p *Pool) reloadTemplate(name string) error {
t, ok := p.tmpls[name]
if !ok {
return errors.New(fmt.Sprintf("template %q not registered", name))
}
return t.parse()
}