diff options
author | Brian Picciano <mediocregopher@gmail.com> | 2024-05-19 22:14:25 +0200 |
---|---|---|
committer | Brian Picciano <mediocregopher@gmail.com> | 2024-05-19 22:14:25 +0200 |
commit | 332e983da476974805a3a0d53bfedaebe73a9fcb (patch) | |
tree | 1956248d86fc07c0193ffdeea77469655fd69c8a | |
parent | 0665d0c65974533fbd313f4e0b062b5103057aeb (diff) |
Get rid of most of preprocess funcs, only Image leftover for convenience
-rw-r--r-- | src/gmi/tpl.go | 34 | ||||
-rw-r--r-- | src/http/http.go | 2 | ||||
-rw-r--r-- | src/http/posts.go | 19 | ||||
-rw-r--r-- | src/http/tpl.go | 15 | ||||
-rw-r--r-- | src/post/preprocess.go | 71 | ||||
-rw-r--r-- | src/render/methods.go | 38 |
6 files changed, 37 insertions, 142 deletions
diff --git a/src/gmi/tpl.go b/src/gmi/tpl.go index cdf9535..d4c6b00 100644 --- a/src/gmi/tpl.go +++ b/src/gmi/tpl.go @@ -57,27 +57,6 @@ func (a *api) tplHandler() (gemini.Handler, error) { } preprocessFuncs := post.PreprocessFunctions{ - BlogURL: func(path string) string { - return blogURL(a.params.PublicURL, path, false) - }, - BlogHTTPURL: func(path string) string { - return blogURL(a.params.HTTPPublicURL, path, true) - }, - BlogGeminiURL: func(path string) string { - return blogURL(a.params.PublicURL, path, true) - }, - AssetURL: func(id string) string { - path := filepath.Join("assets", id) - return blogURL(a.params.PublicURL, path, false) - }, - PostURL: func(id string) string { - path := filepath.Join("posts", id) + ".gmi" - return blogURL(a.params.PublicURL, path, false) - }, - StaticURL: func(path string) string { - path = filepath.Join("static", path) - return blogURL(a.params.HTTPPublicURL, path, true) - }, Image: func(args ...string) (string, error) { var ( id = args[0] @@ -97,19 +76,6 @@ func (a *api) tplHandler() (gemini.Handler, error) { allTpls := template.New("") - allTpls.Funcs(preprocessFuncs.ToFuncMap()) - - allTpls.Funcs(template.FuncMap{ - "PostURLAbs": func(id string) string { - path := filepath.Join("posts", id) + ".gmi" - return blogURL(a.params.PublicURL, path, true) - }, - "PostHTTPURL": func(id string) string { - path := filepath.Join("posts", id) - return preprocessFuncs.BlogHTTPURL(path) - }, - }) - err := fs.WalkDir(tplFS, "tpl", func(path string, d fs.DirEntry, err error) error { if err != nil { diff --git a/src/http/http.go b/src/http/http.go index 93cc043..6458416 100644 --- a/src/http/http.go +++ b/src/http/http.go @@ -149,7 +149,7 @@ func New(params Params) (API, error) { auther: NewAuther(params.AuthUsers, params.AuthRatelimit), } - a.redirectTpl = mustParseTpl(a.emptyTpl(), "redirect.html") + a.redirectTpl = mustParseTpl(template.New(""), "redirect.html") a.srv = &http.Server{Handler: a.handler()} diff --git a/src/http/posts.go b/src/http/posts.go index 42e5b4a..daaaafc 100644 --- a/src/http/posts.go +++ b/src/http/posts.go @@ -65,25 +65,6 @@ func (a *api) postPreprocessFuncImage(args ...string) (string, error) { func (a *api) postPreprocessFuncs() post.PreprocessFunctions { return post.PreprocessFunctions{ - BlogURL: func(path string) string { - return a.blogURL(a.params.PublicURL, path, false) - }, - BlogHTTPURL: func(path string) string { - return a.blogURL(a.params.PublicURL, path, true) - }, - BlogGeminiURL: func(path string) string { - return a.blogURL(a.params.GeminiPublicURL, path, true) - }, - 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(a.params.PublicURL, path, false) - }, Image: a.postPreprocessFuncImage, } } diff --git a/src/http/tpl.go b/src/http/tpl.go index c4afbdc..57d7cfd 100644 --- a/src/http/tpl.go +++ b/src/http/tpl.go @@ -90,25 +90,12 @@ func (a *api) draftsURL(abs bool) string { return a.blogURL(a.params.PublicURL, "drafts", abs) } -func (a *api) tplFuncs() template.FuncMap { - return template.FuncMap{ - "DraftURL": func(id string) string { return a.draftPostURL(id, false) }, - } -} - -func (a *api) emptyTpl() *template.Template { - tpl := template.New("") - tpl = tpl.Funcs(a.tplFuncs()) - tpl = tpl.Funcs(a.postPreprocessFuncs().ToFuncMap()) - return tpl -} - func mustParseTpl(tpl *template.Template, name string) *template.Template { return template.Must(tpl.New(name).Parse(mustReadTplFile(name))) } func (a *api) mustParseBasedTpl(name string) *template.Template { - tpl := a.emptyTpl() + tpl := template.New("") tpl = mustParseTpl(tpl, "gemini-cta.html") tpl = mustParseTpl(tpl, "base.html") tpl = mustParseTpl(tpl, name) diff --git a/src/post/preprocess.go b/src/post/preprocess.go index 77aea81..7aceee2 100644 --- a/src/post/preprocess.go +++ b/src/post/preprocess.go @@ -1,46 +1,9 @@ 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 - - // BlogHTTPURL returns the given string, rooted to the base URL of the - // blog's HTTP server (which may or may not include path components itself). - // - // The given path should not have a leading slash. - BlogHTTPURL func(path string) string - - // BlogGeminiURL returns the given string, rooted to the base URL of the - // blog's gemini server (which may or may not include path components - // itself). - // - // The given path should not have a leading slash. - BlogGeminiURL 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. // @@ -49,37 +12,3 @@ type PreprocessFunctions struct { // alt text, or possibly displayed to the user with the image. Image func(args ...string) (string, error) } - -func (funcs PreprocessFunctions) ToFuncMap() template.FuncMap { - return template.FuncMap{ - "BlogURL": funcs.BlogURL, - "BlogHTTPURL": funcs.BlogHTTPURL, - "BlogGeminiURL": funcs.BlogGeminiURL, - "AssetURL": funcs.AssetURL, - "PostURL": funcs.PostURL, - "StaticURL": funcs.StaticURL, - "Image": funcs.Image, - } -} - -// 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(funcs.ToFuncMap()) - - 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 -} diff --git a/src/render/methods.go b/src/render/methods.go index ee22dfd..2b8675b 100644 --- a/src/render/methods.go +++ b/src/render/methods.go @@ -5,11 +5,13 @@ import ( "context" "fmt" "html/template" + "io" "net/url" "path" "path/filepath" "strconv" "strings" + txttpl "text/template" "dev.mediocregopher.com/mediocre-blog.git/src/gmi/gemtext" "dev.mediocregopher.com/mediocre-blog.git/src/post" @@ -196,11 +198,41 @@ func (m *Methods) GetPostSeriesNextPrevious( return res, nil } -func (m *Methods) PostGemtextBody(p post.StoredPost) (string, error) { +type preprocessPostPayload struct { + RootURL URLBuilder + image func(args ...string) (string, error) +} + +func (p preprocessPostPayload) Image(args ...string) (string, error) { + return p.image(args...) +} + +// preprocessPostBody 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 (m *Methods) preprocessPostBody(into io.Writer, p post.Post) error { + tpl := txttpl.New("") + + tpl, err := tpl.Parse(p.Body) + if err != nil { + return fmt.Errorf("parsing post body as template: %w", err) + } + + err = tpl.Execute(into, preprocessPostPayload{ + RootURL: m.RootURL(), + image: m.preprocessFuncs.Image, + }) + if err != nil { + return fmt.Errorf("executing post body as template: %w", err) + } + return nil +} + +func (m *Methods) PostGemtextBody(p post.StoredPost) (string, error) { buf := new(bytes.Buffer) - if err := p.PreprocessBody(buf, m.preprocessFuncs); err != nil { + if err := m.preprocessPostBody(buf, p.Post); err != nil { return "", fmt.Errorf("preprocessing post body: %w", err) } @@ -222,7 +254,7 @@ func (m *Methods) PostGemtextBody(p post.StoredPost) (string, error) { func (m *Methods) PostHTMLBody(p post.StoredPost) (template.HTML, error) { bodyBuf := new(bytes.Buffer) - if err := p.PreprocessBody(bodyBuf, m.preprocessFuncs); err != nil { + if err := m.preprocessPostBody(bodyBuf, p.Post); err != nil { return "", fmt.Errorf("preprocessing post body: %w", err) } |