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 +++++------------------- 2 files changed, 31 insertions(+), 19 deletions(-) create mode 100644 src/gmi/posts_preprocess_funcs.go (limited to 'src/gmi') 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) { -- cgit v1.2.3