-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathrender.go
executable file
·246 lines (229 loc) · 5.89 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
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
package main
import (
"bytes"
"encoding/json"
"html/template"
"os"
"path/filepath"
"strconv"
"time"
"github.com/gorilla/feeds"
"github.com/snabb/sitemap"
)
type Data interface{}
type RenderArticle struct {
Article
Next *Article
Prev *Article
}
// Compile html template
func CompileTpl(tplPath string, partialTpl string, name string, funcContext FuncContext) template.Template {
// Read template data from file
html, err := os.ReadFile(tplPath)
if err != nil {
Fatal(err.Error())
}
// Append partial template
htmlStr := string(html) + partialTpl
// Generate html content
tpl, err := template.New(name).Funcs(funcContext.FuncMap()).Parse(htmlStr)
if err != nil {
Fatal(err.Error())
}
return *tpl
}
// Render html file by data
func RenderPage(tpl template.Template, tplData interface{}, outPath string) {
// Create file
outFile, err := os.Create(outPath)
if err != nil {
Fatal(err.Error())
}
defer func() {
outFile.Close()
}()
defer wg.Done()
// Template render
err = tpl.Execute(outFile, tplData)
if err != nil {
Fatal(err.Error())
}
}
// Generate all article page
func RenderArticles(tpl template.Template, articles Collections) {
defer wg.Done()
articleCount := len(articles)
for i := range articles {
currentArticle := articles[i].(Article)
var renderArticle = RenderArticle{currentArticle, nil, nil}
// Only show next and prev article if it is not hidden
if !renderArticle.Hide {
if i >= 1 {
// Find prev unhidden article
for j := i - 1; j >= 0; j-- {
prevArticle := articles[j].(Article)
if !prevArticle.Hide {
renderArticle.Prev = &prevArticle
break
}
}
}
if i <= articleCount-2 {
// Find next unhidden article
for j := i + 1; j < articleCount; j++ {
nextArticle := articles[j].(Article)
if !nextArticle.Hide {
renderArticle.Next = &nextArticle
break
}
}
}
}
outPath := filepath.Join(publicPath, currentArticle.Link)
wg.Add(1)
go RenderPage(tpl, renderArticle, outPath)
}
}
// Generate rss page
func GenerateRSS(articles Collections) {
defer wg.Done()
var feedArticles Collections
if len(articles) < globalConfig.Site.Limit {
feedArticles = articles
} else {
feedArticles = articles[0:globalConfig.Site.Limit]
}
if globalConfig.Site.Url != "" {
feed := &feeds.Feed{
Title: globalConfig.Site.Title,
Link: &feeds.Link{Href: globalConfig.Site.Url},
Description: globalConfig.Site.Subtitle,
Author: &feeds.Author{Name: globalConfig.Site.Title, Email: ""},
Created: time.Now(),
}
feed.Items = make([]*feeds.Item, 0)
for _, item := range feedArticles {
article := item.(Article)
feed.Items = append(feed.Items, &feeds.Item{
Title: article.Title,
Link: &feeds.Link{Href: globalConfig.Site.Url + "/" + article.Link},
Description: string(article.Preview),
Author: &feeds.Author{Name: article.Author.Name, Email: ""},
Created: article.Time,
Updated: article.MTime,
})
}
if atom, err := feed.ToAtom(); err == nil {
err := os.WriteFile(filepath.Join(publicPath, "atom.xml"), []byte(atom), 0644)
if err != nil {
Fatal(err.Error())
}
} else {
Fatal(err.Error())
}
}
}
// Generate sitemap page
func GenerateSitemap(articles Collections) {
defer wg.Done()
if globalConfig.Site.Url != "" {
sm := sitemap.New()
globalModTime := time.Now()
sm.Add(&sitemap.URL{
Loc: globalConfig.Site.Url,
LastMod: &globalModTime,
ChangeFreq: sitemap.Weekly,
})
for _, item := range articles {
article := item.(Article)
var lastModTime time.Time
if article.MTime.After(article.Time) {
lastModTime = article.MTime
} else {
lastModTime = article.Time
}
sm.Add(&sitemap.URL{
Loc: globalConfig.Site.Url + "/" + article.Link,
LastMod: &lastModTime,
ChangeFreq: sitemap.Weekly,
})
}
var sitemap bytes.Buffer
sm.WriteTo(&sitemap)
err := os.WriteFile(filepath.Join(publicPath, "sitemap.xml"), sitemap.Bytes(), 0644)
if err != nil {
Fatal(err.Error())
}
}
}
// Generate article list page
func RenderArticleList(rootPath string, articles Collections, tagName string) {
defer wg.Done()
// Create path
pagePath := filepath.Join(publicPath, rootPath)
os.MkdirAll(pagePath, 0777)
// Split page
limit := globalConfig.Site.Limit
total := len(articles)
page := total / limit
rest := total % limit
if rest != 0 {
page++
}
if total < limit {
page = 1
}
for i := 0; i < page; i++ {
var prev = filepath.Join(rootPath, "page"+strconv.Itoa(i)+".html")
var next = filepath.Join(rootPath, "page"+strconv.Itoa(i+2)+".html")
outPath := filepath.Join(pagePath, "index.html")
if i != 0 {
fileName := "page" + strconv.Itoa(i+1) + ".html"
outPath = filepath.Join(pagePath, fileName)
} else {
prev = ""
}
if i == 1 {
prev = filepath.Join(rootPath, "index.html")
}
first := i * limit
count := first + limit
if i == page-1 {
if rest != 0 {
count = first + rest
}
next = ""
}
var data = map[string]interface{}{
"Articles": articles[first:count],
"Site": globalConfig.Site,
"Develop": globalConfig.Develop,
"Page": i + 1,
"Total": page,
"Prev": template.URL(filepath.ToSlash(prev)),
"Next": template.URL(filepath.ToSlash(next)),
"TagName": tagName,
"TagCount": len(articles),
}
wg.Add(1)
go RenderPage(pageTpl, data, outPath)
}
}
// Generate article list JSON
func GenerateJSON(articles Collections) {
defer wg.Done()
datas := make([]map[string]interface{}, 0)
for i := range articles {
article := articles[i].(Article)
var data = map[string]interface{}{
"title": article.Title,
"content": article.Markdown,
"preview": string(article.Preview),
"link": article.Link,
"cover": article.Cover,
}
datas = append(datas, data)
}
str, _ := json.Marshal(datas)
os.WriteFile(filepath.Join(publicPath, "index.json"), []byte(str), 0644)
}