summaryrefslogtreecommitdiff
path: root/src/http/posts.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/http/posts.go')
-rw-r--r--src/http/posts.go94
1 files changed, 74 insertions, 20 deletions
diff --git a/src/http/posts.go b/src/http/posts.go
index 0f8924a..eff0eaa 100644
--- a/src/http/posts.go
+++ b/src/http/posts.go
@@ -15,37 +15,68 @@ import (
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
+ "github.com/mediocregopher/blog.mediocregopher.com/srv/gmi"
"github.com/mediocregopher/blog.mediocregopher.com/srv/http/apiutil"
"github.com/mediocregopher/blog.mediocregopher.com/srv/post"
"github.com/mediocregopher/mediocre-go-lib/v2/mctx"
)
-func (a *api) parsePostBody(post post.Post) (*txttpl.Template, error) {
+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")))
- 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
- }
+ 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
+ },
+ })
+ }
- return buf.String(), nil
- },
- })
+ 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
+ },
+ })
+ }
- tpl, err := tpl.New(post.ID + "-body.html").Parse(post.Body)
+ tpl, err := tpl.New(p.ID + "-body.html").Parse(p.Body)
if err != nil {
return nil, err
@@ -73,6 +104,16 @@ func (a *api) postToPostTplPayload(storedPost post.StoredPost) (postTplPayload,
return postTplPayload{}, fmt.Errorf("executing post body as template: %w", err)
}
+ if storedPost.Format == post.FormatGemtext {
+
+ prevBodyBuf := bodyBuf
+ bodyBuf = new(bytes.Buffer)
+
+ if err := gmi.GemtextToMarkdown(bodyBuf, prevBodyBuf); err != nil {
+ return postTplPayload{}, fmt.Errorf("converting gemtext to markdown: %w", err)
+ }
+ }
+
// this helps the markdown renderer properly parse pages which end in a
// `</script>` tag... I don't know why.
_, _ = bodyBuf.WriteString("\n")
@@ -324,10 +365,12 @@ func (a *api) editPostHandler(isDraft bool) http.Handler {
Post post.StoredPost
Tags []string
IsDraft bool
+ Formats []post.Format
}{
Post: storedPost,
Tags: tags,
IsDraft: isDraft,
+ Formats: post.Formats,
}
executeTemplate(rw, r, tpl, tplPayload)
@@ -336,12 +379,23 @@ func (a *api) editPostHandler(isDraft bool) http.Handler {
func postFromPostReq(r *http.Request) (post.Post, error) {
+ formatStr := r.PostFormValue("format")
+ if formatStr == "" {
+ return post.Post{}, errors.New("format is required")
+ }
+
+ format, err := post.FormatFromString(formatStr)
+ if err != nil {
+ return post.Post{}, fmt.Errorf("parsing format: %w", err)
+ }
+
p := post.Post{
ID: r.PostFormValue("id"),
Title: r.PostFormValue("title"),
Description: r.PostFormValue("description"),
Tags: strings.Fields(r.PostFormValue("tags")),
Series: r.PostFormValue("series"),
+ Format: format,
}
// textareas encode newlines as CRLF for historical reasons
@@ -353,7 +407,7 @@ func postFromPostReq(r *http.Request) (post.Post, error) {
p.Title == "" ||
p.Body == "" ||
len(p.Tags) == 0 {
- return post.Post{}, errors.New("ID, Title, Tags, and Body are all required")
+ return post.Post{}, errors.New("id, ritle, tags, and body are all required")
}
return p, nil