-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathviewengine_html.go
237 lines (175 loc) · 4.63 KB
/
viewengine_html.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
package xun
import (
"errors"
"io/fs"
"path/filepath"
"strings"
"github.com/yaitoo/xun/fsnotify"
)
// HtmlViewEngine is a view engine that loads templates from a file system.
//
// It supports 2 types of templates:
// - Components: These are templates that are loaded from the "components" directory.
// - Pages: These are templates that are loaded from the "layouts/views/pages/" directory.
//
// Components are used to build up larger templates, while pages are used to render
// the final HTML that is sent to the client.
type HtmlViewEngine struct {
fsys fs.FS
app *App
templates map[string]*HtmlTemplate
}
// Load loads all templates from the given file system.
//
// It loads all components, layouts, pages and views from the given file system.
func (ve *HtmlViewEngine) Load(fsys fs.FS, app *App) error {
if ve.templates == nil {
ve.templates = map[string]*HtmlTemplate{}
}
ve.fsys = fsys
ve.app = app
err := ve.loadComponents()
if err != nil {
return err
}
err = ve.loadLayouts()
if err != nil {
return err
}
err = ve.loadPages()
if err != nil {
return err
}
return ve.loadViews()
}
// FileChanged is called when a file has been changed.
//
// It is used to reload templates when they have been changed.
func (ve *HtmlViewEngine) FileChanged(fsys fs.FS, app *App, event fsnotify.Event) error { // skipcq: RVV-B0012
if event.Has(fsnotify.Remove) || !strings.EqualFold(filepath.Ext(event.Name), ".html") {
return nil
}
name := event.Name[:len(event.Name)-5]
if event.Has(fsnotify.Write) {
t, ok := ve.templates[name]
if ok {
return t.Reload(fsys, ve.templates)
}
} else if event.Has(fsnotify.Create) {
if strings.HasPrefix(event.Name, "components/") || strings.HasPrefix(event.Name, "layouts/") {
_, err := ve.loadTemplate(event.Name)
return err
} else if strings.HasPrefix(event.Name, "pages/") {
return ve.loadPage(event.Name)
} else if strings.HasPrefix(event.Name, "views/") {
return ve.loadView(event.Name)
}
return nil
}
return nil
}
func (ve *HtmlViewEngine) loadComponents() error {
err := fs.WalkDir(ve.fsys, "components", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() || !strings.EqualFold(filepath.Ext(path), ".html") {
return nil
}
_, err = ve.loadTemplate(path)
return err
})
if err != nil && errors.Is(err, fs.ErrNotExist) {
return nil
}
return err
}
func (ve *HtmlViewEngine) loadTemplate(path string) (*HtmlTemplate, error) {
name := path[:len(path)-5]
t := NewHtmlTemplate(name, path)
if err := t.Load(ve.fsys, ve.templates); err != nil {
return nil, err
}
for n := range t.dependencies {
d, ok := ve.templates[n]
if ok {
d.dependents[name] = t
}
}
ve.templates[name] = t
return t, nil
}
func (ve *HtmlViewEngine) loadLayouts() error {
err := fs.WalkDir(ve.fsys, "layouts", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() {
_, err = ve.loadTemplate(path)
return err
}
return nil
})
if err != nil && errors.Is(err, fs.ErrNotExist) {
return nil
}
return err
}
func (ve *HtmlViewEngine) loadPages() error {
err := fs.WalkDir(ve.fsys, "pages", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() || !strings.EqualFold(filepath.Ext(path), ".html") {
return nil
}
return ve.loadPage(path)
})
if err != nil && errors.Is(err, fs.ErrNotExist) {
return nil
}
return err
}
func (ve *HtmlViewEngine) loadPage(path string) error {
name := path[6:] // delete prefix "pages/"
t := NewHtmlTemplate(name, path)
if err := t.Load(ve.fsys, ve.templates); err != nil {
return err
}
// delete file extension ".html"
ve.templates[path[:len(path)-5]] = t
if strings.HasSuffix(path, "/index.html") { // remove it, because index.html will be redirected to ./ in http.ServeFileFS
name = name[:len(name)-10]
}
_, _, pattern := splitFile(name)
pattern = strings.TrimSuffix(pattern, ".html")
ve.app.HandlePage(pattern, path[6:len(path)-5], &HtmlViewer{
template: t,
})
return nil
}
func (ve *HtmlViewEngine) loadViews() error {
err := fs.WalkDir(ve.fsys, "views", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() || !strings.EqualFold(filepath.Ext(path), ".html") {
return nil
}
return ve.loadView(path)
})
if err != nil && errors.Is(err, fs.ErrNotExist) {
return nil
}
return err
}
func (ve *HtmlViewEngine) loadView(path string) error {
t, err := ve.loadTemplate(path)
if err != nil {
return err
}
ve.app.viewers[path[:len(path)-5]] = &HtmlViewer{
template: t,
}
return nil
}