diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6722cd9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.xml diff --git a/cli b/cli new file mode 100755 index 0000000..2775a8b Binary files /dev/null and b/cli differ diff --git a/rss/rss.go b/rss/rss.go index b98d556..fbff567 100644 --- a/rss/rss.go +++ b/rss/rss.go @@ -4,8 +4,9 @@ import ( "fmt" "io" "net/http" + "regexp" "strings" - "time" + "time" "golang.org/x/net/html" ) @@ -76,14 +77,54 @@ func parseArticle(content string) (string, *time.Time, error) { } } - return builder.String(), pagetime, nil + article := strings.TrimSuffix(strings.TrimPrefix(builder.String(), "
"), "
") + + return article, pagetime, nil } -func GenerateRss(siteUrl, siteTitle string, pageUrls []string) string { +func GenerateRss(siteUrl, siteTitle, siteDesc string, pageUrls ...string) (string, error) { // get page // parse article // parse date // create item element // collect item elements into feed - return "" + var items strings.Builder + var err error + + itemfmt := ` + Content Title + %s + %s + %s + %s + +` + + for _, u := range pageUrls { + page, err := fetchPage(u) + if err != nil { + continue + } + article, atime, err := parseArticle(page) + if err != nil && article == "" { + continue + } + if atime != nil { + items.WriteString(fmt.Sprintf(itemfmt, u, u, atime.Format("Mon, 2 Jan 2006 15:04:05 MST"), article)) + } else { + items.WriteString(fmt.Sprintf(itemfmt, u, u, time.Now().Format("Mon, 2 Jan 2006 15:04:05 MST"), article)) + } + } + + feed := ` + + + %s + %s + %s + %s + + + ` + return fmt.Sprintf(feed, siteTitle, siteUrl, siteDesc, items.String()), err } diff --git a/rss/rss_test.go b/rss/rss_test.go index 367406f..407b7fa 100644 --- a/rss/rss_test.go +++ b/rss/rss_test.go @@ -19,13 +19,19 @@ func TestArticleParse(t *testing.T) { "article stripped out of basic HTML", "
hello world
", nil, - "
hello world
", + "hello world", }, { "article and time stripped out of basic HTML", "
hello world
", &testDate, - "
hello world
", + "hello world", + }, + { + "article with attributes", + "
hello world
", + &testDate, + "hello world", }, }