forked from yaitoo/xun
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviewengine_html.go
More file actions
175 lines (137 loc) · 4.02 KB
/
viewengine_html.go
File metadata and controls
175 lines (137 loc) · 4.02 KB
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
package xun
import (
"io/fs"
"log/slog"
"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) {
if ve.templates == nil {
ve.templates = map[string]*HtmlTemplate{}
}
ve.fsys = fsys
ve.app = app
ve.loadComponents()
ve.loadLayouts()
ve.loadPages()
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, app.funcMap)
}
} 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) loadFiles(dir string, process func(path string) error) {
fs.WalkDir(ve.fsys, dir, func(path string, d fs.DirEntry, _ error) error { // nolint: errcheck
if !strings.EqualFold(filepath.Ext(path), ".html") {
return nil
}
if err := process(path); err != nil {
ve.app.logger.Error("xun: load html", slog.String("path", path), slog.Any("err", err))
}
return nil
})
}
func (ve *HtmlViewEngine) loadComponents() {
ve.loadFiles("components", func(path string) error {
_, err := ve.loadTemplate(path)
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, ve.app.funcMap); 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() {
ve.loadFiles("layouts", func(path string) error {
_, err := ve.loadTemplate(path)
return err
})
}
func (ve *HtmlViewEngine) loadPages() {
ve.loadFiles("pages", func(path string) error { // nolint: errcheck
return ve.loadPage(path)
})
}
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, ve.app.funcMap); 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() {
ve.loadFiles("views", func(path string) error {
return ve.loadView(path)
})
}
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
}