-
Notifications
You must be signed in to change notification settings - Fork 0
/
blogs.go
60 lines (52 loc) · 1.34 KB
/
blogs.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
package main
import (
"cmp"
"fmt"
"slices"
)
type BlogPosts []BlogPost
func (blogs BlogPosts) SortDescending() {
slices.SortFunc(blogs, func(a, b BlogPost) int {
return cmp.Compare(b.DatePosted(), a.DatePosted())
})
}
func (blogs *BlogPosts) Append(blog BlogPost) {
// https://stackoverflow.com/a/24726368/446681
*blogs = append(*blogs, blog)
}
func (blogs BlogPosts) Content() string {
content := "# Blog Posts\r\n"
blogs.SortDescending()
year := 0
for _, blog := range blogs {
if blog.YearPosted() != year {
content += fmt.Sprintf("## %d\r\n", blog.YearPosted())
year = blog.YearPosted()
}
content += "* " + blog.LinkMarkdown() + "\r\n"
}
return content
}
func (blogs BlogPosts) CreateHomepage(layout string, filename string) {
if len(blogs) == 0 {
return
}
fmt.Printf("Creating blog homepage: %s\r\n", filename)
html := md2Html(layout, blogs.Content())
saveFile(filename, html)
}
func (blogs BlogPosts) CreateRssPage(meta SiteMeta, filename string) {
if len(blogs) == 0 {
return
}
fmt.Printf("Creating blog RSS: %s\r\n", filename)
rss := NewRss(meta.Title, meta.Description, meta.BaseLink)
for _, blog := range blogs {
rss.Add(blog.Title(), blog.Summary(), blog.LinkUrl(), blog.DatePosted())
}
xml, err := rss.ToXml()
if err != nil {
fmt.Printf("ERROR producing RSS file: %s\r\n", err)
}
saveFile(filename, xml)
}