Implement atom feeds

This commit is contained in:
adnano 2020-11-22 15:14:50 -05:00
parent 491b4637c1
commit fe4dcb144e
8 changed files with 50 additions and 48 deletions

View file

@ -9,9 +9,10 @@ import (
// Config contains site configuration. // Config contains site configuration.
type Config struct { type Config struct {
Title string Title string // site title
Feeds map[string]string URL string // site URL
Templates *Templates Feeds map[string]string // site feeds
Templates *Templates // site templates
} }
// NewConfig returns a new configuration. // NewConfig returns a new configuration.
@ -35,6 +36,8 @@ func (c *Config) Load(path string) error {
switch key { switch key {
case "title": case "title":
c.Title = value c.Title = value
case "url":
c.URL = value
} }
case "feeds": case "feeds":
c.Feeds[key] = value c.Feeds[key] = value
@ -47,6 +50,7 @@ func (c *Config) LoadTemplates(path string) error {
// Site contains site metadata passed to templates // Site contains site metadata passed to templates
type Site struct { type Site struct {
Title string Title string
URL string
} }
// Load templates // Load templates
@ -55,8 +59,10 @@ func (c *Config) LoadTemplates(path string) error {
"site": func() Site { "site": func() Site {
return Site{ return Site{
Title: c.Title, Title: c.Title,
URL: c.URL,
} }
}, },
}) })
c.Templates.LoadDefault()
return c.Templates.Load(path) return c.Templates.Load(path)
} }

11
dir.go
View file

