From 293655452cfc6a106c55384e839f9c07d340b954 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Sat, 21 Jan 2023 16:46:11 +0100 Subject: [PATCH] Continue to polish up posts pages --- src/gmi/gmi.go | 20 ++++++++++++++++- src/gmi/tpl.go | 44 +++++++++++++++++++++++++++++++++---- src/gmi/tpl/index.gmi | 4 ++-- src/gmi/tpl/posts/index.gmi | 11 ++++++---- src/gmi/tpl/posts/post.gmi | 24 ++++++++++++++++++++ 5 files changed, 92 insertions(+), 11 deletions(-) diff --git a/src/gmi/gmi.go b/src/gmi/gmi.go index 7d4d186..b543e05 100644 --- a/src/gmi/gmi.go +++ b/src/gmi/gmi.go @@ -119,6 +119,21 @@ func (a *api) Shutdown(ctx context.Context) error { return a.srv.Shutdown(ctx) } +func indexMiddleware(h gemini.Handler) gemini.Handler { + + return gemini.HandlerFunc(func( + ctx context.Context, + rw gemini.ResponseWriter, + r *gemini.Request, + ) { + if strings.HasSuffix(r.URL.Path, "/") { + r.URL.Path += "index.gmi" + } + + h.ServeGemini(ctx, rw, r) + }) +} + func postsMiddleware(tplHandler gemini.Handler) gemini.Handler { return gemini.HandlerFunc(func( @@ -156,7 +171,10 @@ func (a *api) handler() (gemini.Handler, error) { mux.Handle("/posts/", postsMiddleware(tplHandler)) mux.Handle("/", tplHandler) - h := mux + var h gemini.Handler + + h = mux + h = indexMiddleware(h) // TODO logging // TODO caching diff --git a/src/gmi/tpl.go b/src/gmi/tpl.go index 7aa1a2f..7959b8e 100644 --- a/src/gmi/tpl.go +++ b/src/gmi/tpl.go @@ -25,6 +25,11 @@ type rendererGetPostsRes struct { HasMore bool } +type rendererGetPostSeriesNextPreviousRes struct { + Next *post.StoredPost + Previous *post.StoredPost +} + type renderer struct { url *url.URL postStore post.Store @@ -43,6 +48,41 @@ func (r renderer) GetPostByID(id string) (post.StoredPost, error) { return p, nil } +func (r renderer) GetPostSeriesNextPrevious(p post.StoredPost) (rendererGetPostSeriesNextPreviousRes, error) { + + seriesPosts, err := r.postStore.GetBySeries(p.Series) + if err != nil { + return rendererGetPostSeriesNextPreviousRes{}, fmt.Errorf( + "fetching posts for series %q: %w", p.Series, err, + ) + } + + var ( + res rendererGetPostSeriesNextPreviousRes + foundThis bool + ) + + for i := range seriesPosts { + + seriesPost := seriesPosts[i] + + if seriesPost.ID == p.ID { + foundThis = true + continue + } + + if !foundThis { + res.Next = &seriesPost + continue + } + + res.Previous = &seriesPost + break + } + + return res, nil +} + func (r renderer) GetQueryValue(key, def string) string { v := r.url.Query().Get(key) if v == "" { @@ -97,10 +137,6 @@ func (a *api) tplHandler() (gemini.Handler, error) { r *gemini.Request, ) { - if strings.HasSuffix(r.URL.Path, "/") { - r.URL.Path += "index.gmi" - } - tplPath := strings.TrimPrefix(r.URL.Path, "/") ctx = mctx.Annotate(ctx, diff --git a/src/gmi/tpl/index.gmi b/src/gmi/tpl/index.gmi index 7c68b92..9f12e0d 100644 --- a/src/gmi/tpl/index.gmi +++ b/src/gmi/tpl/index.gmi @@ -1,3 +1,3 @@ -# Index +# mediocregopher's lil web corner -=> /posts/index.gmi See all posts +=> /posts/ See all posts diff --git a/src/gmi/tpl/posts/index.gmi b/src/gmi/tpl/posts/index.gmi index 12eb39c..dd4c84c 100644 --- a/src/gmi/tpl/posts/index.gmi +++ b/src/gmi/tpl/posts/index.gmi @@ -1,10 +1,10 @@ # mediocregopher's Posts {{ $page := .GetQueryIntValue "page" 0 -}} -{{ $getPostsRes := .GetPosts $page 20 -}} +{{ $getPostsRes := .GetPosts $page 15 -}} {{ if gt $page 0 -}} -=> /posts.gmi?page={{ .Add $page -1 }} Previous Page +=> /posts/?page={{ .Add $page -1 }} Previous Page {{ end -}} @@ -14,5 +14,8 @@ {{ end -}} {{ if $getPostsRes.HasMore -}} -=> /posts.gmi?page={{ .Add $page 1 }} Next page -{{ end -}} +=> /posts/?page={{ .Add $page 1 }} Next page +{{ end }} +================================================================================ + +=> / Home diff --git a/src/gmi/tpl/posts/post.gmi b/src/gmi/tpl/posts/post.gmi index 52ac5ff..4f58c84 100644 --- a/src/gmi/tpl/posts/post.gmi +++ b/src/gmi/tpl/posts/post.gmi @@ -7,3 +7,27 @@ {{ end -}} {{ $post.Body }} + +-------------------------------------------------------------------------------- + +Published {{ $post.PublishedAt.Format "2006-01-02" }} + +{{- if $post.Series }} + +This post is part of a series! + +{{ $seriesNextPrev := .GetPostSeriesNextPrevious $post -}} + +{{ if $seriesNextPrev.Next -}} +=> /posts/{{ $seriesNextPrev.Next.ID }}.gmi Next: {{ $seriesNextPrev.Next.Title }} +{{ end -}} + +{{ if $seriesNextPrev.Previous -}} +=> /posts/{{ $seriesNextPrev.Previous.ID }}.gmi Previously: {{ $seriesNextPrev.Previous.Title }} +{{ end -}} + +{{ end }} +================================================================================ + +=> /posts/ More posts +=> / Home