From ffdd9520b9803e141582ba647050682659075760 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Sat, 21 Jan 2023 17:37:22 +0100 Subject: Implement preprocessing of post bodies for gemini --- src/gmi/tpl.go | 40 ++++++++++++++++++++++++++++++++++++++++ src/gmi/tpl/posts/post.gmi | 2 +- 2 files changed, 41 insertions(+), 1 deletion(-) (limited to 'src/gmi') diff --git a/src/gmi/tpl.go b/src/gmi/tpl.go index 7959b8e..7f41993 100644 --- a/src/gmi/tpl.go +++ b/src/gmi/tpl.go @@ -8,6 +8,7 @@ import ( "io" "io/fs" "net/url" + "path/filepath" "strconv" "strings" "text/template" @@ -83,6 +84,45 @@ func (r renderer) GetPostSeriesNextPrevious(p post.StoredPost) (rendererGetPostS 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 == "" { diff --git a/src/gmi/tpl/posts/post.gmi b/src/gmi/tpl/posts/post.gmi index 4f58c84..7d1719b 100644 --- a/src/gmi/tpl/posts/post.gmi +++ b/src/gmi/tpl/posts/post.gmi @@ -6,7 +6,7 @@ > {{ $post.Description }} {{ end -}} -{{ $post.Body }} +{{ .PostBody $post }} -------------------------------------------------------------------------------- -- cgit v1.2.3