-
Notifications
You must be signed in to change notification settings - Fork 26
/
feeds.go
86 lines (81 loc) · 2.31 KB
/
feeds.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"cmp"
"io"
"net/http"
"time"
"github.com/araddon/dateparse"
"github.com/jlelse/feeds"
"go.goblog.app/app/pkgs/bufferpool"
"go.goblog.app/app/pkgs/contenttype"
)
type feedType string
const (
noFeed feedType = ""
rssFeed feedType = "rss"
atomFeed feedType = "atom"
jsonFeed feedType = "json"
minRssFeed feedType = "min.rss"
minAtomFeed feedType = "min.atom"
minJsonFeed feedType = "min.json"
)
func (a *goBlog) generateFeed(blog string, f feedType, w http.ResponseWriter, r *http.Request, posts []*post, title, description, path, query string) {
now := time.Now()
title = a.renderMdTitle(cmp.Or(title, a.cfg.Blogs[blog].Title))
description = cmp.Or(description, a.cfg.Blogs[blog].Description)
feed := &feeds.Feed{
Title: title,
Description: description,
Link: &feeds.Link{Href: a.getFullAddress(path) + query},
Created: now,
Author: &feeds.Author{
Name: a.cfg.User.Name,
Email: a.cfg.User.Email,
},
Image: &feeds.Image{
Url: a.profileImagePath(profileImageFormatJPEG, 0, 0),
},
}
for _, p := range posts {
buf := bufferpool.Get()
switch f {
case minRssFeed, minAtomFeed, minJsonFeed:
a.minFeedHtml(buf, p)
default:
a.feedHtml(buf, p)
}
feed.Add(&feeds.Item{
Title: p.RenderedTitle,
Link: &feeds.Link{Href: a.fullPostURL(p)},
Description: a.postSummary(p),
Id: p.Path,
Content: buf.String(),
Created: noError(dateparse.ParseLocal(p.Published)),
Updated: noError(dateparse.ParseLocal(p.Updated)),
Tags: sortedStrings(p.Parameters[a.cfg.Micropub.CategoryParam]),
})
bufferpool.Put(buf)
}
var feedWriteFunc func(w io.Writer) error
var feedMediaType string
switch f {
case rssFeed, minRssFeed:
feedMediaType = contenttype.RSS
feedWriteFunc = feed.WriteRss
case atomFeed, minAtomFeed:
feedMediaType = contenttype.ATOM
feedWriteFunc = feed.WriteAtom
case jsonFeed, minJsonFeed:
feedMediaType = contenttype.JSONFeed
feedWriteFunc = feed.WriteJSON
default:
a.serve404(w, r)
return
}
pipeReader, pipeWriter := io.Pipe()
go func() {
_ = pipeWriter.CloseWithError(feedWriteFunc(pipeWriter))
}()
w.Header().Set(contentType, feedMediaType+contenttype.CharsetUtf8Suffix)
_ = pipeReader.CloseWithError(a.min.Get().Minify(feedMediaType, w, pipeReader))
}