blogposts: fill categoryListings to link to related posts automatically

This commit is contained in:
gutmet 2023-04-08 17:10:31 +02:00
parent 410e7fc496
commit ad117af7a8
4 changed files with 29 additions and 11 deletions

View File

@ -72,8 +72,9 @@ func emitPostPage(post Post) string {
page.Time = post.Meta.Date.Format(TimeFormat) page.Time = post.Meta.Date.Format(TimeFormat)
page.Blogpost = true page.Blogpost = true
for _, categoryName := range post.Meta.Categories { for _, categoryName := range post.Meta.Categories {
page.Categories = append(page.Categories, Category{categoryName, linkToCategory(categoryName)}) page.Categories = append(page.Categories, Category{Name: categoryName, Path: linkToCategory(categoryName)})
addToCategory(categoryName, post) related := CategoryListing{Name: categoryName, ASCIIName: ConvASCII(categoryName), Posts: categories[categoryName]}
page.CategoryListings = append(page.CategoryListings, related)
} }
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
t.Execute(buf, page) t.Execute(buf, page)
@ -96,7 +97,7 @@ func (p byDateDesc) Len() int { return len(p) }
func (p byDateDesc) Swap(i, j int) { p[i], p[j] = p[j], p[i] } func (p byDateDesc) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p byDateDesc) Less(i, j int) bool { return p[i].Meta.Date.After(p[j].Meta.Date) } func (p byDateDesc) Less(i, j int) bool { return p[i].Meta.Date.After(p[j].Meta.Date) }
func emitIndexPage(i int, posts []Post, total int) { func emitIndexAndPostPages(i int, posts []Post, total int) {
file := indexFilename(i) file := indexFilename(i)
postCollection := make([]Blogpost, 0) postCollection := make([]Blogpost, 0)
sort.Sort(byDateDesc(posts)) sort.Sort(byDateDesc(posts))
@ -179,7 +180,13 @@ func getPost(file string) (Post, error) {
} }
} }
func makeCategories() { func makeCategories(posts []Post) {
sort.Sort(byDate(posts))
for _, post := range posts {
for _, category := range post.Meta.Categories {
addToCategory(category, post)
}
}
page := Page{} page := Page{}
page.Title = blogname + ": Categories" page.Title = blogname + ": Categories"
page.Style = style page.Style = style
@ -243,25 +250,25 @@ func getPosts() []Post {
func makeBlog() { func makeBlog() {
posts := getPosts() posts := getPosts()
makeCategories(posts)
makeTimeline(posts)
makeRSS(posts)
sort.Sort(byDate(posts)) sort.Sort(byDate(posts))
total := len(posts) total := len(posts)
s := make([]Post, 0) s := make([]Post, 0)
n := 0 n := 0
for i, tmp := range posts { for i, tmp := range posts {
if (i / postsPerPage) != n { if (i / postsPerPage) != n {
emitIndexPage(n, s, total) emitIndexAndPostPages(n, s, total)
n = i / postsPerPage n = i / postsPerPage
s = make([]Post, 0) s = make([]Post, 0)
} }
s = append(s, tmp) s = append(s, tmp)
} }
if len(s) != 0 { if len(s) != 0 {
emitIndexPage(n, s, total) emitIndexAndPostPages(n, s, total)
} }
os.Link(indexFilename(n), filepath.Join("stage2", folder, "index.html")) os.Link(indexFilename(n), filepath.Join("stage2", folder, "index.html"))
makeCategories()
makeTimeline(posts)
makeRSS(posts)
} }
func Generate(dir string) { func Generate(dir string) {

2
go.mod
View File

@ -3,7 +3,7 @@ module git.gutmet.org/wombat.git
go 1.14 go 1.14
require ( require (
git.gutmet.org/finstr.git v0.0.0-20210628072143-8f552a2440f5 // indirect git.gutmet.org/finstr.git v0.0.0-20210628072143-8f552a2440f5
git.gutmet.org/goutil.git v0.0.0-20201108182825-c19893df11f9 git.gutmet.org/goutil.git v0.0.0-20201108182825-c19893df11f9
git.gutmet.org/simpleserver.git v0.0.0-20201011112611-555b83d5de53 git.gutmet.org/simpleserver.git v0.0.0-20201011112611-555b83d5de53
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect

View File

@ -24,9 +24,15 @@ a.linktopost {
color: #E8E8E8; color: #E8E8E8;
text-decoration: none; text-decoration: none;
} }
div.categoryListing {
padding-bottom: 150em;
}
a.categoryListing { a.categoryListing {
color: #E8E8E8; color: #E8E8E8;
} }
div.relatedListing{
padding-top: 10em;
}
img { img {
display: block; display: block;
max-width: 100%; max-width: 100%;

View File

@ -73,8 +73,13 @@ const defaultTemplate string = `<!DOCTYPE html>
{{.Content}} {{.Content}}
{{if .CategoryListings}} {{if .CategoryListings}}
{{if .Blogpost}}
<div class="relatedListing">
Related posts:
{{else}}
<div class="categoryListing"> <div class="categoryListing">
<dl style="padding-bottom:150em;"> {{end}}
<dl>
{{range $i, $listing := .CategoryListings -}} {{range $i, $listing := .CategoryListings -}}
<dt><a name="{{$listing.ASCIIName}}" class="categoryListing">{{$listing.Name}}</a></dt> <dt><a name="{{$listing.ASCIIName}}" class="categoryListing">{{$listing.Name}}</a></dt>
{{range $j, $post := $listing.Posts -}} {{range $j, $post := $listing.Posts -}}