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/post/preprocess.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/post/preprocess.go (limited to 'src/post/preprocess.go') diff --git a/src/post/preprocess.go b/src/post/preprocess.go new file mode 100644 index 0000000..4d4be9d --- /dev/null +++ b/src/post/preprocess.go @@ -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 +} -- cgit v1.2.3