@ -1,6 +1,7 @@
package main package main
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"os" "os"
pathpkg "path" pathpkg "path"
@ -109,22 +110,18 @@ func (d *Dir) manipulate(cfg *Config) error {
// Create feeds // Create feeds
if title, ok := cfg.Feeds[d.Path]; ok { if title, ok := cfg.Feeds[d.Path]; ok {
var b strings.Builder var b bytes.Buffer
feed := &Feed{ feed := &Feed{
Title: title, Title: title,
Path: d.Path, Path: d.Path,
Updated: time.Now(), Updated: time.Now(),
Entries: d.Pages, Entries: d.Pages,
} }
tmpl := cfg.Templates.FindTemplate(d.Path, "feed.gmi") tmpl := cfg.Templates.FindTemplate(d.Path, "atom.xml")
if err := tmpl.Execute(&b, feed); err != nil { if err := tmpl.Execute(&b, feed); err != nil {
return err return err
} }
d.Pages = append(d.Pages, &Page{ d.files[pathpkg.Join(d.Path, "atom.xml")] = b.Bytes()
Title: title,
Path: pathpkg.Join(d.Path, "feed"),
Content: b.String(),
})
} }
// Manipulate subdirectories // Manipulate subdirectories

View file

@ -1,4 +1,5 @@
title=Example Site title=Example Site
url=//example.com
[feeds] [feeds]
/blog/=Example Feed /blog/=Example Feed

View file

@ -2,4 +2,4 @@
Welcome to my example blog! Welcome to my example blog!
=> feed/ Feed => atom.xml Atom feed

View file

@ -1,7 +0,0 @@
# {{ .Title }}
Feed for {{ .Path }} on {{ site.Title }}.
Last updated on {{ .Updated.Format "2006-01-02" }}.
{{ range .Entries }}=> {{ .Path }} {{ .Date.Format "2006-01-02" }} {{ .Title }}
{{ end -}}

View file

@ -1,9 +0,0 @@
# {{ .Title }}
{{ if .Content }}{{ .Content }}{{ end }}
## Pages
{{ range .Pages }}=> {{ .Path }} {{ .Title }}
{{ end }}
## Directories
{{ range .Dirs }}=> {{ .Path }} {{ .Title }}
{{ end }}

View file

@ -1,4 +0,0 @@
# {{ .Title }}
{{ if not .Date.IsZero }}Posted on {{ .Date.Format "2006-01-02" }} on {{ site.Title }}.{{ end }}
{{ .Content }}

View file

@ -19,11 +19,6 @@ func NewTemplates() *Templates {
t := &Templates{ t := &Templates{
tmpls: map[string]*template.Template{}, tmpls: map[string]*template.Template{},
} }
// Load default templates
t.LoadTemplate("/index.gmi", index_gmi)
t.LoadTemplate("/page.gmi", page_gmi)
t.LoadTemplate("/feed.gmi", feed_gmi)
t.LoadTemplate("/output.html", output_html)
return t return t
} }
@ -32,6 +27,15 @@ func (t *Templates) Funcs(funcs template.FuncMap) {
t.funcs = funcs t.funcs = funcs
} }
// LoadDefault loads the default templates.
// Should be called after Funcs.
func (t *Templates) LoadDefault() {
t.LoadTemplate("/index.gmi", index_gmi)
t.LoadTemplate("/page.gmi", page_gmi)
t.LoadTemplate("/atom.xml", atom_xml)
t.LoadTemplate("/output.html", output_html)
}
// LoadTemplate loads a template from the provided path and content. // LoadTemplate loads a template from the provided path and content.
func (t *Templates) LoadTemplate(path string, content string) { func (t *Templates) LoadTemplate(path string, content string) {
tmpl := template.New(path) tmpl := template.New(path)
@ -76,26 +80,24 @@ func (t *Templates) FindTemplate(path string, tmpl string) *template.Template {
} }
// Default index template // Default index template
const index_gmi = `# Index of {{ .Path }} const index_gmi = `# {{ .Title }}
{{ if .Content }}
{{ range .Dirs }}=> {{ .Path }} {{ .Content }}{{ end }}
{{ if .Dirs }}{{ range .Dirs }}=> {{ .Path }}{{ if .Title }} {{ .Title }}{{ end }}
{{ end }}
{{ end -}} {{ end -}}
{{ range .Pages }}=> {{ .Path }} {{ range .Pages }}=> {{ .Path }} {{ if not .Date.IsZero -}}
{{.Date.Format "2006-01-02"}} {{end}}{{.Title}}
{{ end -}}` {{ end -}}`
// Default page template // Default page template
const page_gmi = `# {{ .Title }} const page_gmi = `# {{ .Title }}
{{- if not .Date.IsZero }}
Posted on {{ .Date.Format "2006-01-02" }}
{{- if site.Title }} on {{ site.Title }}{{ end }}{{ end }}
{{ .Content }}` {{ .Content }}`
// Default feed template
const feed_gmi = `# {{ .Title }}
Last updated at {{ .Updated.Format "2006-01-02" }}
{{ range .Entries }}=> {{ .Path }} {{ .Date.Format "2006-01-02" }} {{ .Title }}
{{ end -}}`
// Default template for html output // Default template for html output
const output_html = `<!DOCTYPE html> const output_html = `<!DOCTYPE html>
<meta charset="utf-8"> <meta charset="utf-8">
@ -103,3 +105,19 @@ const output_html = `<!DOCTYPE html>
<title>{{ .Title }}</title> <title>{{ .Title }}</title>
{{ .Content }}` {{ .Content }}`
// Default atom feed template
const atom_xml = `<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<id>{{ site.URL }}{{ .Path }}</id>
<title>{{ .Title }}</title>
<updated>{{ .Updated.Format "2006-01-02T15:04:05Z07:00" }}</updated>
<link href="{{ site.URL }}{{ .Path }}" rel="alternate">
{{ range .Entries }}<entry>
<id>{{ site.URL }}{{ .Path }}</id>
<title>{{ .Title }}</title>
<updated>{{ .Date.Format "2006-01-02T15:04:05Z07:00" }}</updated>
<link href="{{ site.URL }}{{ .Path }}" rel="alternate">
</entry>
{{ end -}}
</feed>`