add rss feed for blogs

This commit is contained in:
gutmet 2020-10-29 12:01:30 +01:00
parent 458a0291b5
commit 9b2d4c9faf
4 changed files with 48 additions and 2 deletions

View File

@ -21,8 +21,8 @@ const postsPerPage int = 5
var blogname string var blogname string
var folder string var folder string
var style string var style string
var templateString string
var t *template.Template var t *template.Template
var rssTemplate *template.Template
var categories map[string][]Post var categories map[string][]Post
func linkToPost(filename string) string { func linkToPost(filename string) string {
@ -208,6 +208,13 @@ func makeTimeline(posts []Post) {
goutil.WriteFile(filepath.Join("stage2", folder, "timeline.html"), buf.String()) goutil.WriteFile(filepath.Join("stage2", folder, "timeline.html"), buf.String())
} }
func makeRSS(posts []Post) {
sort.Sort(byDateDesc(posts))
buf := new(bytes.Buffer)
rssTemplate.Execute(buf, posts)
goutil.WriteFile(filepath.Join("stage2", folder, "feed.rss"), buf.String())
}
type byDate []Post type byDate []Post
func (p byDate) Len() int { return len(p) } func (p byDate) Len() int { return len(p) }
@ -246,6 +253,7 @@ func makeBlog() {
os.Link(indexFilename(n), filepath.Join("stage2", folder, "index.html")) os.Link(indexFilename(n), filepath.Join("stage2", folder, "index.html"))
makeCategories() makeCategories()
makeTimeline(posts) makeTimeline(posts)
makeRSS(posts)
} }
func Generate(dir string) { func Generate(dir string) {
@ -259,8 +267,10 @@ func Generate(dir string) {
var err error var err error
var err2 error var err2 error
style, err = goutil.ReadFile("style.css") style, err = goutil.ReadFile("style.css")
templateString, err2 = goutil.ReadFile("template") templateString, err2 := goutil.ReadFile("template")
t = template.Must(template.New("page").Parse(templateString)) t = template.Must(template.New("page").Parse(templateString))
rssTemplateString, _ := goutil.ReadFile("rssTemplate")
rssTemplate = template.Must(template.New("rss").Parse(rssTemplateString))
if err == nil && err2 == nil { if err == nil && err2 == nil {
makeBlog() makeBlog()
} else { } else {

View File

@ -41,6 +41,10 @@ func dumpInitFiles(style Style) error {
if err != nil { if err != nil {
return err return err
} }
err = goutil.WriteFile("rssTemplate", rssTemplate)
if err != nil {
return err
}
err = goutil.WriteFile("style.css", css) err = goutil.WriteFile("style.css", css)
if err != nil { if err != nil {
return err return err

View File

@ -1,5 +1,27 @@
package initer package initer
var rssTemplate string = `<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
<title>YOUR TITLE</title>
<link>LINK TO YOUR BLOG</link>
<description>DESCRIPTION OF YOUR BLOG</description>
{{range $index, $post := .}}
{{- if (lt $index 10)}}
<item>
<title>{{$post.Meta.Title}}</title>
<content:encoded><![CDATA[ {{$post.Content}} ]]></content:encoded>
<link>BASE ADDRESS OF YOUR BLOG{{$post.Link}}</link>
<pubDate>{{$post.Meta.RFC1123Date}}</pubDate>
</item>
{{end}}
{{- end}}
</channel>
</rss>
`
var defaultTemplate string = `<!DOCTYPE html> var defaultTemplate string = `<!DOCTYPE html>
<html> <html>
<head><title>{{.Title}}</title> <head><title>{{.Title}}</title>
@ -7,6 +29,9 @@ var defaultTemplate string = `<!DOCTYPE html>
<!-- Tell "smart" phones that they are as wide as they are wide... --> <!-- Tell "smart" phones that they are as wide as they are wide... -->
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="data:;base64,iVBORw0KGgo="> <link rel="icon" href="data:;base64,iVBORw0KGgo=">
{{if .PostCollection}}
<link rel="alternate" type="application/rss+xml" title="RSS" href="/blog{{.Blogname}}/feed.rss">
{{end}}
<style> <style>
{{.Style}} {{.Style}}
</style> </style>
@ -115,6 +140,9 @@ var bsStarterTemplate string = `
<!-- Tell "smart" phones that they are as wide as they are wide... --> <!-- Tell "smart" phones that they are as wide as they are wide... -->
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="icon" href="data:;base64,iVBORw0KGgo="> <link rel="icon" href="data:;base64,iVBORw0KGgo=">
{{if .PostCollection}}
<link rel="alternate" type="application/rss+xml" title="RSS" href="/blog{{.Blogname}}/feed.rss">
{{end}}
<title>{{.Title}}</title> <title>{{.Title}}</title>
<style> <style>
{{.Style}} {{.Style}}

View File

@ -61,3 +61,7 @@ type Metadata struct {
func (m Metadata) FormattedDate() string { func (m Metadata) FormattedDate() string {
return m.Date.Format("2006-01-02") return m.Date.Format("2006-01-02")
} }
func (m Metadata) RFC1123Date() string {
return m.Date.Format(time.RFC1123Z)
}