items in rss feed. #3
This commit is contained in:
parent
1b99eaa016
commit
e93808ac88
4
post.go
4
post.go
|
@ -33,6 +33,10 @@ func (post Post) UrlPath() string {
|
|||
return post.user.UrlPath() + "posts/" + post.id + "/"
|
||||
}
|
||||
|
||||
func (post Post) FullUrl() string {
|
||||
return post.user.FullUrl() + "posts/" + post.id + "/"
|
||||
}
|
||||
|
||||
func (post Post) UrlMediaPath(filename string) string {
|
||||
return post.UrlPath() + "media/" + filename
|
||||
}
|
||||
|
|
11
post_test.go
11
post_test.go
|
@ -34,6 +34,17 @@ func TestPostUrlPath(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestPostFullUrl(t *testing.T) {
|
||||
user := getTestUser()
|
||||
post, _ := user.CreateNewPost("testpost")
|
||||
expected := "http://localhost:8080/user/" + user.Name() + "/posts/" + post.Id() + "/"
|
||||
if !(post.FullUrl() == expected) {
|
||||
t.Error("Wrong url path")
|
||||
t.Error("Expected: " + expected)
|
||||
t.Error(" Got: " + post.FullUrl())
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostUrlMediaPath(t *testing.T) {
|
||||
user := getTestUser()
|
||||
post, _ := user.CreateNewPost("testpost")
|
||||
|
|
|
@ -113,9 +113,9 @@ func (repo Repository) UserUrlPath(user User) string {
|
|||
return "/user/" + user.name + "/"
|
||||
}
|
||||
|
||||
func (repo Repository) FullUserUrl(user User) string {
|
||||
func (repo Repository) FullUrl() string {
|
||||
config, _ := repo.Config()
|
||||
return config.Domain + repo.UserUrlPath(user)
|
||||
return config.Domain
|
||||
}
|
||||
|
||||
func (repo Repository) Template() (string, error) {
|
||||
|
|
34
rss.go
34
rss.go
|
@ -19,10 +19,10 @@ type RSSChannel struct {
|
|||
}
|
||||
|
||||
type RSSItem struct {
|
||||
Title string `xml:"title"`
|
||||
Link string `xml:"link"`
|
||||
Description string `xml:"description"`
|
||||
PubDate string `xml:"pubDate"`
|
||||
Guid string `xml:"guid"`
|
||||
Title string `xml:"title"`
|
||||
Link string `xml:"link"`
|
||||
PubDate string `xml:"pubDate"`
|
||||
}
|
||||
|
||||
func RenderRSSFeed(user User) (string, error) {
|
||||
|
@ -33,21 +33,27 @@ func RenderRSSFeed(user User) (string, error) {
|
|||
Version: "2.0",
|
||||
Channel: RSSChannel{
|
||||
Title: config.Title,
|
||||
Link: user.repo.FullUserUrl(user),
|
||||
Link: user.FullUrl(),
|
||||
Description: config.SubTitle,
|
||||
Items: make([]RSSItem, 0),
|
||||
},
|
||||
}
|
||||
|
||||
// posts, _ := user.Posts()
|
||||
// for _, post := range posts {
|
||||
// rss.Channel.Items = append(rss.Channel.Items, RSSItem{
|
||||
// Title: post.Title(),
|
||||
// Link: post.Link(),
|
||||
// Description: post.Description(),
|
||||
// PubDate: post.PubDate(),
|
||||
// })
|
||||
// }
|
||||
posts, _ := user.Posts()
|
||||
for _, postId := range posts {
|
||||
post, _ := user.GetPost(postId)
|
||||
_, meta := post.MarkdownData()
|
||||
date, ok := meta["date"]
|
||||
if !ok {
|
||||
date = ""
|
||||
}
|
||||
rss.Channel.Items = append(rss.Channel.Items, RSSItem{
|
||||
Guid: postId,
|
||||
Title: post.Title(),
|
||||
Link: post.FullUrl(),
|
||||
PubDate: date.(string),
|
||||
})
|
||||
}
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
err := xml.NewEncoder(buf).Encode(rss)
|
||||
|
|
51
rss_test.go
51
rss_test.go
|
@ -2,6 +2,7 @@ package owl_test
|
|||
|
||||
import (
|
||||
"h4kor/owl-blogs"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
@ -47,3 +48,53 @@ func TestRenderRSSFeedUserData(t *testing.T) {
|
|||
t.Error("SubTitle not rendered. Got: " + res)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderRSSFeedPostData(t *testing.T) {
|
||||
user := getTestUser()
|
||||
post, _ := user.CreateNewPost("testpost")
|
||||
|
||||
content := "---\n"
|
||||
content += "title: Test Post\n"
|
||||
content += "date: 2015-01-01\n"
|
||||
content += "---\n"
|
||||
content += "This is a test"
|
||||
os.WriteFile(post.ContentFile(), []byte(content), 0644)
|
||||
|
||||
res, err := owl.RenderRSSFeed(user)
|
||||
if err != nil {
|
||||
t.Error("Error rendering RSS feed: " + err.Error())
|
||||
return
|
||||
}
|
||||
if !strings.Contains(res, "Test Post") {
|
||||
t.Error("Title not rendered. Got: " + res)
|
||||
}
|
||||
if !strings.Contains(res, post.FullUrl()) {
|
||||
t.Error("SubTitle not rendered. Got: " + res)
|
||||
}
|
||||
if !strings.Contains(res, "2015-01-01") {
|
||||
t.Error("Date not rendered. Got: " + res)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderRSSFeedPostDataWithoutDate(t *testing.T) {
|
||||
user := getTestUser()
|
||||
post, _ := user.CreateNewPost("testpost")
|
||||
|
||||
content := "---\n"
|
||||
content += "title: Test Post\n"
|
||||
content += "---\n"
|
||||
content += "This is a test"
|
||||
os.WriteFile(post.ContentFile(), []byte(content), 0644)
|
||||
|
||||
res, err := owl.RenderRSSFeed(user)
|
||||
if err != nil {
|
||||
t.Error("Error rendering RSS feed: " + err.Error())
|
||||
return
|
||||
}
|
||||
if !strings.Contains(res, "Test Post") {
|
||||
t.Error("Title not rendered. Got: " + res)
|
||||
}
|
||||
if !strings.Contains(res, post.FullUrl()) {
|
||||
t.Error("SubTitle not rendered. Got: " + res)
|
||||
}
|
||||
}
|
||||
|
|
6
user.go
6
user.go
|
@ -3,6 +3,7 @@ package owl
|
|||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"time"
|
||||
|
@ -29,6 +30,11 @@ func (user User) UrlPath() string {
|
|||
return user.repo.UserUrlPath(user)
|
||||
}
|
||||
|
||||
func (user User) FullUrl() string {
|
||||
url, _ := url.JoinPath(user.repo.FullUrl(), user.UrlPath())
|
||||
return url
|
||||
}
|
||||
|
||||
func (user User) PostDir() string {
|
||||
return path.Join(user.Dir(), "public")
|
||||
}
|
||||
|
|
|
@ -145,3 +145,10 @@ func TestUserUrlPath(t *testing.T) {
|
|||
t.Error("Wrong url path, Expected: " + "/user/" + user.Name() + "/" + " Got: " + user.UrlPath())
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserFullUrl(t *testing.T) {
|
||||
user := getTestUser()
|
||||
if !(user.FullUrl() == "http://localhost:8080/user/"+user.Name()+"/") {
|
||||
t.Error("Wrong url path, Expected: " + "http://localhost:8080/user/" + user.Name() + "/" + " Got: " + user.FullUrl())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue