-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathtemplates.go
More file actions
70 lines (59 loc) · 2.19 KB
/
templates.go
File metadata and controls
70 lines (59 loc) · 2.19 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
// SPDX-License-Identifier: MIT
package main
import (
"embed"
"fmt"
"html/template"
"os"
)
//go:embed asset/templates/bare/*.html asset/templates/dark/*.html asset/templates/light/*.html
var templateFS embed.FS
var validStyles = []string{"dark", "light", "bare"}
// resolveSearchTemplate returns the parsed search template. Custom file override
// (--template-search) takes priority, then the embedded theme (--template-style).
func resolveSearchTemplate(cfg *Config) (*template.Template, error) {
if cfg.SearchTemplate != "" {
data, err := os.ReadFile(cfg.SearchTemplate)
if err != nil {
return nil, fmt.Errorf("reading custom search template %q: %w", cfg.SearchTemplate, err)
}
return template.New("search").Parse(string(data))
}
if !isValidStyle(cfg.TemplateStyle) {
return nil, fmt.Errorf("unknown template style %q (valid: dark, light, bare)", cfg.TemplateStyle)
}
path := fmt.Sprintf("asset/templates/%s/search.html", cfg.TemplateStyle)
data, err := templateFS.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("reading embedded search template for style %q: %w", cfg.TemplateStyle, err)
}
return template.New("search").Parse(string(data))
}
// resolveDisplayTemplate returns the parsed display template. Custom file override
// (--template-display) takes priority, then the embedded theme (--template-style).
func resolveDisplayTemplate(cfg *Config) (*template.Template, error) {
if cfg.DisplayTemplate != "" {
data, err := os.ReadFile(cfg.DisplayTemplate)
if err != nil {
return nil, fmt.Errorf("reading custom display template %q: %w", cfg.DisplayTemplate, err)
}
return template.New("display").Parse(string(data))
}
if !isValidStyle(cfg.TemplateStyle) {
return nil, fmt.Errorf("unknown template style %q (valid: dark, light, bare)", cfg.TemplateStyle)
}
path := fmt.Sprintf("asset/templates/%s/display.html", cfg.TemplateStyle)
data, err := templateFS.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("reading embedded display template for style %q: %w", cfg.TemplateStyle, err)
}
return template.New("display").Parse(string(data))
}
func isValidStyle(style string) bool {
for _, s := range validStyles {
if s == style {
return true
}
}
return false
}