properly add HTML data to item description
This commit is contained in:
parent
c82c681221
commit
e25999832c
49
rss/rss.go
49
rss/rss.go
|
@ -4,12 +4,29 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
const feedfmt = `<?xml version="1.0" encoding="utf-8"?>
|
||||
<rss version="2.0">
|
||||
<channel>
|
||||
<title>%s</title>
|
||||
<link>%s</link>
|
||||
<description>%s</description>
|
||||
%s
|
||||
</channel>
|
||||
</rss>
|
||||
`
|
||||
|
||||
const itemfmt = `<item>
|
||||
<title>Content Title</title>
|
||||
<link>%s</link>
|
||||
<guid>%s</guid>
|
||||
<pubDate>%s</pubDate>
|
||||
<description><![CDATA[%s]]></description>
|
||||
</item>`;
|
||||
|
||||
func fetchPage(url string) (string, error) {
|
||||
resp, err := http.Get(url)
|
||||
|
@ -77,29 +94,13 @@ func parseArticle(content string) (string, *time.Time, error) {
|
|||
}
|
||||
}
|
||||
|
||||
article := strings.TrimSuffix(strings.TrimPrefix(builder.String(), "<article>"), "</article>")
|
||||
|
||||
return article, pagetime, nil
|
||||
return builder.String(), pagetime, nil
|
||||
}
|
||||
|
||||
func GenerateRss(siteUrl, siteTitle, siteDesc string, pageUrls ...string) (string, error) {
|
||||
// get page
|
||||
// parse article
|
||||
// parse date
|
||||
// create item element
|
||||
// collect item elements into feed
|
||||
var items strings.Builder
|
||||
var err error
|
||||
|
||||
itemfmt := ` <item>
|
||||
<title>Content Title</title>
|
||||
<link>%s</link>
|
||||
<guid>%s</guid>
|
||||
<pubDate>%s</pubDate>
|
||||
<description>%s</description>
|
||||
</item>
|
||||
`
|
||||
|
||||
for _, u := range pageUrls {
|
||||
page, err := fetchPage(u)
|
||||
if err != nil {
|
||||
|
@ -116,15 +117,5 @@ func GenerateRss(siteUrl, siteTitle, siteDesc string, pageUrls ...string) (strin
|
|||
}
|
||||
}
|
||||
|
||||
feed := `<?xml version="1.0" encoding="utf-8"?>
|
||||
<rss version="2.0">
|
||||
<channel>
|
||||
<title>%s</title>
|
||||
<link>%s</link>
|
||||
<description>%s</description>
|
||||
%s
|
||||
</channel>
|
||||
</rss>
|
||||
`
|
||||
return fmt.Sprintf(feed, siteTitle, siteUrl, siteDesc, items.String()), err
|
||||
return fmt.Sprintf(feedfmt, siteTitle, siteUrl, siteDesc, items.String()), err
|
||||
}
|
||||
|
|
|
@ -19,19 +19,13 @@ func TestArticleParse(t *testing.T) {
|
|||
"article stripped out of basic HTML",
|
||||
"<html><head></head><body><article>hello world</article></body></html>",
|
||||
nil,
|
||||
"hello world",
|
||||
"<article>hello world</article>",
|
||||
},
|
||||
{
|
||||
"article and time stripped out of basic HTML",
|
||||
"<html><head></head><body><article><time datetime=\"2004-05-14\">May 14 2004</time>hello world</article></body></html>",
|
||||
&testDate,
|
||||
"<time datetime=\"2004-05-14\">May 14 2004</time>hello world",
|
||||
},
|
||||
{
|
||||
"article with attributes",
|
||||
"<html><head></head><body><article class=\"test\"><time datetime=\"2004-05-14\">May 14 2004</time>hello world</article></body></html>",
|
||||
&testDate,
|
||||
"<time datetime=\"2004-05-14\">May 14 2004</time>hello world",
|
||||
"<article><time datetime=\"2004-05-14\">May 14 2004</time>hello world</article>",
|
||||
},
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue