package feed
import (
"fmt"
"strings"
"time"
"golang.org/x/net/html"
)
const feedfmt = `
%s
%s
%s%s
`
const itemfmt = `
-
Content Title
%s
%s
%s
%s
`
type RSSBuilder struct {
Info FeedInfo
Items []FeedItem
}
func GenerateRss(siteUrl, siteTitle, siteDesc string, pageUrls ...string) (string, error) {
var items strings.Builder
var errs strings.Builder
var err error
for _, u := range pageUrls {
var formattedArticle strings.Builder
var err error
page, err := fetchPage(u)
if err != nil {
continue
}
article, atime, err := parseArticle(page)
if err != nil && article == "" {
errs.WriteString(fmt.Sprintf("error parsing article %s: %s", u, err.Error()))
continue
}
for _, line := range strings.Split(article, "\n") {
formattedArticle.WriteString(fmt.Sprintf("\t\t%s\n", html.EscapeString(line)))
}
if atime != nil {
items.WriteString(fmt.Sprintf(itemfmt, u, u, atime.Format("Mon, 2 Jan 2006 15:04:05 MST"), formattedArticle.String()))
} else {
items.WriteString(fmt.Sprintf(itemfmt, u, u, time.Now().Format("Mon, 2 Jan 2006 15:04:05 MST"), formattedArticle.String()))
}
}
return fmt.Sprintf(feedfmt, siteTitle, siteUrl, siteDesc, items.String()), err
}