-
Notifications
You must be signed in to change notification settings - Fork 0
/
show.go
63 lines (51 loc) · 1.22 KB
/
show.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
package printers
import (
"html/template"
"io/fs"
"log"
"os"
"path/filepath"
"sort"
"github.com/pgeowng/japoto/config"
"github.com/pgeowng/japoto/types"
)
type Show struct {
Provider string
Name string
}
func (show Show) Print(entries []types.Entry) {
files := []string{
"./template/base.layout.tmpl",
"./template/show.content.tmpl",
}
err := os.MkdirAll(filepath.Join(config.Dest, show.Provider), fs.ModePerm)
if err != nil {
log.Fatal(err)
}
ts, err := template.ParseFiles(files...)
if err != nil {
log.Fatal(err)
}
fpath := filepath.Join(config.Dest, show.Provider, show.Name+".html")
f, err := os.Create(fpath)
if err != nil {
log.Fatalf("%s create error: %v\n", fpath, err)
}
defer f.Close()
entries = FilterEntries(entries, func(entry types.Entry) bool {
return entry.ShowId == show.Name && entry.Provider == show.Provider
})
sort.Slice(entries, func(i, j int) bool {
return entries[i].Title > entries[j].Title
})
err = ts.Execute(f, map[string]interface{}{
"PublicURL": config.PublicURL,
"CreateTime": config.CreateTime,
"Provider": show.Provider,
"ShowName": show.Name,
"Entries": entries,
})
if err != nil {
log.Fatalf("%s write error: %v\n", fpath, err)
}
}