This repository has been archived by the owner on May 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
template.go
108 lines (97 loc) · 2.8 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
package internal
import (
"bytes"
_ "embed"
"errors"
html "html/template"
"io/fs"
"os"
"path/filepath"
"time"
)
//go:embed default.html
var defaultTemplate string
const (
filePerm fs.FileMode = 0644
dirPerm fs.FileMode = 0755
)
// TemplateFuncs contains some simple helper functions available inside a template.
var TemplateFuncs = html.FuncMap{
"shortdate": func(t time.Time) string {
return t.Format("2006-01-02")
},
"prevday": func(t time.Time) time.Time {
return t.AddDate(0, 0, -1)
},
"nextday": func(t time.Time) time.Time {
return t.AddDate(0, 0, 1)
},
}
// TemplateGenerator contains the basic info for this generator.
type TemplateGenerator struct {
Name string
Version string
Source string
}
// TemplateFeed contains a feed and it's parsed output (items or an error).
type TemplateFeed struct {
Conf Feed // Basic config for the feed
Items []Item // Any parsed and filtered items
Error error // Error returned when trying to download/parse the feed
}
// TemplateVars is a set of basic info that can be provided when executing/writing a template.
type TemplateVars struct {
Today time.Time // Current time
Generator TemplateGenerator // Basic generator info
Feeds []TemplateFeed // List of feeds and their config, items and errors
}
// NewTemplateVars creates a new instance and adds current time and generator info to it.
// The Feeds field will be empty and has to be manually updated.
func NewTemplateVars() TemplateVars {
return TemplateVars{
Today: time.Now(),
Generator: TemplateGenerator{
Name: GeneratorName,
Version: GeneratorVersion,
Source: GeneratorSource,
},
}
}
// LoadTemplates tries to parse a template from file or use a default template.
// The returned template has no name and has some helper functions declared.
func LoadTemplate(file string) (tmpl *html.Template, err error) {
tmpl = html.New("").Funcs(TemplateFuncs)
if len(file) == 0 {
tmpl, err = tmpl.Parse(defaultTemplate)
} else {
var b []byte
b, err = os.ReadFile(file)
if err == nil {
tmpl, err = tmpl.Parse(string(b))
}
}
return
}
// WriteTemplate executes a loaded template (using provided vars) and writes
// it's output to a file.
func WriteTemplate(file string, tmpl *html.Template, vars interface{}) error {
var buf bytes.Buffer
if err := tmpl.Execute(&buf, vars); err != nil {
return err
}
d := filepath.Dir(file)
if err := os.MkdirAll(d, dirPerm); err != nil {
return err
}
return os.WriteFile(file, bytes.TrimSpace(buf.Bytes()), filePerm)
}
// Symlink tries to make a new symlink dst pointing to file src.
func Symlink(src, dst string) error {
if err := os.Remove(dst); err != nil {
// Ignore error if the symlink simply doesn't exist yet
if !errors.Is(err, os.ErrNotExist) {
return err
}
}
return os.Symlink(filepath.Base(src), dst)
}