From 78b410c668354c30323a259167beb7eddaa2d58f Mon Sep 17 00:00:00 2001 From: Sebastian Zack Tin Lahm-Lee Date: Sun, 4 Oct 2026 12:58:12 +0800 Subject: first ver --- feed.go | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 feed.go (limited to 'feed.go') 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(`` + "\n") + b.WriteString("\n\n") + fmt.Fprintf(&b, " %s\n", xmlEsc(siteName)) + fmt.Fprintf(&b, " %s\n", xmlEsc(s.baseURL)) + b.WriteString(" Casual Words From The Moonlight Tower\n") + fmt.Fprintf(&b, " %s\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, "

%s

\n", p.Title) + } + fmt.Fprintf(&desc, "

%s

\n", p.DateStr) + desc.WriteString(string(p.Body)) + b.WriteString(" \n") + fmt.Fprintf(&b, " %s\n", xmlEsc(title)) + fmt.Fprintf(&b, " %s\n", xmlEsc(link)) + fmt.Fprintf(&b, " %s\n", xmlEsc(link)) + fmt.Fprintf(&b, " %s\n", p.Date.UTC().Format(time.RFC1123Z)) + fmt.Fprintf(&b, " %s\n", xmlEsc(s.truncateDescription(desc.String()))) + b.WriteString(" \n") + } + b.WriteString("
\n
\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

(more on the source page)

" + +// 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 &. + 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 +// (& < > ' ") is valid XML. +func xmlEsc(s string) string { + return html.EscapeString(s) +} -- cgit v1.2.3