-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
266 lines (239 loc) · 5.88 KB
/
app.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
254
255
256
257
258
259
260
261
262
263
264
265
266
package main
import (
"fmt"
"net/http"
"os"
"strings"
"time"
"github.com/jehiah/go-strftime"
"github.com/rjeczalik/notify"
"github.com/spf13/afero"
"github.com/yanzay/log"
"github.com/gorook/rook/assets/newsite"
"github.com/gorook/rook/config"
"github.com/gorook/rook/fs"
"github.com/gorook/rook/site"
"github.com/gorook/rook/theme"
)
const (
configFileName = "config.yml"
blogDirName = "posts"
docsDirName = "docs"
themeDirName = "_theme"
staticDirName = "static"
publicDirName = "public"
dateTimeFormat = "%Y-%m-%d %H:%M:%S"
)
type appOption int
const (
appDefault appOption = iota
appRenderToMemory
appInMemory
)
type application struct {
fs *fs.FS
config *config.SiteConfig
blog *site.Site
docs *site.Site
theme *theme.Theme
rendered map[string]string // {path: rendered page}
local bool
}
func newApplication(opt appOption) *application {
var readfs, writefs afero.Fs
var local bool
switch opt {
case appDefault:
readfs = afero.NewOsFs()
writefs = readfs
case appRenderToMemory:
readfs = afero.NewOsFs()
writefs = afero.NewMemMapFs()
local = true
case appInMemory:
readfs = afero.NewMemMapFs()
writefs = readfs
}
return &application{
fs: fs.New(readfs, writefs),
local: local,
}
}
func (a *application) clean() error {
return a.fs.RemoveAll(publicDirName)
}
func (a *application) init(addr string) error {
var err error
a.config, err = config.FromFile(a.fs, configFileName)
if err != nil {
return err
}
if a.local {
a.config.BaseURL = fmt.Sprintf("http://%s/", addr)
}
a.blog, err = site.FromDir(a.fs, a.config, blogDirName, site.ContentTypeBlog)
if err != nil {
return fmt.Errorf("unable to load blog: %v", err)
}
a.docs, err = site.FromDir(a.fs, a.config, docsDirName, site.ContentTypeDocs)
if err != nil {
return fmt.Errorf("unable to load docs: %v", err)
}
a.theme, err = theme.FromDir(a.fs, themeDirName)
return err
}
func (a *application) prepare() {
a.theme.SetConfig(a.config)
a.theme.SetTags(a.blog.AllTags())
}
func (a *application) build() error {
err := a.clean()
if err != nil {
return err
}
a.prepare()
a.renderAll()
err = a.saveAll()
if err != nil {
return err
}
err = a.copyStatic()
return err
}
func (a *application) renderAll() {
a.rendered = make(map[string]string)
for _, page := range a.blog.Pages {
a.rendered[page.Path] = a.theme.RenderPost(page)
}
for _, ipage := range a.blog.IndexPages {
a.rendered[ipage.Path] = a.theme.RenderIndex(ipage)
}
for _, tag := range a.blog.TagPages {
for _, tpage := range tag {
a.rendered[tpage.Path] = a.theme.RenderIndex(tpage)
}
}
for _, page := range a.docs.Pages {
a.rendered[page.Path] = a.theme.RenderPage(page)
}
}
func (a *application) renderChanged(path string) {
page := a.blog.ByPath(path)
a.rendered[page.Path] = a.theme.RenderPost(page)
for _, ipage := range a.blog.IndexPages {
a.rendered[ipage.Path] = a.theme.RenderIndex(ipage)
}
for _, tag := range a.blog.TagPages {
for _, tpage := range tag {
a.rendered[tpage.Path] = a.theme.RenderIndex(tpage)
}
}
}
func (a *application) saveAll() error {
for path, content := range a.rendered {
dir := publicDirName + "/" + path
err := a.fs.MkDirAll(dir)
if err != nil {
return err
}
err = a.fs.WriteFile(dir+"/index.html", []byte(content))
if err != nil {
return err
}
}
return nil
}
func (a *application) copyStatic() error {
siteStatic := fmt.Sprintf("%s/", staticDirName)
themeStatic := fmt.Sprintf("%s/%s/", themeDirName, staticDirName)
publicStatic := fmt.Sprintf("%s/%s/", publicDirName, staticDirName)
err := a.fs.CopyTree(siteStatic, publicDirName+"/")
if err != nil {
return err
}
a.fs.MkDirAll(publicStatic)
return a.fs.CopyTree(themeStatic, publicStatic)
}
func (a *application) startServer(addr string) error {
newWatcher("posts/...", a.contentChanged)
newWatcher("_theme/...", a.themeChanged)
newWatcher("config.yml", a.configChanged)
err := a.build()
if err != nil {
return err
}
handler := a.fs.HTTP(publicDirName)
log.Infof("Listening on %s", a.config.BaseURL)
return http.ListenAndServe(addr, handler)
}
func (a *application) contentChanged(e notify.EventInfo) {
start := time.Now()
wd, err := os.Getwd()
if err != nil {
log.Errorf("unable to get current dir: %v", err)
return
}
path := strings.TrimPrefix(e.Path(), wd+"/")
log.Infof("Content changed: '%s'. Rebuilding...", path)
err = a.blog.Rebuild(a.fs, path)
if err != nil {
log.Errorf("unable to rebuild site: %v", err)
return
}
a.theme.SetTags(a.blog.AllTags())
a.renderChanged(path)
err = a.saveAll()
if err != nil {
log.Errorf("unable to save site: %v", err)
return
}
log.Infof("Done in %s", time.Since(start))
}
func (a *application) themeChanged(e notify.EventInfo) {
start := time.Now()
log.Infof("Theme changed. Rebuilding...")
var err error
a.theme, err = theme.FromDir(a.fs, themeDirName)
if err != nil {
log.Errorf("unable to load theme: %v", err)
return
}
err = a.build()
if err != nil {
log.Errorf("unable to build site: %v", err)
return
}
log.Infof("Done in %s", time.Since(start))
}
func (a *application) configChanged(e notify.EventInfo) {
start := time.Now()
err := a.init("")
if err != nil {
log.Errorf("unable to init application: %v", err)
return
}
err = a.build()
if err != nil {
log.Errorf("unable to build site: %v", err)
return
}
log.Infof("Done in %s", time.Since(start))
}
func (a *application) createPost(name string) error {
data := map[string]string{
"date": strftime.Format(dateTimeFormat, time.Now()),
}
content := theme.Exec(a.fs, "_theme/post.md", data)
err := a.fs.WriteFile(blogDirName+"/"+name, []byte(content))
if err != nil {
return fmt.Errorf("unable to write file: %v", err)
}
return nil
}
func (a *application) createSite(name string) error {
err := newsite.RestoreAssets(name, "")
if err != nil {
return fmt.Errorf("unable to create new site: %v", err)
}
return nil
}