From 006c0028106425abb3f718b2e86349dee5b7a2ea Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Sun, 26 May 2024 21:43:49 +0200 Subject: Refactor how preprocess functions work a bit --- src/gmi/posts_preprocess_funcs.go | 26 ++++++++++++++++++ src/gmi/tpl.go | 24 ++++------------- src/http/http.go | 9 ++++--- src/http/posts.go | 57 ++++++++++++++++++++------------------- src/http/tpl.go | 2 +- src/post/preprocess.go | 9 +++---- src/render/methods.go | 9 ++----- 7 files changed, 72 insertions(+), 64 deletions(-) create mode 100644 src/gmi/posts_preprocess_funcs.go (limited to 'src') diff --git a/src/gmi/posts_preprocess_funcs.go b/src/gmi/posts_preprocess_funcs.go new file mode 100644 index 0000000..f8fcda6 --- /dev/null +++ b/src/gmi/posts_preprocess_funcs.go @@ -0,0 +1,26 @@ +package gmi + +import ( + "fmt" + + "dev.mediocregopher.com/mediocre-blog.git/src/render" +) + +type postPreprocessFuncs struct { + urlBuilder render.URLBuilder +} + +func (f postPreprocessFuncs) Image(args ...string) (string, error) { + var ( + id = args[0] + descr = "Image" + ) + + if len(args) > 1 { + descr = args[1] + } + + return fmt.Sprintf( + "\n=> %s %s", f.urlBuilder.Asset(id), descr, + ), nil +} diff --git a/src/gmi/tpl.go b/src/gmi/tpl.go index fb62a17..5ea114d 100644 --- a/src/gmi/tpl.go +++ b/src/gmi/tpl.go @@ -34,24 +34,10 @@ var tplFS embed.FS func (a *api) tplHandler() (gemini.Handler, error) { - preprocessFuncs := post.PreprocessFunctions{ - Image: func(args ...string) (string, error) { - var ( - id = args[0] - descr = "Image" - ) - - if len(args) > 1 { - descr = args[1] - } - - return fmt.Sprintf( - "\n=> %s %s", a.urlBuilder.Asset(id), descr, - ), nil - }, - } - - allTpls := template.New("") + var ( + postPreprocessFuncs = postPreprocessFuncs{a.urlBuilder} + allTpls = template.New("") + ) err := fs.WalkDir(tplFS, "tpl", func(path string, d fs.DirEntry, err error) error { @@ -122,7 +108,7 @@ func (a *api) tplHandler() (gemini.Handler, error) { a.params.PostStore, nil, // asset.Store, not supported by gemini endpoint nil, // post.DraftStore, not supported by gemini endpoint - preprocessFuncs, + postPreprocessFuncs, )) if errors.Is(err, post.ErrPostNotFound) { diff --git a/src/http/http.go b/src/http/http.go index 6e57a77..4403a69 100644 --- a/src/http/http.go +++ b/src/http/http.go @@ -126,9 +126,10 @@ type api struct { params Params srv *http.Server - redirectTpl *template.Template - auther Auther - urlBuilder render.URLBuilder + redirectTpl *template.Template + auther Auther + urlBuilder render.URLBuilder + postPreprocessFuncs postPreprocessFuncs } // New initializes and returns a new API instance, including setting up all @@ -156,12 +157,12 @@ func New(params Params) (API, error) { ), } + a.postPreprocessFuncs = newPostPreprocessFuncs(a.urlBuilder) a.redirectTpl = mustParseTpl(template.New(""), "redirect.html") a.srv = &http.Server{Handler: a.handler()} go func() { - err := a.srv.Serve(l) if err != nil && !errors.Is(err, http.ErrServerClosed) { ctx := mctx.WithAnnotator(context.Background(), &a.params) diff --git a/src/http/posts.go b/src/http/posts.go index 8488996..1bc65c8 100644 --- a/src/http/posts.go +++ b/src/http/posts.go @@ -5,10 +5,10 @@ import ( "context" "errors" "fmt" + "html/template" "net/http" "path/filepath" "strings" - txttpl "text/template" "time" "dev.mediocregopher.com/mediocre-blog.git/src/http/apiutil" @@ -18,7 +18,18 @@ import ( "dev.mediocregopher.com/mediocre-go-lib.git/mctx" ) -func (a *api) postPreprocessFuncImage(args ...string) (string, error) { +type postPreprocessFuncs struct { + urlBuilder render.URLBuilder + imageTpl *template.Template +} + +func newPostPreprocessFuncs(urlBuilder render.URLBuilder) postPreprocessFuncs { + imageTpl := template.New("image.html") + imageTpl = template.Must(imageTpl.Parse(mustReadTplFile("image.html"))) + return postPreprocessFuncs{urlBuilder, imageTpl} +} + +func (f postPreprocessFuncs) Image(args ...string) (string, error) { var ( id = args[0] @@ -29,39 +40,29 @@ func (a *api) postPreprocessFuncImage(args ...string) (string, error) { descr = args[1] } - tpl := txttpl.New("image.html") - tpl = txttpl.Must(tpl.Parse(mustReadTplFile("image.html"))) - - tplPayload := struct { - RootURL render.URLBuilder - ID string - Descr string - Resizable bool - }{ - RootURL: render.NewURLBuilder( - a.params.PublicURL, - a.params.PublicURL, // httpURL - a.params.GeminiPublicURL, - ), - ID: id, - Descr: descr, - Resizable: asset.IsImageResizable(id), - } + var ( + tplPayload = struct { + RootURL render.URLBuilder + ID string + Descr string + Resizable bool + }{ + RootURL: f.urlBuilder, + ID: id, + Descr: descr, + Resizable: asset.IsImageResizable(id), + } + buf = new(bytes.Buffer) + ) - buf := new(bytes.Buffer) - if err := tpl.ExecuteTemplate(buf, "image.html", tplPayload); err != nil { + err := f.imageTpl.ExecuteTemplate(buf, "image.html", tplPayload) + if err != nil { return "", err } return buf.String(), nil } -func (a *api) postPreprocessFuncs() post.PreprocessFunctions { - return post.PreprocessFunctions{ - Image: a.postPreprocessFuncImage, - } -} - func (a *api) getPostsHandler() http.Handler { var ( tpl = a.mustParseBasedTpl("posts.html") diff --git a/src/http/tpl.go b/src/http/tpl.go index 47cea80..42341a2 100644 --- a/src/http/tpl.go +++ b/src/http/tpl.go @@ -66,7 +66,7 @@ func (a *api) newTPLData(r *http.Request, payload interface{}) tplData { a.params.PostStore, a.params.PostAssetStore, a.params.PostDraftStore, - a.postPreprocessFuncs(), + a.postPreprocessFuncs, ), Payload: payload, Title: "mediocregopher's lil web corner", diff --git a/src/post/preprocess.go b/src/post/preprocess.go index 7aceee2..2f3e091 100644 --- a/src/post/preprocess.go +++ b/src/post/preprocess.go @@ -1,14 +1,13 @@ package post // 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 { +// interleave dynamic content into their bodies. +type PreprocessFunctions interface { // Image returns a string which should be inlined into the post body in - // order to display an. + // order to render an image. // // 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) + Image(args ...string) (string, error) } diff --git a/src/render/methods.go b/src/render/methods.go index a671f7e..a28f6b5 100644 --- a/src/render/methods.go +++ b/src/render/methods.go @@ -194,11 +194,7 @@ func (m *Methods) GetPostSeriesNextPrevious( type preprocessPostPayload struct { RootURL URLBuilder - image func(args ...string) (string, error) -} - -func (p preprocessPostPayload) Image(args ...string) (string, error) { - return p.image(args...) + post.PreprocessFunctions } // preprocessPostBody interprets the Post's Body as a text template which may @@ -213,8 +209,7 @@ func (m *Methods) preprocessPostBody(into io.Writer, p post.Post) error { } err = tpl.Execute(into, preprocessPostPayload{ - RootURL: m.RootURL(), - image: m.preprocessFuncs.Image, + m.RootURL(), m.preprocessFuncs, }) if err != nil { return fmt.Errorf("executing post body as template: %w", err) -- cgit v1.2.3