-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.go
235 lines (176 loc) · 4.03 KB
/
generator.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
package main
import (
"encoding/json"
"html/template"
"os"
"path/filepath"
cp "github.com/otiai10/copy"
"github.com/bytehunger/snack/sitemap"
)
const outputDir = "build"
type Generator struct {
Adapter Adapter
}
func (g *Generator) Generate() error {
// Get the site from the adapter.
site, err := g.Adapter.NewSite()
if err != nil {
return err
}
// Start the actual generating
for _, page := range site.Pages {
renderData := RenderData{
Site: &site,
Page: &page,
}
err := g.GeneratePage(&renderData)
if err != nil {
return err
}
}
assetsPath := filepath.Join("themes", site.Theme, "assets")
// Copy the themes assets folder if present.
if _, err = os.Stat(assetsPath); !os.IsNotExist(err) {
err = cp.Copy(
assetsPath,
filepath.Join(outputDir, "assets"),
)
if err != nil {
return err
}
}
// Copy a pictures folder if present.
if _, err := os.Stat("pictures"); !os.IsNotExist(err) {
err = cp.Copy("pictures", filepath.Join(outputDir, "pictures"))
if err != nil {
return err
}
}
// Copy a data folder if present.
if _, err := os.Stat("data"); !os.IsNotExist(err) {
err = cp.Copy("data", filepath.Join(outputDir, "data"))
if err != nil {
return err
}
}
err = g.GenerateSitemap(site)
if err != nil {
return err
}
return g.GenerateContentJSON(site)
}
func (g *Generator) GeneratePage(data *RenderData) error {
header, err := template.New("header.html.tpl").Funcs(funcMap).ParseFiles(
filepath.Join("themes", data.Site.Theme, "header.html.tpl"),
)
if err != nil {
return err
}
footer, err := template.New("footer.html.tpl").Funcs(funcMap).ParseFiles(
filepath.Join("themes", data.Site.Theme, "footer.html.tpl"),
)
if err != nil {
return err
}
path := filepath.Join(outputDir, data.Page.Pathname())
err = os.MkdirAll(path, 0755)
if err != nil {
return err
}
f, err := os.Create(filepath.Join(path, data.Page.Filename()))
if err != nil {
return err
}
defer f.Close()
err = header.Execute(f, data)
if err != nil {
return err
}
// Concatenate all global and local sections.
sections := append(
append(
data.Site.GlobalSections.Header,
data.Page.Sections...,
),
data.Site.GlobalSections.Footer...,
)
for _, section := range sections {
// Don't render inactive sections.
if section.Inactive {
continue
}
basename := section.Type + ".html.tpl"
data.Section = section
tpl, err := template.New(basename).
Funcs(funcMap).
ParseFiles(
filepath.Join(
"themes",
data.Site.Theme,
"sections", basename,
),
)
if err != nil {
return err
}
err = tpl.Execute(f, data)
if err != nil {
return err
}
}
err = footer.Execute(f, data)
if err != nil {
return err
}
return nil
}
func (g *Generator) GenerateContentJSON(site Site) error {
type ContentPage struct {
Title string `json:"title"`
Path string `json:"path"`
Description string `json:"description"`
Sections []Section `json:"-"`
NoIndex bool `json:"-"`
PublishedAt W3CDate `json:"publishedAt"`
UpdatedAt W3CDate `json:"updatedAt"`
}
pages := []ContentPage{}
for _, page := range site.Pages {
if page.NoIndex {
continue
}
pages = append(pages, ContentPage(page))
}
content, err := json.Marshal(pages)
if err != nil {
return err
}
path := filepath.Join(outputDir, "content.json")
return os.WriteFile(path, content, 0644)
}
func (g *Generator) GenerateSitemap(site Site) error {
urls := []sitemap.URL{}
for _, page := range site.Pages {
// Do not include noindex pages.
if page.NoIndex {
continue
}
url := sitemap.URL{
Loc: page.URL(site.Host),
}
if page.PublishedAt.IsDate() {
url.LastMod = page.PublishedAt.String()
}
if page.UpdatedAt.IsDate() {
url.LastMod = page.UpdatedAt.String()
}
// Append all pages to the URLSet.
urls = append(urls, url)
}
urlset := sitemap.URLSet{
URLs: urls,
XMLNS: "http://www.sitemaps.org/schemas/sitemap/0.9",
}
path := filepath.Join(outputDir, "sitemap.xml")
return urlset.Write(path)
}