Fix post body templates not being parsed correctly
This commit is contained in:
parent
bdd1f01605
commit
180575fe4a
@ -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)
|
||||
}
|
||||
|
@ -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")))
|
||||
func (a *api) parseTpl(name, tplBody string) (*template.Template, error) {
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user