-
Notifications
You must be signed in to change notification settings - Fork 5
/
parse.go
253 lines (217 loc) · 7.53 KB
/
parse.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
package fir
import (
"fmt"
"html/template"
"io/fs"
"os"
"path"
"path/filepath"
"regexp"
"github.com/sourcegraph/conc/pool"
)
type eventTemplate map[string]struct{}
type eventTemplates map[string]eventTemplate
type readFileFunc func(string) (string, []byte, error)
func layoutEmptyContentSet(opt routeOpt, content, layoutContentName string) (*template.Template, eventTemplates, error) {
// is content html content or a file/directory
pageContentPath := filepath.Join(opt.publicDir, content)
if isFileOrString(pageContentPath, opt) {
return parseString(
template.New(
layoutContentName).
Funcs(opt.funcMap),
content)
}
// content must be a file or directory
pageFiles := getPartials(opt, find(opt, pageContentPath, opt.extensions))
contentTemplate := template.New(filepath.Base(pageContentPath)).Funcs(opt.funcMap)
return parseFiles(contentTemplate, opt.readFile, pageFiles...)
}
func layoutSetContentEmpty(opt routeOpt, layout string) (*template.Template, eventTemplates, error) {
pageLayoutPath := filepath.Join(opt.publicDir, layout)
evt := make(eventTemplates)
// is layout html content or a file/directory
if isFileOrString(pageLayoutPath, opt) {
return parseString(template.New("").Funcs(opt.funcMap), layout)
}
// layout must be a file
if isDir(pageLayoutPath, opt) {
return nil, evt, fmt.Errorf("layout %s is a directory but must be a file", pageLayoutPath)
}
// compile layout
commonFiles := getPartials(opt, []string{pageLayoutPath})
layoutTemplate := template.New(filepath.Base(pageLayoutPath)).Funcs(opt.funcMap)
return parseFiles(template.Must(layoutTemplate.Clone()), opt.readFile, commonFiles...)
}
func layoutSetContentSet(opt routeOpt, content, layout, layoutContentName string) (*template.Template, eventTemplates, error) {
layoutTemplate, evt, err := layoutSetContentEmpty(opt, layout)
if err != nil {
return nil, evt, err
}
//log.Println("compiled layoutTemplate...")
//for _, v := range layoutTemplate.Templates() {
// fmt.Println("template => ", v.Name())
//}
// 2. add content to layout
// check if content is a not a file or directory
pageContentPath := filepath.Join(opt.publicDir, content)
if isFileOrString(pageContentPath, opt) {
pageTemplate, currEvt, err := parseString(layoutTemplate, content)
if err != nil {
panic(err)
}
evt = deepMergeEventTemplates(evt, currEvt)
if err := checkPageContent(pageTemplate, layoutContentName); err != nil {
return nil, nil, err
}
return pageTemplate, evt, nil
} else {
pageFiles := getPartials(opt, []string{pageContentPath})
pageTemplate, currEvt, err := parseFiles(layoutTemplate.Funcs(opt.funcMap), opt.readFile, pageFiles...)
if err != nil {
panic(err)
}
evt = deepMergeEventTemplates(evt, currEvt)
if err := checkPageContent(pageTemplate, layoutContentName); err != nil {
return nil, nil, err
}
return pageTemplate, evt, nil
}
}
func getPartials(opt routeOpt, files []string) []string {
for _, partial := range opt.partials {
files = append(files, find(opt, filepath.Join(opt.publicDir, partial), opt.extensions)...)
}
return files
}
func checkPageContent(tmpl *template.Template, layoutContentName string) error {
if ct := tmpl.Lookup(layoutContentName); ct == nil {
return fmt.Errorf("err looking up layoutContent: the layout %s expects a template named %s",
tmpl.Name(), layoutContentName)
}
return nil
}
// creates a html/template for the route
func parseTemplate(opt routeOpt) (*template.Template, eventTemplates, error) {
// if both layout and content is empty show a default page.
if opt.layout == "" && opt.content == "" {
return template.Must(template.New("").
Parse(`<div style="text-align:center"> This is a default page. </div>`)), nil, nil
}
// if layout is set and content is empty
if opt.layout != "" && opt.content == "" {
return layoutSetContentEmpty(opt, opt.layout)
}
// if layout is empty and content is set
if opt.layout == "" && opt.content != "" {
return layoutEmptyContentSet(opt, opt.content, opt.layoutContentName)
}
// both layout and content are set
return layoutSetContentSet(opt, opt.content, opt.layout, opt.layoutContentName)
}
// creates a html/template for the route errors
func parseErrorTemplate(opt routeOpt) (*template.Template, eventTemplates, error) {
if opt.errorLayout == "" {
opt.errorLayout = opt.layout
opt.errorLayoutContentName = opt.layoutContentName
}
// if both layout and content is empty show a default page.
if opt.errorLayout == "" && opt.errorContent == "" {
return template.Must(template.New("").
Parse(`<div style="text-align:center"> This is a default page. </div>`)), nil, nil
}
// if layout is set and content is empty
if opt.errorLayout != "" && opt.errorContent == "" {
return layoutSetContentEmpty(opt, opt.errorLayout)
}
// if layout is empty and content is set
if opt.errorLayout == "" && opt.errorContent != "" {
return layoutEmptyContentSet(opt, opt.errorContent, opt.errorLayoutContentName)
}
// both layout and content are set
return layoutSetContentSet(opt, opt.errorContent, opt.errorLayout, opt.errorLayoutContentName)
}
type fileInfo struct {
name string
content []byte
eventTemplates eventTemplates
err error
}
var templateNameRegex = regexp.MustCompile(`^[ A-Za-z0-9\-:]*$`)
func parseString(t *template.Template, content string) (*template.Template, eventTemplates, error) {
fi := readAttributes(fileInfo{content: []byte(content)})
t, err := t.Parse(string(fi.content))
return t, fi.eventTemplates, err
}
func parseFiles(t *template.Template, readFile func(string) (string, []byte, error), filenames ...string) (*template.Template, eventTemplates, error) {
if len(filenames) == 0 {
// Not really a problem, but be consistent.
return t, nil, nil
}
resultPool := pool.NewWithResults[fileInfo]()
for _, filename := range filenames {
filename := filename
resultPool.Go(func() fileInfo {
name, b, err := readFile(filename)
return readAttributes(fileInfo{name: name, content: b, err: err})
})
}
evt := make(eventTemplates)
fileInfos := resultPool.Wait()
for _, fi := range fileInfos {
evt = deepMergeEventTemplates(evt, fi.eventTemplates)
if fi.err != nil {
return t, evt, fi.err
}
s := string(fi.content)
// First template becomes return value if not already defined,
// and we use that one for subsequent New calls to associate
// all the templates together. Also, if this file has the same name
// as t, this file becomes the contents of t, so
// t, err := New(name).Funcs(xxx).ParseFiles(name)
// works. Otherwise we create a new template associated with t.
var tmpl *template.Template
if t == nil {
t = template.New(fi.name)
}
if fi.name == t.Name() {
tmpl = t
} else {
tmpl = t.New(fi.name)
}
_, err := tmpl.Parse(s)
if err != nil {
return t, evt, err
}
}
return t, evt, nil
}
func readFileOS(file string) (name string, b []byte, err error) {
name = filepath.Base(file)
b, err = os.ReadFile(file)
return
}
func readFileFS(fsys fs.FS) func(string) (string, []byte, error) {
return func(file string) (name string, b []byte, err error) {
name = path.Base(file)
b, err = fs.ReadFile(fsys, file)
return
}
}
func deepMergeEventTemplates(evt1, evt2 eventTemplates) eventTemplates {
merged := make(eventTemplates)
for eventID, templatesMap := range evt1 {
merged[eventID] = templatesMap
}
for eventID, templatesMap := range evt2 {
templatesMap1, ok := merged[eventID]
if !ok {
merged[eventID] = templatesMap
continue
}
for templateName := range templatesMap {
templatesMap1[templateName] = struct{}{}
}
}
return merged
}