summaryrefslogtreecommitdiff
path: root/srv
diff options
context:
space:
mode:
authorBrian Picciano <mediocregopher@gmail.com>2022-05-21 12:10:38 -0600
committerBrian Picciano <mediocregopher@gmail.com>2022-05-21 12:10:38 -0600
commit180575fe4ace9c491940069dc911681b8e4940f3 (patch)
tree26786af8dc04ed5103779d6a7ed09f3c701b93ef /srv
parentbdd1f01605f48dd002836221b0eac03874038f5d (diff)
Fix post body templates not being parsed correctly
Diffstat (limited to 'srv')
-rw-r--r--srv/src/http/posts.go37
-rw-r--r--srv/src/http/tpl.go36
2 files changed, 44 insertions, 29 deletions
diff --git a/srv/src/http/posts.go b/srv/src/http/posts.go
index 2f96c6d..432a1e1 100644
--- a/srv/src/http/posts.go
+++ b/srv/src/http/posts.go
@@ -8,6 +8,7 @@ import (
"net/http"
"path/filepath"
"strings"
+ txttpl "text/template"
"time"
"github.com/gomarkdown/markdown"
@@ -17,6 +18,40 @@ import (
"github.com/mediocregopher/blog.mediocregopher.com/srv/post"
)
+func (a *api) parsePostBody(storedPost post.StoredPost) (*txttpl.Template, error) {
+ tpl := txttpl.New("root")
+ tpl = tpl.Funcs(txttpl.FuncMap(a.tplFuncs()))
+
+ tpl = txttpl.Must(tpl.New("image.html").Parse(mustReadTplFile("image.html")))
+ tpl = tpl.Funcs(txttpl.FuncMap{
+ "Image": func(id string) (string, error) {
+
+ tplPayload := struct {
+ ID string
+ Resizable bool
+ }{
+ ID: id,
+ Resizable: isImgResizable(id),
+ }
+
+ buf := new(bytes.Buffer)
+ if err := tpl.ExecuteTemplate(buf, "image.html", tplPayload); err != nil {
+ return "", err
+ }
+
+ return buf.String(), nil
+ },
+ })
+
+ tpl, err := tpl.New(storedPost.ID + "-body.html").Parse(storedPost.Body)
+
+ if err != nil {
+ return nil, err
+ }
+
+ return tpl, nil
+}
+
type postTplPayload struct {
post.StoredPost
SeriesPrevious, SeriesNext *post.StoredPost
@@ -25,7 +60,7 @@ type postTplPayload struct {
func (a *api) postToPostTplPayload(storedPost post.StoredPost) (postTplPayload, error) {
- bodyTpl, err := a.parseTpl(storedPost.ID+"-body.html", storedPost.Body)
+ bodyTpl, err := a.parsePostBody(storedPost)
if err != nil {
return postTplPayload{}, fmt.Errorf("parsing post body as template: %w", err)
}
diff --git a/srv/src/http/tpl.go b/srv/src/http/tpl.go
index 2e22370..5c235a1 100644
--- a/srv/src/http/tpl.go
+++ b/srv/src/http/tpl.go
@@ -1,7 +1,6 @@
package http
import (
- "bytes"
"embed"
"fmt"
"html/template"
@@ -59,11 +58,8 @@ func (a *api) assetsURL(abs bool) string {
return a.blogURL("assets", abs)
}
-func (a *api) parseTpl(name, tplBody string) (*template.Template, error) {
-
- tpl := template.New("root")
-
- tpl = tpl.Funcs(template.FuncMap{
+func (a *api) tplFuncs() template.FuncMap {
+ return template.FuncMap{
"BlogURL": func(path string) string {
return a.blogURL(path, false)
},
@@ -81,33 +77,17 @@ func (a *api) parseTpl(name, tplBody string) (*template.Template, error) {
"DateTimeFormat": func(t time.Time) string {
return t.Format("2006-01-02")
},
- })
-
- tpl = template.Must(tpl.New("image.html").Parse(mustReadTplFile("image.html")))
-
- tpl = tpl.Funcs(template.FuncMap{
- "Image": func(id string) (template.HTML, error) {
-
- tplPayload := struct {
- ID string
- Resizable bool
- }{
- ID: id,
- Resizable: isImgResizable(id),
- }
+ }
+}
- buf := new(bytes.Buffer)
- if err := tpl.ExecuteTemplate(buf, "image.html", tplPayload); err != nil {
- return "", err
- }
+func (a *api) parseTpl(name, tplBody string) (*template.Template, error) {
- return template.HTML(buf.Bytes()), nil
- },
- })
+ tpl := template.New(name)
+ tpl = tpl.Funcs(a.tplFuncs())
var err error
- if tpl, err = tpl.New(name).Parse(tplBody); err != nil {
+ if tpl, err = tpl.Parse(tplBody); err != nil {
return nil, err
}