summaryrefslogtreecommitdiff
path: root/feed.go
blob: 27e18bed59c0392a5bf2ac2d0d33bf29f23b5043 (plain)
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
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)
}