summaryrefslogtreecommitdiff
path: root/src/http
diff options
context:
space:
mode:
Diffstat (limited to 'src/http')
-rw-r--r--src/http/posts.go109
1 files changed, 48 insertions, 61 deletions
diff --git a/src/http/posts.go b/src/http/posts.go
index eff0eaa..cae8f43 100644
--- a/src/http/posts.go
+++ b/src/http/posts.go
@@ -21,68 +21,43 @@ import (
"github.com/mediocregopher/mediocre-go-lib/v2/mctx"
)
-func (a *api) parsePostBody(p post.Post) (*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")))
-
- if p.Format == post.FormatMarkdown {
- tpl = tpl.Funcs(txttpl.FuncMap{
- "Image": func(id string) (string, error) {
-
- tplPayload := struct {
- ID string
- Descr string
- Resizable bool
- }{
- ID: id,
- // I could use variadic args to make this work, I think
- Descr: "TODO: proper alt text",
- Resizable: isImgResizable(id),
- }
-
- buf := new(bytes.Buffer)
- if err := tpl.ExecuteTemplate(buf, "image.html", tplPayload); err != nil {
- return "", err
- }
-
- return buf.String(), nil
- },
- })
- }
+func (a *api) postPreprocessFuncImage(args ...string) (string, error) {
+
+ var (
+ id = args[0]
+ descr = "TODO"
+ )
- if p.Format == post.FormatGemtext {
- tpl = tpl.Funcs(txttpl.FuncMap{
- "Image": func(id, descr string) (string, error) {
-
- tplPayload := struct {
- ID string
- Descr string
- Resizable bool
- }{
- ID: id,
- Descr: descr,
- Resizable: isImgResizable(id),
- }
-
- buf := new(bytes.Buffer)
- if err := tpl.ExecuteTemplate(buf, "image.html", tplPayload); err != nil {
- return "", err
- }
-
- return buf.String(), nil
- },
- })
+ if len(args) > 1 {
+ descr = args[1]
}
- tpl, err := tpl.New(p.ID + "-body.html").Parse(p.Body)
+ tpl := txttpl.New("image.html")
- if err != nil {
- return nil, err
+ tpl.Funcs(txttpl.FuncMap{
+ "AssetURL": func(id string) string {
+ return a.assetURL(id, false)
+ },
+ })
+
+ tpl = txttpl.Must(tpl.Parse(mustReadTplFile("image.html")))
+
+ tplPayload := struct {
+ ID string
+ Descr string
+ Resizable bool
+ }{
+ ID: id,
+ Descr: descr,
+ Resizable: isImgResizable(id),
+ }
+
+ buf := new(bytes.Buffer)
+ if err := tpl.ExecuteTemplate(buf, "image.html", tplPayload); err != nil {
+ return "", err
}
- return tpl, nil
+ return buf.String(), nil
}
type postTplPayload struct {
@@ -93,15 +68,27 @@ type postTplPayload struct {
func (a *api) postToPostTplPayload(storedPost post.StoredPost) (postTplPayload, error) {
- bodyTpl, err := a.parsePostBody(storedPost.Post)
- if err != nil {
- return postTplPayload{}, fmt.Errorf("parsing post body as template: %w", err)
+ preprocessFuncs := post.PreprocessFunctions{
+ BlogURL: func(path string) string {
+ return a.blogURL(path, false)
+ },
+ 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(path, false)
+ },
+ Image: a.postPreprocessFuncImage,
}
bodyBuf := new(bytes.Buffer)
- if err := bodyTpl.Execute(bodyBuf, nil); err != nil {
- return postTplPayload{}, fmt.Errorf("executing post body as template: %w", err)
+ if err := storedPost.PreprocessBody(bodyBuf, preprocessFuncs); err != nil {
+ return postTplPayload{}, fmt.Errorf("preprocessing post body: %w", err)
}
if storedPost.Format == post.FormatGemtext {