From ad117af7a8d4ef89a2dc3bb368fe2ba510d55b4f Mon Sep 17 00:00:00 2001 From: gutmet Date: Sat, 8 Apr 2023 17:10:31 +0200 Subject: [PATCH] blogposts: fill categoryListings to link to related posts automatically --- blog/blog.go | 25 ++++++++++++++++--------- go.mod | 2 +- initer/styles.go | 6 ++++++ initer/templates.go | 7 ++++++- 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/blog/blog.go b/blog/blog.go index f50b07d..b669e7a 100644 --- a/blog/blog.go +++ b/blog/blog.go @@ -72,8 +72,9 @@ func emitPostPage(post Post) string { page.Time = post.Meta.Date.Format(TimeFormat) page.Blogpost = true for _, categoryName := range post.Meta.Categories { - page.Categories = append(page.Categories, Category{categoryName, linkToCategory(categoryName)}) - addToCategory(categoryName, post) + page.Categories = append(page.Categories, Category{Name: categoryName, Path: linkToCategory(categoryName)}) + related := CategoryListing{Name: categoryName, ASCIIName: ConvASCII(categoryName), Posts: categories[categoryName]} + page.CategoryListings = append(page.CategoryListings, related) } buf := new(bytes.Buffer) 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) 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) postCollection := make([]Blogpost, 0) 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.Title = blogname + ": Categories" page.Style = style @@ -243,25 +250,25 @@ func getPosts() []Post { func makeBlog() { posts := getPosts() + makeCategories(posts) + makeTimeline(posts) + makeRSS(posts) sort.Sort(byDate(posts)) total := len(posts) s := make([]Post, 0) n := 0 for i, tmp := range posts { if (i / postsPerPage) != n { - emitIndexPage(n, s, total) + emitIndexAndPostPages(n, s, total) n = i / postsPerPage s = make([]Post, 0) } s = append(s, tmp) } if len(s) != 0 { - emitIndexPage(n, s, total) + emitIndexAndPostPages(n, s, total) } os.Link(indexFilename(n), filepath.Join("stage2", folder, "index.html")) - makeCategories() - makeTimeline(posts) - makeRSS(posts) } func Generate(dir string) { diff --git a/go.mod b/go.mod index 9b692c2..ff9d5da 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module git.gutmet.org/wombat.git go 1.14 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/simpleserver.git v0.0.0-20201011112611-555b83d5de53 github.com/pmezard/go-difflib v1.0.0 // indirect diff --git a/initer/styles.go b/initer/styles.go index c06ae62..4f75a2c 100644 --- a/initer/styles.go +++ b/initer/styles.go @@ -24,9 +24,15 @@ a.linktopost { color: #E8E8E8; text-decoration: none; } +div.categoryListing { + padding-bottom: 150em; +} a.categoryListing { color: #E8E8E8; } +div.relatedListing{ + padding-top: 10em; +} img { display: block; max-width: 100%; diff --git a/initer/templates.go b/initer/templates.go index 05748e2..0fbdfdb 100644 --- a/initer/templates.go +++ b/initer/templates.go @@ -73,8 +73,13 @@ const defaultTemplate string = ` {{.Content}} {{if .CategoryListings}} +{{if .Blogpost}} +
+Related posts: +{{else}}
-
+{{end}} +
{{range $i, $listing := .CategoryListings -}}
{{$listing.Name}}
{{range $j, $post := $listing.Posts -}}