Fix post body templates not being parsed correctly

This commit is contained in:
Brian Picciano 2022-05-21 12:10:38 -06:00
parent bdd1f01605
commit 180575fe4a
2 changed files with 44 additions and 29 deletions

View File

@ -8,6 +8,7 @@ import (
"net/http" "net/http"
"path/filepath" "path/filepath"
"strings" "strings"
txttpl "text/template"
"time" "time"
"github.com/gomarkdown/markdown" "github.com/gomarkdown/markdown"
@ -17,6 +18,40 @@ import (
"github.com/mediocregopher/blog.mediocregopher.com/srv/post" "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 { type postTplPayload struct {
post.StoredPost post.StoredPost
SeriesPrevious, SeriesNext *post.StoredPost SeriesPrevious, SeriesNext *post.StoredPost
@ -25,7 +60,7 @@ type postTplPayload struct {
func (a *api) postToPostTplPayload(storedPost post.StoredPost) (postTplPayload, error) { 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 { if err != nil {
return postTplPayload{}, fmt.Errorf("parsing post body as template: %w", err) return postTplPayload{}, fmt.Errorf("parsing post body as template: %w", err)
} }

View File

@ -1,7 +1,6 @@
package http package http
import ( import (
"bytes"
"embed" "embed"
"fmt" "fmt"
"html/template" "html/template"
@ -59,11 +58,8 @@ func (a *api) assetsURL(abs bool) string {
return a.blogURL("assets", abs) return a.blogURL("assets", abs)
} }
func (a *api) parseTpl(name, tplBody string) (*template.Template, error) { func (a *api) tplFuncs() template.FuncMap {
return template.FuncMap{
tpl := template.New("root")
tpl = tpl.Funcs(template.FuncMap{
"BlogURL": func(path string) string { "BlogURL": func(path string) string {
return a.blogURL(path, false) 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 { "DateTimeFormat": func(t time.Time) string {
return t.Format("2006-01-02") return t.Format("2006-01-02")
}, },
}) }
}
tpl = template.Must(tpl.New("image.html").Parse(mustReadTplFile("image.html"))) func (a *api) parseTpl(name, tplBody string) (*template.Template, error) {
tpl = tpl.Funcs(template.FuncMap{ tpl := template.New(name)
"Image": func(id string) (template.HTML, error) { tpl = tpl.Funcs(a.tplFuncs())
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 template.HTML(buf.Bytes()), nil
},
})
var err error var err error
if tpl, err = tpl.New(name).Parse(tplBody); err != nil { if tpl, err = tpl.Parse(tplBody); err != nil {
return nil, err return nil, err
} }