summaryrefslogtreecommitdiff
path: root/feed.go
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--feed.go78
1 files changed, 78 insertions, 0 deletions
diff --git a/feed.go b/feed.go
new file mode 100644
index 0000000..27e18be
--- /dev/null
+++ b/feed.go
@@ -0,0 +1,78 @@
+package main
+
+import (
+ "fmt"
+ "html"
+ "net/http"
+ "strings"
+ "time"
+)
+
+// handleFeed serves an RSS 2.0 feed (/feed.xml, alias /rss.xml) generated
+// from the newest posts. Item titles use the post's `title:` meta if it
+// has one, otherwise the date. Descriptions carry the post's HTML, cut off
+// with a pointer to the post page once they exceed -feed-maxlen characters.
+func (s *Server) handleFeed(w http.ResponseWriter, r *http.Request) {
+ posts := s.site.Posts()
+ if len(posts) > s.feedItems {
+ posts = posts[:s.feedItems]
+ }
+ var b strings.Builder
+ b.WriteString(`<?xml version="1.0" encoding="UTF-8"?>` + "\n")
+ b.WriteString("<rss version=\"2.0\">\n<channel>\n")
+ fmt.Fprintf(&b, " <title>%s</title>\n", xmlEsc(siteName))
+ fmt.Fprintf(&b, " <link>%s</link>\n", xmlEsc(s.baseURL))
+ b.WriteString(" <description>Casual Words From The Moonlight Tower</description>\n")
+ fmt.Fprintf(&b, " <lastBuildDate>%s</lastBuildDate>\n", time.Now().UTC().Format(time.RFC1123Z))
+ for _, p := range posts {
+ title := p.TitleText
+ if title == "" {
+ title = p.DateStr
+ }
+ link := s.baseURL + "/post/" + p.Name + ".html"
+ var desc strings.Builder
+ if p.Title != "" {
+ fmt.Fprintf(&desc, "<h1>%s</h1>\n", p.Title)
+ }
+ fmt.Fprintf(&desc, "<p>%s</p>\n", p.DateStr)
+ desc.WriteString(string(p.Body))
+ b.WriteString(" <item>\n")
+ fmt.Fprintf(&b, " <title>%s</title>\n", xmlEsc(title))
+ fmt.Fprintf(&b, " <link>%s</link>\n", xmlEsc(link))
+ fmt.Fprintf(&b, " <guid isPermaLink=\"true\">%s</guid>\n", xmlEsc(link))
+ fmt.Fprintf(&b, " <pubDate>%s</pubDate>\n", p.Date.UTC().Format(time.RFC1123Z))
+ fmt.Fprintf(&b, " <description>%s</description>\n", xmlEsc(s.truncateDescription(desc.String())))
+ b.WriteString(" </item>\n")
+ }
+ b.WriteString("</channel>\n</rss>\n")
+ w.Header().Set("Content-Type", "application/rss+xml; charset=utf-8")
+ w.Header().Set("Cache-Control", "no-cache")
+ w.Write([]byte(b.String()))
+}
+
+const feedMoreNote = "\n<p>(more on the source page)</p>"
+
+// truncateDescription cuts an HTML description at max characters (runes),
+// backing off so it doesn't split a tag or an entity in half, and appends
+// a pointer to the post page. max <= 0 disables truncation.
+func (s *Server) truncateDescription(desc string) string {
+ if s.feedMaxLen <= 0 || len([]rune(desc)) <= s.feedMaxLen {
+ return desc
+ }
+ cut := string([]rune(desc)[:s.feedMaxLen])
+ // Back off if the cut landed inside a tag.
+ if i := strings.LastIndex(cut, "<"); i > strings.LastIndex(cut, ">") {
+ cut = cut[:i]
+ }
+ // Back off if the cut landed inside an entity like &amp;.
+ if j := strings.LastIndex(cut, "&"); j > strings.LastIndex(cut, ";") {
+ cut = cut[:j]
+ }
+ return strings.TrimRight(cut, " \t\r\n") + feedMoreNote
+}
+
+// xmlEsc escapes text for XML character data. html.EscapeString's output
+// (&amp; &lt; &gt; &#39; &#34;) is valid XML.
+func xmlEsc(s string) string {
+ return html.EscapeString(s)
+}