forked from gobuffalo/buffalo
/
template.go
306 lines (255 loc) · 7.05 KB
/
template.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package render
import (
"fmt"
"html/template"
"io"
"io/fs"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"github.com/sirupsen/logrus"
"golang.org/x/text/language"
)
type templateRenderer struct {
*Engine
contentType string
names []string
aliases sync.Map
}
func (s *templateRenderer) ContentType() string {
return s.contentType
}
func (s *templateRenderer) resolve(name string) ([]byte, error) {
if s.TemplatesFS == nil {
return nil, fmt.Errorf("no templates fs defined")
}
f, err := s.TemplatesFS.Open(name)
if err == nil {
return io.ReadAll(f)
}
v, ok := s.aliases.Load(name)
if !ok {
return nil, fmt.Errorf("could not find template %s", name)
}
f, err = s.TemplatesFS.Open(v.(string))
if err != nil {
return nil, err
}
return io.ReadAll(f)
}
func (s *templateRenderer) Render(w io.Writer, data Data) error {
if err := s.updateAliases(); err != nil {
return err
}
var body template.HTML
for _, name := range s.names {
var err error
body, err = s.exec(name, data)
if err != nil {
return fmt.Errorf("%s: %w", name, err)
}
data["yield"] = body
}
_, err := w.Write([]byte(body))
return err
}
func (s *templateRenderer) updateAliases() error {
if s.TemplatesFS == nil {
return nil
}
return fs.WalkDir(s.TemplatesFS, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
// index.plush.ko-kr.html as index.ko-kr.html
shortcut := strings.Replace(path, ".plush.", ".", 1)
s.aliases.Store(shortcut, path)
// register short version (lang only) of shortcut
words := strings.Split(filepath.Base(shortcut), ".")
if len(words) > 2 {
for _, w := range words[1 : len(words)-1] {
if tag, err := language.Parse(w); err == nil {
base, confidence := tag.Base()
if confidence == language.Exact || confidence == language.High {
// index.plush.ko-kr.html as index.ko.html
shortcut := strings.Replace(shortcut, w, base.String(), 1)
s.aliases.Store(shortcut, path)
// index.plush.ko-kr.html as index.plush.ko.html
shortcut = strings.Replace(path, w, base.String(), 1)
s.aliases.Store(shortcut, path)
}
}
}
}
return nil
})
}
func fixExtension(name string, ct string) string {
if filepath.Ext(name) == "" {
switch {
case strings.Contains(ct, "html"):
name += ".html"
case strings.Contains(ct, "javascript"):
name += ".js"
case strings.Contains(ct, "markdown"):
name += ".md"
}
}
return name
}
// partialFeeder returns template string for the name from `TemplateBox`.
// It should be registered as helper named `partialFeeder` so plush can
// find it with the name.
func (s *templateRenderer) partialFeeder(name string) (string, error) {
ct := strings.ToLower(s.contentType)
d, f := filepath.Split(name)
name = filepath.Join(d, "_"+f)
name = fixExtension(name, ct)
b, err := s.resolve(name)
return string(b), err
}
func (s *templateRenderer) exec(name string, data Data) (template.HTML, error) {
ct := strings.ToLower(s.contentType)
data["contentType"] = ct
name = fixExtension(name, ct)
source, err := s.localizedResolve(name, data)
if err != nil {
return "", err
}
helpers := map[string]interface{}{}
for k, v := range s.Helpers {
helpers[k] = v
}
// Allows to specify custom partialFeeder
if helpers["partialFeeder"] == nil {
helpers["partialFeeder"] = s.partialFeeder
}
helpers = s.addAssetsHelpers(helpers)
body := string(source)
for _, ext := range s.exts(name) {
te, ok := s.TemplateEngines[ext]
if !ok {
logrus.Errorf("could not find a template engine for %s", ext)
continue
}
body, err = te(body, data, helpers)
if err != nil {
return "", err
}
}
return template.HTML(body), nil
}
// next step, deprecate if this is no longer required.
func (s *templateRenderer) localizedName(name string, data Data) string {
templateName := name
languages, ok := data["languages"].([]string)
if !ok || len(languages) == 0 {
return templateName
}
ll := len(languages)
// Default language is the last in the list
defaultLanguage := languages[ll-1]
ext := filepath.Ext(name)
rawName := strings.TrimSuffix(name, ext)
for _, l := range languages {
var candidateName string
if l == defaultLanguage {
break
}
candidateName = rawName + "." + strings.ToLower(l) + ext
if _, err := s.resolve(candidateName); err == nil {
// Replace name with the existing suffixed version
templateName = candidateName
break
}
}
return templateName
}
func (s *templateRenderer) localizedResolve(name string, data Data) ([]byte, error) {
languages, ok := data["languages"].([]string)
if !ok || len(languages) == 0 {
return s.resolve(name)
}
defaultLang := languages[len(languages)-1] // default language
ext := filepath.Ext(name)
rawName := strings.TrimSuffix(name, ext)
for _, lang := range languages {
if lang == defaultLang {
break
}
fullLower := strings.ToLower(lang) // forms of ko-kr or ko
short := strings.Split(fullLower, "-")[0] // form of ko
fullLocale := rawName + "." + fullLower + ext
if source, err := s.resolve(fullLocale); err == nil {
return source, nil
}
if fullLower != short {
langOnly := rawName + "." + short + ext
if source, err := s.resolve(langOnly); err == nil {
return source, nil
}
}
}
return s.resolve(name)
}
func (s *templateRenderer) exts(name string) []string {
exts := []string{}
for {
ext := filepath.Ext(name)
if ext == "" {
break
}
name = strings.TrimSuffix(name, ext)
exts = append(exts, strings.ToLower(ext[1:]))
}
if len(exts) == 0 {
return []string{"html"}
}
sort.Sort(sort.Reverse(sort.StringSlice(exts)))
return exts
}
func (s *templateRenderer) assetPath(file string) (string, error) {
if len(assetMap.Keys()) == 0 || os.Getenv("GO_ENV") != "production" {
manifest, err := s.AssetsFS.Open("manifest.json")
if err != nil {
manifest, err = s.AssetsFS.Open("assets/manifest.json")
if err != nil {
return assetPathFor(file), nil
}
}
defer manifest.Close()
err = loadManifest(manifest)
if err != nil {
return assetPathFor(file), fmt.Errorf("your manifest.json is not correct: %s", err)
}
}
return assetPathFor(file), nil
}
// Template renders the named files using the specified
// content type and the github.com/gobuffalo/plush
// package for templating. If more than 1 file is provided
// the second file will be considered a "layout" file
// and the first file will be the "content" file which will
// be placed into the "layout" using "{{yield}}".
func Template(c string, names ...string) Renderer {
e := New(Options{})
return e.Template(c, names...)
}
// Template renders the named files using the specified
// content type and the github.com/gobuffalo/plush
// package for templating. If more than 1 file is provided
// the second file will be considered a "layout" file
// and the first file will be the "content" file which will
// be placed into the "layout" using "{{yield}}".
func (e *Engine) Template(c string, names ...string) Renderer {
return &templateRenderer{
Engine: e,
contentType: c,
names: names,
}
}