forked from revel/revel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
364 lines (323 loc) · 11.6 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved.
// Revel Framework source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package revel
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
)
// ErrorCSSClass httml CSS error class name
var ErrorCSSClass = "hasError"
// TemplateLoader object handles loading and parsing of templates.
// Everything below the application's views directory is treated as a template.
type TemplateLoader struct {
// Template data and implementation
templatesAndEngineList []TemplateEngine
// If an error was encountered parsing the templates, it is stored here.
compileError *Error
// Paths to search for templates, in priority order.
paths []string
// Map from template name to the path from whence it was loaded.
TemplatePaths map[string]string
// A map of looked up template results
TemplateMap map[string]Template
}
type Template interface {
// The name of the template.
Name() string // Name of template
// The content of the template as a string (Used in error handling).
Content() []string // Content
// Called by the server to render the template out the io.Writer, context contains the view args to be passed to the template.
Render(wr io.Writer, context interface{}) error
// The full path to the file on the disk.
Location() string // Disk location
}
var invalidSlugPattern = regexp.MustCompile(`[^a-z0-9 _-]`)
var whiteSpacePattern = regexp.MustCompile(`\s+`)
func NewTemplateLoader(paths []string) *TemplateLoader {
loader := &TemplateLoader{
paths: paths,
}
return loader
}
// Refresh method scans the views directory and parses all templates as Go Templates.
// If a template fails to parse, the error is set on the loader.
// (It's awkward to refresh a single Go Template)
// Refresh method scans the views directory and parses all templates as Go Templates.
// If a template fails to parse, the error is set on the loader.
// (It's awkward to refresh a single Go Template)
func (loader *TemplateLoader) Refresh() (err *Error) {
TRACE.Printf("Refreshing templates from %s", loader.paths)
if len(loader.templatesAndEngineList) == 0 {
if err = loader.InitializeEngines(GO_TEMPLATE); err != nil {
return
}
}
for _, engine := range loader.templatesAndEngineList {
engine.Event(TEMPLATE_REFRESH_REQUESTED, nil)
}
fireEvent(TEMPLATE_REFRESH_REQUESTED, nil)
defer func() {
for _, engine := range loader.templatesAndEngineList {
engine.Event(TEMPLATE_REFRESH_COMPLETED, nil)
}
fireEvent(TEMPLATE_REFRESH_COMPLETED, nil)
// Reset the TemplateMap, we don't prepopulate the map because
loader.TemplateMap = map[string]Template{}
}()
// Resort the paths, make sure the revel path is the last path,
// so anything can override it
revelTemplatePath := filepath.Join(RevelPath, "templates")
// Go through the paths
for i, o := range loader.paths {
if o == revelTemplatePath && i != len(loader.paths)-1 {
loader.paths[i] = loader.paths[len(loader.paths)-1]
loader.paths[len(loader.paths)-1] = revelTemplatePath
}
}
TRACE.Printf("Refreshing templates from %s", loader.paths)
loader.compileError = nil
loader.TemplatePaths = map[string]string{}
for _, basePath := range loader.paths {
// Walk only returns an error if the template loader is completely unusable
// (namely, if one of the TemplateFuncs does not have an acceptable signature).
// Handling symlinked directories
var fullSrcDir string
f, err := os.Lstat(basePath)
if err == nil && f.Mode()&os.ModeSymlink == os.ModeSymlink {
fullSrcDir, err = filepath.EvalSymlinks(basePath)
if err != nil {
panic(err)
}
} else {
fullSrcDir = basePath
}
var templateWalker filepath.WalkFunc
templateWalker = func(path string, info os.FileInfo, err error) error {
if err != nil {
ERROR.Println("error walking templates:", err)
return nil
}
// Walk into watchable directories
if info.IsDir() {
if !loader.WatchDir(info) {
return filepath.SkipDir
}
return nil
}
// Only add watchable
if !loader.WatchFile(info.Name()) {
return nil
}
fileBytes, err := loader.findAndAddTemplate(path, fullSrcDir, basePath)
// Store / report the first error encountered.
if err != nil && loader.compileError == nil {
loader.compileError, _ = err.(*Error)
if nil == loader.compileError {
_, line, description := ParseTemplateError(err)
loader.compileError = &Error{
Title: "Template Compilation Error",
Path: path,
Description: description,
Line: line,
SourceLines: strings.Split(string(fileBytes), "\n"),
}
}
ERROR.Printf("Template compilation error (In %s around line %d):\n\t%s",
path, loader.compileError.Line, err.Error())
} else if nil != err { //&& strings.HasPrefix(templateName, "errors/") {
if compileError, ok := err.(*Error); ok {
ERROR.Printf("Template compilation error (In %s around line %d):\n\t%s",
path, compileError.Line, err.Error())
} else {
ERROR.Printf("Template compilation error (In %s ):\n\t%s",
path, err.Error())
}
}
return nil
}
if _, err = os.Lstat(fullSrcDir); os.IsNotExist(err) {
// #1058 Given views/template path is not exists
// so no need to walk, move on to next path
continue
}
funcErr := Walk(fullSrcDir, templateWalker)
// If there was an error with the Funcs, set it and return immediately.
if funcErr != nil {
loader.compileError = funcErr.(*Error)
return loader.compileError
}
}
// Note: compileError may or may not be set.
return loader.compileError
}
// Checks to see if template exists in templatePaths, if so it is skipped (templates are imported in order
// reads the template file into memory, replaces namespace keys with module (if found
func (loader *TemplateLoader) findAndAddTemplate(path, fullSrcDir, basePath string) (fileBytes []byte, err error) {
templateName := filepath.ToSlash(path[len(fullSrcDir)+1:])
// Convert template names to use forward slashes, even on Windows.
if os.PathSeparator == '\\' {
templateName = strings.Replace(templateName, `\`, `/`, -1) // `
}
// Check to see if template was found
if place, found := loader.TemplatePaths[templateName]; found {
TRACE.Println("Not Loading, template is already exists: ", templateName, "\r\n\told file:",
place, "\r\n\tnew file:", path)
return
}
fileBytes, err = ioutil.ReadFile(path)
if err != nil {
ERROR.Println("Failed reading file:", path)
return
}
// Parse template file and replace the "_RNS_|" in the template with the module name
// allow for namespaces to be renamed "_RNS_(.*?)|"
if module := ModuleFromPath(path, false);module != nil {
fileBytes = namespaceReplace(fileBytes, module)
}
// if we have an engine picked for this template process it now
baseTemplate := NewBaseTemplate(templateName, path, basePath, fileBytes)
// Try to find a default engine for the file
for _, engine := range loader.templatesAndEngineList {
if engine.Handles(baseTemplate) {
_, err = loader.loadIntoEngine(engine, baseTemplate)
return
}
}
// Try all engines available
var defaultError error
for _, engine := range loader.templatesAndEngineList {
if loaded, loaderr := loader.loadIntoEngine(engine, baseTemplate); loaded {
return
} else {
TRACE.Printf("Engine '%s' unable to compile %s %s", engine.Name(), path, loaderr)
if defaultError == nil {
defaultError = loaderr
}
}
}
// Assign the error from the first parser
err = defaultError
// No engines could be found return the err
if err != nil {
err = fmt.Errorf("Failed to parse template file using engines %s %s", path, err)
}
return
}
func (loader *TemplateLoader) loadIntoEngine(engine TemplateEngine, baseTemplate *TemplateView) (loaded bool, err error) {
if loadedTemplate := engine.Lookup(baseTemplate.TemplateName); loadedTemplate != nil {
// Duplicate template found for engine
TRACE.Println("template already exists: ", baseTemplate.TemplateName, " in engine ", engine.Name(), "\r\n\told file:",
loadedTemplate.Location(), "\r\n\tnew file:", baseTemplate.FilePath)
loaded = true
return
}
if err = engine.ParseAndAdd(baseTemplate); err == nil {
loader.TemplatePaths[baseTemplate.TemplateName] = baseTemplate.FilePath
TRACE.Printf("Engine '%s' compiled %s", engine.Name(), baseTemplate.FilePath)
loaded = true
} else {
TRACE.Printf("Engine '%s' failed to compile %s %s", engine.Name(), baseTemplate.FilePath, err)
}
return
}
// WatchDir returns true of directory doesn't start with . (dot)
// otherwise false
func (loader *TemplateLoader) WatchDir(info os.FileInfo) bool {
// Watch all directories, except the ones starting with a dot.
return !strings.HasPrefix(info.Name(), ".")
}
// WatchFile returns true of file doesn't start with . (dot)
// otherwise false
func (loader *TemplateLoader) WatchFile(basename string) bool {
// Watch all files, except the ones starting with a dot.
return !strings.HasPrefix(basename, ".")
}
// Parse the line, and description from an error message like:
// html/template:Application/Register.html:36: no such template "footer.html"
func ParseTemplateError(err error) (templateName string, line int, description string) {
if e, ok := err.(*Error); ok {
return "", e.Line, e.Description
}
description = err.Error()
i := regexp.MustCompile(`:\d+:`).FindStringIndex(description)
if i != nil {
line, err = strconv.Atoi(description[i[0]+1 : i[1]-1])
if err != nil {
ERROR.Println("Failed to parse line number from error message:", err)
}
templateName = description[:i[0]]
if colon := strings.Index(templateName, ":"); colon != -1 {
templateName = templateName[colon+1:]
}
templateName = strings.TrimSpace(templateName)
description = description[i[1]+1:]
}
return templateName, line, description
}
// DEPRECATED Use TemplateLang, will be removed in future release
func (loader *TemplateLoader) Template(name string) (tmpl Template, err error) {
return loader.TemplateLang(name, "")
}
// Template returns the Template with the given name. The name is the template's path
// relative to a template loader root.
//
// An Error is returned if there was any problem with any of the templates. (In
// this case, if a template is returned, it may still be usable.)
func (loader *TemplateLoader) TemplateLang(name, lang string) (tmpl Template, err error) {
if loader.compileError != nil {
return nil, loader.compileError
}
// Attempt to load a localized template first.
if lang != "" {
// Look up and return the template.
tmpl = loader.templateLoad(name + "." + lang)
}
// Return non localized version
if tmpl == nil {
tmpl = loader.templateLoad(name)
}
if tmpl == nil && err == nil {
err = fmt.Errorf("Template %s not found.", name)
}
return
}
func (loader *TemplateLoader) templateLoad(name string) (tmpl Template) {
if t,found := loader.TemplateMap[name];!found && t != nil {
tmpl = t
} else {
// Look up and return the template.
for _, engine := range loader.templatesAndEngineList {
if tmpl = engine.Lookup(name); tmpl != nil {
loader.TemplateMap[name] = tmpl
break
}
}
}
return
}
func (i *TemplateView) Location() string {
return i.FilePath
}
func (i *TemplateView) Content() (content []string) {
if i.FileBytes != nil {
// Parse the bytes
buffer := bytes.NewBuffer(i.FileBytes)
reader := bufio.NewScanner(buffer)
for reader.Scan() {
content = append(content, string(reader.Bytes()))
}
}
return nil
}
func NewBaseTemplate(templateName, filePath, basePath string, fileBytes []byte) *TemplateView {
return &TemplateView{TemplateName: templateName, FilePath: filePath, FileBytes: fileBytes, BasePath: basePath}
}