Compare commits
2 Commits
7878db5c95
...
ffdd9520b9
Author | SHA1 | Date | |
---|---|---|---|
|
ffdd9520b9 | ||
|
293655452c |
@ -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
|
||||
|
@ -8,6 +8,7 @@ import (
|
||||
"io"
|
||||
"io/fs"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/template"
|
||||
@ -25,6 +26,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 +49,80 @@ 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) PostBody(p post.StoredPost) (string, error) {
|
||||
|
||||
preprocessFuncs := post.PreprocessFunctions{
|
||||
BlogURL: func(path string) string {
|
||||
return filepath.Join("/", path)
|
||||
},
|
||||
AssetURL: func(id string) string {
|
||||
return filepath.Join("/assets", id)
|
||||
},
|
||||
PostURL: func(id string) string {
|
||||
return filepath.Join("/posts", id)
|
||||
},
|
||||
StaticURL: func(path string) string {
|
||||
return filepath.Join("/static", path)
|
||||
},
|
||||
Image: func(args ...string) (string, error) {
|
||||
|
||||
var (
|
||||
id = args[0]
|
||||
descr = "Image"
|
||||
)
|
||||
|
||||
if len(args) > 1 {
|
||||
descr = args[1]
|
||||
}
|
||||
|
||||
return fmt.Sprintf("=> %s %s", filepath.Join("/assets", id), descr), nil
|
||||
},
|
||||
}
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
|
||||
if err := p.PreprocessBody(buf, preprocessFuncs); err != nil {
|
||||
return "", fmt.Errorf("preprocessing post body: %w", err)
|
||||
}
|
||||
|
||||
return buf.String(), nil
|
||||
}
|
||||
|
||||
func (r renderer) GetQueryValue(key, def string) string {
|
||||
v := r.url.Query().Get(key)
|
||||
if v == "" {
|
||||
@ -97,10 +177,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,
|
||||
|
@ -1,3 +1,3 @@
|
||||
# Index
|
||||
# mediocregopher's lil web corner
|
||||
|
||||
=> /posts/index.gmi See all posts
|
||||
=> /posts/ See all posts
|
||||
|
@ -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
|
||||
|
@ -6,4 +6,28 @@
|
||||
> {{ $post.Description }}
|
||||
{{ end -}}
|
||||
|
||||
{{ $post.Body }}
|
||||
{{ .PostBody $post }}
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
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
|
||||
|
@ -21,68 +21,43 @@ import (
|
||||
"github.com/mediocregopher/mediocre-go-lib/v2/mctx"
|
||||
)
|
||||
|
||||
func (a *api) parsePostBody(p post.Post) (*txttpl.Template, error) {
|
||||
tpl := txttpl.New("root")
|
||||
tpl = tpl.Funcs(txttpl.FuncMap(a.tplFuncs()))
|
||||
func (a *api) postPreprocessFuncImage(args ...string) (string, error) {
|
||||
|
||||
tpl = txttpl.Must(tpl.New("image.html").Parse(mustReadTplFile("image.html")))
|
||||
var (
|
||||
id = args[0]
|
||||
descr = "TODO"
|
||||
)
|
||||
|
||||
if p.Format == post.FormatMarkdown {
|
||||
tpl = tpl.Funcs(txttpl.FuncMap{
|
||||
"Image": func(id string) (string, error) {
|
||||
|
||||
tplPayload := struct {
|
||||
ID string
|
||||
Descr string
|
||||
Resizable bool
|
||||
}{
|
||||
ID: id,
|
||||
// I could use variadic args to make this work, I think
|
||||
Descr: "TODO: proper alt text",
|
||||
Resizable: isImgResizable(id),
|
||||
}
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
if err := tpl.ExecuteTemplate(buf, "image.html", tplPayload); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return buf.String(), nil
|
||||
},
|
||||
})
|
||||
if len(args) > 1 {
|
||||
descr = args[1]
|
||||
}
|
||||
|
||||
if p.Format == post.FormatGemtext {
|
||||
tpl = tpl.Funcs(txttpl.FuncMap{
|
||||
"Image": func(id, descr string) (string, error) {
|
||||
tpl := txttpl.New("image.html")
|
||||
|
||||
tplPayload := struct {
|
||||
ID string
|
||||
Descr string
|
||||
Resizable bool
|
||||
}{
|
||||
ID: id,
|
||||
Descr: descr,
|
||||
Resizable: isImgResizable(id),
|
||||
}
|
||||
tpl.Funcs(txttpl.FuncMap{
|
||||
"AssetURL": func(id string) string {
|
||||
return a.assetURL(id, false)
|
||||
},
|
||||
})
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
if err := tpl.ExecuteTemplate(buf, "image.html", tplPayload); err != nil {
|
||||
return "", err
|
||||
}
|
||||
tpl = txttpl.Must(tpl.Parse(mustReadTplFile("image.html")))
|
||||
|
||||
return buf.String(), nil
|
||||
},
|
||||
})
|
||||
tplPayload := struct {
|
||||
ID string
|
||||
Descr string
|
||||
Resizable bool
|
||||
}{
|
||||
ID: id,
|
||||
Descr: descr,
|
||||
Resizable: isImgResizable(id),
|
||||
}
|
||||
|
||||
tpl, err := tpl.New(p.ID + "-body.html").Parse(p.Body)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
buf := new(bytes.Buffer)
|
||||
if err := tpl.ExecuteTemplate(buf, "image.html", tplPayload); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return tpl, nil
|
||||
return buf.String(), nil
|
||||
}
|
||||
|
||||
type postTplPayload struct {
|
||||
@ -93,15 +68,27 @@ type postTplPayload struct {
|
||||
|
||||
func (a *api) postToPostTplPayload(storedPost post.StoredPost) (postTplPayload, error) {
|
||||
|
||||
bodyTpl, err := a.parsePostBody(storedPost.Post)
|
||||
if err != nil {
|
||||
return postTplPayload{}, fmt.Errorf("parsing post body as template: %w", err)
|
||||
preprocessFuncs := post.PreprocessFunctions{
|
||||
BlogURL: func(path string) string {
|
||||
return a.blogURL(path, false)
|
||||
},
|
||||
AssetURL: func(id string) string {
|
||||
return a.assetURL(id, false)
|
||||
},
|
||||
PostURL: func(id string) string {
|
||||
return a.postURL(id, false)
|
||||
},
|
||||
StaticURL: func(path string) string {
|
||||
path = filepath.Join("static", path)
|
||||
return a.blogURL(path, false)
|
||||
},
|
||||
Image: a.postPreprocessFuncImage,
|
||||
}
|
||||
|
||||
bodyBuf := new(bytes.Buffer)
|
||||
|
||||
if err := bodyTpl.Execute(bodyBuf, nil); err != nil {
|
||||
return postTplPayload{}, fmt.Errorf("executing post body as template: %w", err)
|
||||
if err := storedPost.PreprocessBody(bodyBuf, preprocessFuncs); err != nil {
|
||||
return postTplPayload{}, fmt.Errorf("preprocessing post body: %w", err)
|
||||
}
|
||||
|
||||
if storedPost.Format == post.FormatGemtext {
|
||||
|
66
src/post/preprocess.go
Normal file
66
src/post/preprocess.go
Normal file
@ -0,0 +1,66 @@
|
||||
package post
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
// PreprocessFunctions are functions which can be used by posts themselves to
|
||||
// interleave dynamic content into their bodies. Usually this is used for
|
||||
// properly constructing URLs, but also for things like displaying images.
|
||||
type PreprocessFunctions struct {
|
||||
|
||||
// BlogURL returns the given string, rooted to the blog's base url (which
|
||||
// may or may not include path components itself).
|
||||
//
|
||||
// The given path should not have a leading slash.
|
||||
BlogURL func(path string) string
|
||||
|
||||
// AssetURL returns the URL of the asset with the given ID.
|
||||
AssetURL func(id string) string
|
||||
|
||||
// PostURL returns the URL of the post with the given ID.
|
||||
PostURL func(id string) string
|
||||
|
||||
// StaticURL returns the URL of a file being served from the static
|
||||
// directory. The given path should _not_ include the prefixed 'static/'
|
||||
// path element.
|
||||
StaticURL func(path string) string
|
||||
|
||||
// Image returns a string which should be inlined into the post body in
|
||||
// order to display an.
|
||||
//
|
||||
// The first argument to Image _must_ be the ID of an image asset. The
|
||||
// second argument _may_ be a description of the image which will be used as
|
||||
// alt text, or possibly displayed to the user with the image.
|
||||
Image func(args ...string) (string, error)
|
||||
}
|
||||
|
||||
// PreprocessBody interprets the Post's Body as a text template which may use
|
||||
// any of the functions found in PreprocessFunctions (all must be set). It
|
||||
// executes the template and writes the result to the given writer.
|
||||
func (p Post) PreprocessBody(into io.Writer, funcs PreprocessFunctions) error {
|
||||
|
||||
tpl := template.New("")
|
||||
|
||||
tpl.Funcs(template.FuncMap{
|
||||
"BlogURL": funcs.BlogURL,
|
||||
"AssetURL": funcs.AssetURL,
|
||||
"PostURL": funcs.PostURL,
|
||||
"StaticURL": funcs.StaticURL,
|
||||
"Image": funcs.Image,
|
||||
})
|
||||
|
||||
tpl, err := tpl.Parse(p.Body)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("parsing post body as template: %w", err)
|
||||
}
|
||||
|
||||
if err := tpl.Execute(into, nil); err != nil {
|
||||
return fmt.Errorf("executing post body as template: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue
Block a user