Skip to content

Commit

Permalink
Some refactoring and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jlelse committed Dec 26, 2023
1 parent 56d3e22 commit 313faf5
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 25 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ require (
github.com/valyala/fastjson v1.6.4 // indirect
go.mau.fi/util v0.2.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20231219180239-dc181d75b848 // indirect
golang.org/x/exp v0.0.0-20231226003508-02704c960a9b // indirect
golang.org/x/image v0.14.0 // indirect
golang.org/x/oauth2 v0.15.0 // indirect
golang.org/x/sys v0.15.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/exp v0.0.0-20231219180239-dc181d75b848 h1:+iq7lrkxmFNBM7xx+Rae2W6uyPfhPeDWD+n+JgppptE=
golang.org/x/exp v0.0.0-20231219180239-dc181d75b848/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
golang.org/x/exp v0.0.0-20231226003508-02704c960a9b h1:kLiC65FbiHWFAOu+lxwNPujcsl8VYyTYYEZnsOO1WK4=
golang.org/x/exp v0.0.0-20231226003508-02704c960a9b/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.14.0 h1:tNgSxAFe3jC4uYqvZdTr84SZoM1KfwdC9SKIFrLjFn4=
golang.org/x/image v0.14.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE=
Expand Down
36 changes: 14 additions & 22 deletions postsFuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,40 +313,32 @@ func (p *post) getChannel() string {
return p.Blog + "/" + p.Section
}

func (a *goBlog) addReplyTitleAndContext(p *post) {
if replyLink := p.firstParameter(a.cfg.Micropub.ReplyParam); replyLink != "" {
addTitle := p.firstParameter(a.cfg.Micropub.ReplyTitleParam) == "" && a.getBlogFromPost(p).addReplyTitle
addContext := p.firstParameter(a.cfg.Micropub.ReplyContextParam) == "" && a.getBlogFromPost(p).addReplyContext
func (a *goBlog) addTitleAndContext(p *post, linkParam, titleParam, contextParam string, addTitleFlag, addContextFlag bool) {
if link := p.firstParameter(linkParam); link != "" {
addTitle := p.firstParameter(titleParam) == "" && addTitleFlag
addContext := p.firstParameter(contextParam) == "" && addContextFlag
if !addTitle && !addContext {
return
}
if mf, err := a.parseMicroformats(replyLink, true); err == nil {
if mf, err := a.parseMicroformats(link, true); err == nil {
if addTitle && mf.Title != "" {
p.addParameter(a.cfg.Micropub.ReplyTitleParam, mf.Title)
p.addParameter(titleParam, mf.Title)
}
if addContext && mf.Content != "" {
p.addParameter(a.cfg.Micropub.ReplyContextParam, mf.Content)
p.addParameter(contextParam, mf.Content)
}
}
}
}

func (a *goBlog) addReplyTitleAndContext(p *post) {
bc := a.getBlogFromPost(p)
a.addTitleAndContext(p, a.cfg.Micropub.ReplyParam, a.cfg.Micropub.ReplyTitleParam, a.cfg.Micropub.ReplyContextParam, bc.addReplyTitle, bc.addReplyContext)
}

func (a *goBlog) addLikeTitleAndContext(p *post) {
if likeLink := p.firstParameter(a.cfg.Micropub.LikeParam); likeLink != "" {
addTitle := p.firstParameter(a.cfg.Micropub.LikeTitleParam) == "" && a.getBlogFromPost(p).addLikeTitle
addContext := p.firstParameter(a.cfg.Micropub.LikeContextParam) == "" && a.getBlogFromPost(p).addLikeContext
if !addTitle && !addContext {
return
}
if mf, err := a.parseMicroformats(likeLink, true); err == nil {
if addTitle && mf.Title != "" {
p.addParameter(a.cfg.Micropub.LikeTitleParam, mf.Title)
}
if addContext && mf.Content != "" {
p.addParameter(a.cfg.Micropub.LikeContextParam, mf.Content)
}
}
}
bc := a.getBlogFromPost(p)
a.addTitleAndContext(p, a.cfg.Micropub.LikeParam, a.cfg.Micropub.LikeTitleParam, a.cfg.Micropub.LikeContextParam, bc.addLikeTitle, bc.addLikeContext)
}

// Public because of rendering
Expand Down
44 changes: 44 additions & 0 deletions postsFuncs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"net/http"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_AddReplyTitleAndContext(t *testing.T) {
app := createMicropubTestEnv(t)

bc := app.cfg.Blogs[app.cfg.DefaultBlog]
bc.addReplyContext = true
bc.addReplyTitle = true

fhc := newFakeHttpClient()
app.httpClient = fhc.Client
fhc.setFakeResponse(http.StatusOK, `
<!doctypehtml>
<title>Microformats Entry Example</title>
<article class=h-entry>
<h1 class=p-name>My First Microformats Post</h1>
<div class=e-content>
<p>This is the main content of my post. It can contain <a href=#>links</a>, <strong>bold</strong> text, and more.</div>
</article>
`)

err := app.createPost(&post{
Path: "/testpost",
Parameters: map[string][]string{
"replylink": {"https://example.com"},
},
Content: "Test",
})
require.NoError(t, err)

p, err := app.getPost("/testpost")
require.NoError(t, err)

assert.Equal(t, "My First Microformats Post", p.firstParameter("replytitle"))
assert.Equal(t, "This is the main content of my post. It can contain links, bold text, and more.", p.firstParameter("replycontext"))
}

0 comments on commit 313faf5

Please sign in to comment.