This repository has been archived by the owner on Oct 5, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.go
217 lines (183 loc) · 5.67 KB
/
render.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
package commands
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"time"
"github.com/fsnotify/fsnotify"
"github.com/urfave/cli/v2"
"github.com/wilhelm-murdoch/go-collection"
"github.com/wilhelm-murdoch/go-stash/config"
"github.com/wilhelm-murdoch/go-stash/models"
"github.com/wilhelm-murdoch/go-stash/template"
"github.com/wilhelm-murdoch/go-stash/writers"
)
// RenderHandler
func RenderHandler(c *cli.Context, cfg *config.Configuration) error {
if err := render(c, cfg); err != nil {
return err
}
if !c.Bool("watch") {
return nil
}
return watch(c, cfg)
}
// UnmarshalWalkCollection
func UnmarshalWalkCollection[B models.Bloggable](basePath string) (*collection.Collection[B], error) {
items := collection.New[B]()
err := filepath.Walk(basePath, func(path string, info os.FileInfo, err error) error {
if strings.HasSuffix(info.Name(), ".json") && err == nil && info.Name() != "index.json" {
content, err := ioutil.ReadFile(path)
if err != nil {
return err
}
var item B
if err := json.Unmarshal(content, &item); err != nil {
return err
}
if err != nil {
return fmt.Errorf("failed to unmarshal `%s`: %s", path, err)
}
items.Push(item)
}
return nil
})
return items, err
}
// watch
func watch(c *cli.Context, cfg *config.Configuration) error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
defer watcher.Close()
var (
quit = make(chan bool)
done = make(chan bool)
errors = make(chan error)
)
go func() {
for {
select {
case event := <-watcher.Events:
switch {
case event.Op&fsnotify.Chmod != fsnotify.Chmod:
if err := render(c, cfg); err != nil {
errors <- err
}
}
case err := <-watcher.Errors:
errors <- err
}
}
}()
err = filepath.Walk(fmt.Sprintf("%s/%s", cfg.Paths.Root, cfg.Paths.Templates), func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
if err = watcher.Add(path); err != nil {
return err
}
}
return nil
})
if err != nil {
return err
}
for {
select {
case err := <-errors:
close(quit)
return err
case <-done:
return nil
}
}
}
// render
func render(c *cli.Context, cfg *config.Configuration) error {
log.Println("unmarshaling posts from local json store")
posts, err := UnmarshalWalkCollection[models.Post](fmt.Sprintf("%s/%s", cfg.Paths.Root, cfg.Paths.Posts))
if err != nil {
return fmt.Errorf("could not unmarshal posts from local store: %s", err)
}
posts.Sort(func(i, j int) bool {
l, _ := posts.At(i)
r, _ := posts.At(j)
lDate, _ := time.Parse(time.RFC3339, l.DateAdded)
rDate, _ := time.Parse(time.RFC3339, r.DateAdded)
return lDate.Unix() > rDate.Unix()
})
log.Println("unmarshaling tags from local json store")
tags, err := UnmarshalWalkCollection[models.Tag](fmt.Sprintf("%s/%s", cfg.Paths.Root, cfg.Paths.Tags))
if err != nil {
return fmt.Errorf("could not unmarshal tags from local store: %s", err)
}
log.Println("unmarshaling authors from local json store")
authors, err := UnmarshalWalkCollection[models.Author](fmt.Sprintf("%s/%s", cfg.Paths.Root, cfg.Paths.Authors))
if err != nil {
return fmt.Errorf("could not unmarshal authors from local store: %s", err)
}
log.Println("rendering rss and atom feeds")
if err := writers.WriteFeeds(cfg, posts); err != nil {
return fmt.Errorf("failed to write feeds to %s: %s", cfg.Paths.Root, err)
}
data := []template.TemplateData{
{Name: "Posts", Data: posts.Items()},
{Name: "Tags", Data: tags.Items()},
{Name: "Authors", Data: authors.Items()},
{Name: "Config", Data: cfg},
}
sitemap := writers.NewSitemap()
for _, mapping := range cfg.Mappings {
log.Printf("rendering %s mappings to html", mapping.Type)
switch mapping.Type {
case config.Index:
if err := writers.WriteHtml(fmt.Sprintf("%s/index.html", cfg.Paths.Root), mapping, cfg, data...); err != nil {
return fmt.Errorf("could not render %s mapping: %s", mapping.Type, err)
}
sitemap.AddUrl(fmt.Sprintf("%s/index.html", cfg.Url), time.Now().Format(time.RFC3339))
case config.Page:
if err := writers.WriteHtml(fmt.Sprintf("%s/%s", cfg.Paths.Root, mapping.Output), mapping, cfg, data...); err != nil {
return fmt.Errorf("could not render %s mapping: %s", mapping.Type, err)
}
sitemap.AddUrl(fmt.Sprintf("%s/%s", cfg.Url, mapping.Output), time.Now().Format(time.RFC3339))
case config.Post:
if err := writers.WriteHtmlCollection(fmt.Sprintf("%s/%s", cfg.Paths.Root, cfg.Paths.Posts), posts, mapping, cfg, data...); err != nil {
return fmt.Errorf("could not render %s mapping: %s", mapping.Type, err)
}
case config.Tag:
if err := writers.WriteHtmlCollection(fmt.Sprintf("%s/%s", cfg.Paths.Root, cfg.Paths.Tags), tags, mapping, cfg, data...); err != nil {
return fmt.Errorf("could not render %s mapping: %s", mapping.Type, err)
}
case config.Author:
if err := writers.WriteHtmlCollection(fmt.Sprintf("%s/%s", cfg.Paths.Root, cfg.Paths.Authors), authors, mapping, cfg, data...); err != nil {
return fmt.Errorf("could not render %s mapping: %s", mapping.Type, err)
}
default:
log.Printf("mapping type %s is currently not supported", mapping.Type)
}
}
posts.Each(func(_ int, post models.Post) bool {
sitemap.AddUrl(post.GetUrl(cfg), post.GetDateUpdated())
return false
})
tags.Each(func(_ int, tag models.Tag) bool {
sitemap.AddUrl(tag.GetUrl(cfg), tag.GetDateUpdated())
return false
})
authors.Each(func(_ int, author models.Author) bool {
sitemap.AddUrl(author.GetUrl(cfg), author.GetDateUpdated())
return false
})
sitemap.Save(cfg.Paths.Root)
if err := sitemap.UpsertRobots(cfg); err != nil {
return err
}
return nil
}