forked from zupzup/blog-generator
/
generator.go
273 lines (252 loc) · 6.84 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
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
267
268
269
270
271
272
273
package generator
import (
"fmt"
FU "github.com/fbaube/fileutils"
SU "github.com/fbaube/stringutils"
"html/template"
"path/filepath"
"sort"
S "strings"
"strconv"
"sync"
"github.com/morningconsult/serrors"
)
// DO_SEQUENTIALLY simplifies debugging by forcing all Generators
// to run sequentially - not concurrently - in func runTasks()
var DO_SEQUENTIALLY = true
// Generator interface
type Generator interface {
Generate() error
}
// SiteGenerator object
type SiteGenerator struct {
StConfig *SiteConfig
}
// SiteConfig holds the sources and destination folder
type SiteConfig struct {
Sources []string
Dest string
Configs []SU.PropSet
}
// New creates a new SiteGenerator.
func New(config *SiteConfig) *SiteGenerator {
return &SiteGenerator{StConfig: config}
}
// Generate starts the static blog generation.
func (g *SiteGenerator) Generate() error {
masterPageTmplPath := filepath.Join("template", "masterpagetemplate.html")
sources := g.StConfig.Sources
destination := g.StConfig.Dest
fmt.Printf("SiteGenr: dest<%s> \n", destination)
// fmt.Printf(" sources: %+v \n", sources)
// Clear the WWW output directory and its "archive" subdirectory
if err := FU.ClearAndCreateDirectory(destination); err != nil {
return err
}
if err := FU.ClearAndCreateDirectory(filepath.Join(destination, "archive")); err != nil {
return err
}
// Get the master page template
masterPageTemplate, err := getTemplate(masterPageTmplPath)
if err != nil {
return err
}
// Get all the posts
var posts []*Post
for _, path := range sources {
println("##>> Adding post, from wrkg-dir:", path)
post, err := newPost(path, g.StConfig.Configs[1]["dateformat"])
if err != nil {
return err
}
posts = append(posts, post)
}
sort.Sort(ByDateDesc(posts))
if err := runTasks(posts, masterPageTemplate, destination, g.StConfig.Configs); err != nil {
return err
}
fmt.Println("Finished generating Site...")
return nil
}
func runTasks(posts []*Post, masterPageTemplate *template.Template,
destination string, cfgs []SU.PropSet) error {
generators := []Generator{}
blogProps := cfgs[1]
// ==========================
// POSTS
// ==========================
for _, post := range posts {
pPC := new(PostConfig)
pPC.Post = post
pPC.Dest = destination
pPC.Template = masterPageTemplate
pPC.BlogProps = blogProps
// println(pPC.String())
pg := PostGenerator{pPC}
generators = append(generators, &pg)
// fmt.Printf("##>> Ready post: %+v |||| \n", *pPC)
}
tagPostsMap := createTagPostsMap(posts)
// ==========================
// FRONT PAGE
// ==========================
nrposts, _ := strconv.Atoi(cfgs[1]["frontpageposts"])
pLC := new(ListingConfig)
pLC.Posts = posts[:getNumOfPagesOnFrontpage(posts, nrposts)]
pLC.Template = masterPageTemplate
pLC.Dest = destination
pLC.PageTitle = ""
pLC.BlogProps = blogProps
pLC.IsIndex = true
// println(pLC.String())
fg := ListingGenerator{pLC}
// ==========================
// ARCHIVE
// ==========================
pAC := new(ListingConfig)
pAC.Posts = posts
pAC.Template = masterPageTemplate
pAC.Dest = filepath.Join(destination, "archive")
pAC.PageTitle = "Archive"
pAC.BlogProps = blogProps
pAC.IsIndex = false
// println(pAC.String())
ag := ListingGenerator{pAC}
// ==========================
// TAGS
// ==========================
pTC := new(TagsConfig)
pTC.TagPostsMap = tagPostsMap
pTC.Template = masterPageTemplate
pTC.Dest = destination
pTC.BlogProps = blogProps
println("TagsCfg:", pTC.BaseConfig.String(),
fmt.Sprintf("; \n\t TagPostsMap: %+v", pTC.TagPostsMap))
tg := TagsGenerator{pTC}
staticURLs := []string{}
var tmpl = cfgs[2]["templates"]
var tmpls = S.Split(tmpl, " ")
fmt.Printf("TMPLS: %v \n", tmpls)
for _, staticURL := range tmpls {
staticURLs = append(staticURLs, staticURL) // .Dest)
}
// ==========================
// SITEMAP
// ==========================
sg := SitemapGenerator{&SitemapConfig{
Posts: posts,
TagPostsMap: tagPostsMap,
Destination: destination,
BlogURL: cfgs[1]["url"],
Statics: staticURLs,
}}
// ==========================
// RSS
// ==========================
pRC := new(RSSConfig)
pRC.BlogProps = cfgs[1]
pRC.Posts = posts
pRC.Dest = destination
rg := RSSGenerator{pRC}
// ==========================
// STATICS
// ==========================
pSC := new(StaticsConfig)
// ==========================
// TEMPLATES
// ==========================
psTmplsToFiles := SU.PropSet{} // map[string]string{}
for _, staticFrom := range tmpls {
staticTo := S.TrimSuffix(staticFrom, ".html")
staticTo = S.TrimSuffix(staticTo, ".htm")
psTmplsToFiles["static/" + staticFrom] = filepath.Join(destination, staticTo, "index.html")
}
// ==========================
// TEMPLATES
// ==========================
pSC.TmplsToFiles = psTmplsToFiles
pSC.Dest = cfgs[0]["dest"]
pSC.Template = masterPageTemplate
pSC.BlogProps = blogProps
fmt.Printf("StcsCfg: %s; \n\t %+v \n",
pSC.BaseConfig.String(), pSC.TmplsToFiles)
statg := StaticsGenerator{pSC}
generators = append(generators, &fg, &ag, &tg, &sg, &rg, &statg)
if DO_SEQUENTIALLY {
for _, g := range generators {
if err := g.Generate(); err != nil {
println("ERROR:", err.Error())
return err
}
}
return nil
}
var wg sync.WaitGroup
done := make(chan bool, 1)
errs := make(chan error, 1)
pool := make(chan struct{}, 50)
for _, generator := range generators {
wg.Add(1)
go func(g Generator) {
defer wg.Done()
pool <- struct{}{}
defer func() { <-pool }()
if err := g.Generate(); err != nil {
errs <- err
}
}(generator)
}
go func() {
wg.Wait()
close(done)
}()
select {
case <-done:
return nil
case err := <-errs:
if err != nil {
return err
}
}
return nil
}
func createTagPostsMap(posts []*Post) map[string][]*Post {
result := make(map[string][]*Post)
for _, post := range posts {
tags := S.Split(post.PropSet["tags"], " ")
for _, tag := range tags {
key := S.ToLower(tag)
if result[key] == nil {
result[key] = []*Post{post}
} else {
result[key] = append(result[key], post)
}
}
}
return result
}
func getTemplate(path string) (*template.Template, error) {
t, err := template.ParseFiles(path)
if err != nil {
return nil, serrors.Errorf("error reading template %s: %w", path, err)
}
return t, nil
}
func getNumOfPagesOnFrontpage(posts []*Post, numPosts int) int {
if len(posts) < numPosts {
return len(posts)
}
return numPosts
}
// buildCanonicalLink builds a URL by taking the baseURL and appending
// the path, where the first two segments of the path are dropped.
func buildCanonicalLink(baseURL, path string) string {
parts := S.Split(path, "/")
if len(parts) > 2 {
retstr := fmt.Sprintf("%s/%s/index.html", baseURL, S.Join(parts[2:], "/"))
fmt.Printf("##>> bldCanon: baseURL<%s> path <%s> => Canon<%s> \n",
baseURL, path, retstr)
return retstr
}
return "/"
}