-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessor.go
407 lines (369 loc) · 14.3 KB
/
processor.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"slices"
"sort"
"strconv"
"strings"
"text/template"
)
func process(pages []page, posts []post,
resLoader resourceLoader, handleOutput processorOutputHandler) stats {
var searchIndex = mapSlice{}
pageCnt := processPages(pages, &searchIndex, resLoader, handleOutput)
postCnt, tagCnt := processPosts(posts, &searchIndex, resLoader, handleOutput)
config := resLoader.config
if config.enableSearch {
sprintln(" - generating search files ...")
searchIndexJson, err := json.Marshal(searchIndex)
check(err)
searchIndexOutputFilePath := fmt.Sprintf("%s%c%s", deployDirName, os.PathSeparator, searchIndexFileName)
writeDataToFileIfChanged(searchIndexOutputFilePath, searchIndexJson)
searchTemplate := compileSearchTemplate(resLoader)
outputFilePath := fmt.Sprintf("%s%c%s", deployDirName, os.PathSeparator, searchPageFileName)
var searchContentBuffer bytes.Buffer
err = searchTemplate.Execute(&searchContentBuffer, templateContent{EntityType: Page, Title: config.siteName + " - Search", Config: map[string]any{"PageSize": config.pageSize}})
check(err)
if handleOutput != nil {
handleOutput(outputFilePath, searchContentBuffer.Bytes())
}
}
return stats{
pageCnt: pageCnt,
postCnt: postCnt,
tagCnt: tagCnt,
}
}
func processPages(pages []page, searchIndex *mapSlice,
resLoader resourceLoader, handleOutput processorOutputHandler) int {
if pages != nil {
sprintln(" - processing pages ...")
title := resLoader.config.siteName
homePage := resLoader.config.homePage
if homePage != "" {
found := false
for _, page := range pages {
if homePage == page.Id {
found = true
break
}
}
if !found {
exitWithError(fmt.Sprintf("home page not found: '%s'", homePage))
}
}
for _, page := range pages {
if !page.skipProcessing {
pageTemplate := compilePageTemplate(page, resLoader)
outputFileName := page.Id + contentFileExtension
var outputFilePath string
if homePage == page.Id {
outputFilePath = fmt.Sprintf("%s%c%s", deployDirName, os.PathSeparator, indexPageFileName)
} else {
outputFilePath = fmt.Sprintf("%s%c%s%c%s", deployDirName, os.PathSeparator, deployPageDirName, os.PathSeparator, outputFileName)
}
pTitle := title
if page.Title != "" {
pTitle = title + " - " + page.Title
}
var pageContentBuffer bytes.Buffer
err := pageTemplate.Execute(&pageContentBuffer, templateContent{EntityType: Page, Title: pTitle, FileName: outputFileName, Content: page})
check(err)
if handleOutput != nil {
handleOutput(outputFilePath, pageContentBuffer.Bytes())
}
}
*searchIndex = append(*searchIndex, mapItem{Key: page.SearchData.TypeId, Value: page.SearchData.Content})
}
}
return len(pages)
}
func processPosts(posts []post, searchIndex *mapSlice,
resLoader resourceLoader, handleOutput processorOutputHandler) (int, int) {
tagCnt := 0
if posts != nil {
sprintln(" - processing posts ...")
config := resLoader.config
title := config.siteName
homePage := config.homePage
pageSize := config.pageSize
postContentTemplate := compilePostTemplate(resLoader)
pagerTemplate := compilePagerTemplate(resLoader)
postCnt := len(posts)
totalPageCnt := postCnt / pageSize
if postCnt%pageSize > 0 {
totalPageCnt++
}
ppd := pagerData{
CurrPageNum: 1,
TotalPageCnt: totalPageCnt,
PageUriPrefix: "/" + deployPostsDirName,
IndexPageUri: "/",
}
if homePage != "" {
ppd.IndexPageUri += deployPostsDirName + "/"
}
pagePostCnt := 0
var postPageContent string
var archIdxData archiveIndexData
archivePostCnt := make(map[string]int)
archiveContent := make(map[string][]string)
tagPostCnt := make(map[string]int)
tagTitleCnt := make(map[string]int)
tagContent := make(map[string][]string)
for _, post := range posts {
pagePostCnt++
pTitle := title
if post.Title != "" {
pTitle = title + " - " + post.Title
}
outputFileName := post.Id + contentFileExtension
var postContentBuffer bytes.Buffer
err := postContentTemplate.Execute(&postContentBuffer, templateContent{EntityType: Post, Title: pTitle, Content: post, FileName: outputFileName})
check(err)
postContent := strings.TrimSpace(postContentBuffer.String())
postPageContent += postContent
if !post.skipProcessing {
var singlePostContentBuffer bytes.Buffer
err = postContentTemplate.Execute(&singlePostContentBuffer, templateContent{EntityType: Post, Title: pTitle, Content: post})
check(err)
fullTemplate := compileFullTemplate(outputFileName, singlePostContentBuffer.String(), nil, resLoader)
var singlePostFullContentBuffer bytes.Buffer
err = fullTemplate.Execute(&singlePostFullContentBuffer, templateContent{EntityType: Post, Title: pTitle, Content: post})
check(err)
outputFilePath := fmt.Sprintf("%s%c%s%c%s", deployDirName, os.PathSeparator, deployPostDirName, os.PathSeparator, outputFileName)
if handleOutput != nil {
handleOutput(outputFilePath, singlePostFullContentBuffer.Bytes())
}
}
if pagePostCnt == pageSize {
var pagerBuffer bytes.Buffer
err = pagerTemplate.Execute(&pagerBuffer, ppd)
check(err)
postPageContent += pagerBuffer.String()
if ppd.CurrPageNum == 1 {
if homePage == "" {
outputFilePath := fmt.Sprintf("%s%c%s", deployDirName, os.PathSeparator, indexPageFileName)
processContent(indexPageFileName, Post, config.siteName, postPageContent, outputFilePath, resLoader, handleOutput)
}
outputFilePath := fmt.Sprintf("%s%c%s%c%s", deployDirName, os.PathSeparator, deployPostsDirName, os.PathSeparator, indexPageFileName)
processContent(deployPostsDirName+"/"+indexPageFileName, Post, config.siteName, postPageContent, outputFilePath, resLoader, handleOutput)
} else {
fileName := strconv.Itoa(ppd.CurrPageNum) + contentFileExtension
outputFilePath := fmt.Sprintf("%s%c%s%c%s", deployDirName, os.PathSeparator, deployPostsDirName, os.PathSeparator, fileName)
processContent(fileName, Post, config.siteName, postPageContent, outputFilePath, resLoader, handleOutput)
}
pagePostCnt = 0
postPageContent = ""
ppd.CurrPageNum++
}
if config.generateArchive && !post.Date.IsZero() {
year := post.Date.Year
month := post.Date.Month
yearIdx := slices.IndexFunc(archIdxData.YearData, func(data archiveYearData) bool {
return data.Year == year
})
if yearIdx != -1 {
monthData := archIdxData.YearData[yearIdx].MonthData
monthIdx := slices.IndexFunc(monthData, func(data archiveMonthData) bool {
return data.Month == month
})
if monthIdx != -1 {
archIdxData.YearData[yearIdx].MonthData[monthIdx].PostCnt++
} else {
archIdxData.YearData[yearIdx].MonthData = append(archIdxData.YearData[yearIdx].MonthData,
archiveMonthData{
Month: month,
PostCnt: 1,
})
}
} else {
archIdxData.YearData = append(archIdxData.YearData, archiveYearData{
Year: year,
MonthData: []archiveMonthData{
{
Month: month,
PostCnt: 1,
},
},
})
}
postYearAndMonth := formatYearAndMonth(year, month)
archivePostCnt[postYearAndMonth]++
archiveContent[postYearAndMonth] = append(archiveContent[postYearAndMonth], postContent)
}
if len(post.Tags) > 0 {
for _, tag := range post.Tags {
tagTitleCnt[tag]++
t := strings.ToLower(tag)
tagPostCnt[t]++
tagContent[t] = append(tagContent[t], postContent)
}
}
*searchIndex = append(*searchIndex, mapItem{Key: post.SearchData.TypeId, Value: post.SearchData.Content})
}
if pagePostCnt > 0 {
if ppd.CurrPageNum == 1 {
if homePage == "" {
outputFilePath := fmt.Sprintf("%s%c%s", deployDirName, os.PathSeparator, indexPageFileName)
processContent(indexPageFileName, Post, config.siteName, postPageContent, outputFilePath, resLoader, handleOutput)
}
outputFilePath := fmt.Sprintf("%s%c%s%c%s", deployDirName, os.PathSeparator, deployPostsDirName, os.PathSeparator, indexPageFileName)
processContent(deployPostsDirName+"/"+indexPageFileName, Post, config.siteName, postPageContent, outputFilePath, resLoader, handleOutput)
} else {
fileName := strconv.Itoa(ppd.CurrPageNum) + contentFileExtension
outputFilePath := fmt.Sprintf("%s%c%s%c%s", deployDirName, os.PathSeparator, deployPostsDirName, os.PathSeparator, fileName)
var pagerBuffer bytes.Buffer
err := pagerTemplate.Execute(&pagerBuffer, ppd)
check(err)
postPageContent += pagerBuffer.String()
processContent(fileName, Post, config.siteName, postPageContent, outputFilePath, resLoader, handleOutput)
}
}
if config.generateArchive && len(archivePostCnt) > 0 {
sprintln(" - generating archive ...")
archiveTemplate := compileArchiveTemplate(resLoader)
outputFilePath := fmt.Sprintf("%s%c%s%c%s", deployDirName, os.PathSeparator, deployArchiveDirName, os.PathSeparator, indexPageFileName)
var archiveContentBuffer bytes.Buffer
err := archiveTemplate.Execute(&archiveContentBuffer, templateContent{EntityType: Page, Title: config.siteName + " - Archive", Content: archIdxData})
check(err)
if handleOutput != nil {
handleOutput(outputFilePath, archiveContentBuffer.Bytes())
}
processPaginatedPostContent(archivePostCnt, archiveContent, pageSize, deployArchiveDirName, pagerTemplate, resLoader, handleOutput)
}
tagPostCntLen := len(tagPostCnt)
if tagPostCntLen > 0 {
if config.generateTagIndex {
minTagPostCnt := -1
maxTagPostCnt := 0
for _, v := range tagPostCnt {
if minTagPostCnt == -1 || v < minTagPostCnt {
minTagPostCnt = v
}
if v > maxTagPostCnt {
maxTagPostCnt = v
}
}
// ======================================================================
// sort the tags by post count
// ======================================================================
var sortedTags []tagData
for tt, tc := range tagTitleCnt {
tr := float64(tc-minTagPostCnt) / float64(maxTagPostCnt-minTagPostCnt)
tr = float64(int(tr*100))/100 + 1
sortedTags = append(sortedTags, tagData{Title: tt, Count: tc, Ratio: tr})
}
sort.Slice(sortedTags, func(i, j int) bool {
return sortedTags[i].Count > sortedTags[j].Count
})
// ======================================================================
sprintln(" - generating tag index ...")
tagIndexTemplate := compileTagIndexTemplate(resLoader)
outputFilePath := fmt.Sprintf("%s%c%s%c%s", deployDirName, os.PathSeparator, deployTagsDirName, os.PathSeparator, indexPageFileName)
var tagIndexContentBuffer bytes.Buffer
err := tagIndexTemplate.Execute(&tagIndexContentBuffer, templateContent{EntityType: Page, Title: config.siteName + " - Tag Index", Content: sortedTags})
check(err)
if handleOutput != nil {
handleOutput(outputFilePath, tagIndexContentBuffer.Bytes())
}
}
sprintln(" - generating tag pages ...")
tagCnt = tagPostCntLen
processPaginatedPostContent(tagPostCnt, tagContent, pageSize, deployTagsDirName, pagerTemplate, resLoader, handleOutput)
}
}
return len(posts), tagCnt
}
func processPaginatedPostContent(postCnt map[string]int, content map[string][]string, pageSize int,
contentDeployDirName string, pagerTemplate *template.Template,
resLoader resourceLoader, handleOutput processorOutputHandler) {
postCntLen := len(postCnt)
if postCntLen > 0 {
for key, cnt := range postCnt {
totalPageCnt := cnt / pageSize
if cnt%pageSize > 0 {
totalPageCnt++
}
subDirName := strings.ToLower(key)
pdIndexUri := "/" + contentDeployDirName + "/" + subDirName
pd := pagerData{
CurrPageNum: 1,
TotalPageCnt: totalPageCnt,
PageUriPrefix: pdIndexUri,
IndexPageUri: pdIndexUri,
}
pagePostCnt := 0
pageContent := ""
for i := 0; i < cnt; i++ {
pagePostCnt++
pageContent += content[key][i]
if pagePostCnt == pageSize {
var pagerBuffer bytes.Buffer
err := pagerTemplate.Execute(&pagerBuffer, pd)
check(err)
pageContent += pagerBuffer.String()
var fileName string
if pd.CurrPageNum == 1 {
fileName = indexPageFileName
} else {
fileName = strconv.Itoa(pd.CurrPageNum) + contentFileExtension
}
outputFilePath := fmt.Sprintf("%s%c%s%c%s%c%s", deployDirName, os.PathSeparator, contentDeployDirName, os.PathSeparator, subDirName, os.PathSeparator, fileName)
processContent(fileName, Post, resLoader.config.siteName+" - "+key, pageContent, outputFilePath, resLoader, handleOutput)
pagePostCnt = 0
pageContent = ""
pd.CurrPageNum++
}
}
if pagePostCnt > 0 {
var fileName string
if pd.CurrPageNum == 1 {
fileName = indexPageFileName
} else {
fileName = strconv.Itoa(pd.CurrPageNum) + contentFileExtension
var pagerBuffer bytes.Buffer
err := pagerTemplate.Execute(&pagerBuffer, pd)
check(err)
pageContent += pagerBuffer.String()
}
outputFilePath := fmt.Sprintf("%s%c%s%c%s%c%s", deployDirName, os.PathSeparator, contentDeployDirName, os.PathSeparator, subDirName, os.PathSeparator, fileName)
processContent(fileName, Post, resLoader.config.siteName+" - "+key, pageContent, outputFilePath, resLoader, handleOutput)
}
}
}
}
func processContent(templateName string, ceType contentEntityType, title string, content string, outputFilePath string, resLoader resourceLoader, handleOutput processorOutputHandler) {
tmplt := compileFullTemplate(templateName, content, nil, resLoader)
var contentBuffer bytes.Buffer
err := tmplt.Execute(&contentBuffer, templateContent{EntityType: ceType, Title: title})
check(err)
if handleOutput != nil {
handleOutput(outputFilePath, contentBuffer.Bytes())
}
}
func processAndHandleStats(config appConfig, resLoader resourceLoader, useCache bool) {
generatedCnt := 0
stats := process(
parsePages(config, resLoader, processImgThumbnails, useCache),
parsePosts(config, resLoader, processImgThumbnails, useCache),
resLoader,
func(outputFilePath string, data []byte) bool {
idx := strings.LastIndex(outputFilePath, string(os.PathSeparator))
if idx != -1 {
outputDir := outputFilePath[:idx]
createDirIfNotExists(outputDir)
}
generated := writeDataToFileIfChanged(outputFilePath, data)
if generated {
println(" - generated file: " + outputFilePath)
generatedCnt++
}
return generated
})
stats.genCnt = generatedCnt
handleStats(stats)
}