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(" %s%s
\n", p.Title)
}
fmt.Fprintf(&desc, "
(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) }