From aeea076d423bf1988fb5c806d92de456e6d64041 Mon Sep 17 00:00:00 2001 From: Adnan Maolood Date: Sun, 22 Nov 2020 15:14:50 -0500 Subject: [PATCH] Implement atom feeds --- config.go | 12 ++++++--- dir.go | 11 +++----- example/config.ini | 1 + example/src/blog/index.gmi | 2 +- example/templates/feed.gmi | 7 ----- example/templates/index.gmi | 9 ------- example/templates/page.gmi | 4 --- templates.go | 52 +++++++++++++++++++++++++------------ 8 files changed, 50 insertions(+), 48 deletions(-) delete mode 100644 example/templates/feed.gmi delete mode 100644 example/templates/index.gmi delete mode 100644 example/templates/page.gmi diff --git a/config.go b/config.go index fe1ca02..cd0791e 100644 --- a/config.go +++ b/config.go @@ -9,9 +9,10 @@ import ( // Config contains site configuration. type Config struct { - Title string - Feeds map[string]string - Templates *Templates + Title string // site title + URL string // site URL + Feeds map[string]string // site feeds + Templates *Templates // site templates } // NewConfig returns a new configuration. @@ -35,6 +36,8 @@ func (c *Config) Load(path string) error { switch key { case "title": c.Title = value + case "url": + c.URL = value } case "feeds": c.Feeds[key] = value @@ -47,6 +50,7 @@ func (c *Config) LoadTemplates(path string) error { // Site contains site metadata passed to templates type Site struct { Title string + URL string } // Load templates @@ -55,8 +59,10 @@ func (c *Config) LoadTemplates(path string) error { "site": func() Site { return Site{ Title: c.Title, + URL: c.URL, } }, }) + c.Templates.LoadDefault() return c.Templates.Load(path) } diff --git a/dir.go b/dir.go index b4ab358..4d5319e 100644 --- a/dir.go +++ b/dir.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "io/ioutil" "os" pathpkg "path" @@ -109,22 +110,18 @@ func (d *Dir) manipulate(cfg *Config) error { // Create feeds if title, ok := cfg.Feeds[d.Path]; ok { - var b strings.Builder + var b bytes.Buffer feed := &Feed{ Title: title, Path: d.Path, Updated: time.Now(), 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 { return err } - d.Pages = append(d.Pages, &Page{ - Title: title, - Path: pathpkg.Join(d.Path, "feed"), - Content: b.String(), - }) + d.files[pathpkg.Join(d.Path, "atom.xml")] = b.Bytes() } // Manipulate subdirectories diff --git a/example/config.ini b/example/config.ini index 10a7f54..79c0e5f 100644 --- a/example/config.ini +++ b/example/config.ini @@ -1,4 +1,5 @@ title=Example Site +url=//example.com [feeds] /blog/=Example Feed diff --git a/example/src/blog/index.gmi b/example/src/blog/index.gmi index 80cff14..30e1cad 100644 --- a/example/src/blog/index.gmi +++ b/example/src/blog/index.gmi @@ -2,4 +2,4 @@ Welcome to my example blog! -=> feed/ Feed +=> atom.xml Atom feed diff --git a/example/templates/feed.gmi b/example/templates/feed.gmi deleted file mode 100644 index 00f9be4..0000000 --- a/example/templates/feed.gmi +++ /dev/null @@ -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 -}} diff --git a/example/templates/index.gmi b/example/templates/index.gmi deleted file mode 100644 index 68f1df5..0000000 --- a/example/templates/index.gmi +++ /dev/null @@ -1,9 +0,0 @@ -# {{ .Title }} - -{{ if .Content }}{{ .Content }}{{ end }} -## Pages -{{ range .Pages }}=> {{ .Path }} {{ .Title }} -{{ end }} -## Directories -{{ range .Dirs }}=> {{ .Path }} {{ .Title }} -{{ end }} diff --git a/example/templates/page.gmi b/example/templates/page.gmi deleted file mode 100644 index f6bf7da..0000000 --- a/example/templates/page.gmi +++ /dev/null @@ -1,4 +0,0 @@ -# {{ .Title }} -{{ if not .Date.IsZero }}Posted on {{ .Date.Format "2006-01-02" }} on {{ site.Title }}.{{ end }} - -{{ .Content }} diff --git a/templates.go b/templates.go index a4022e2..67bf2d3 100644 --- a/templates.go +++ b/templates.go @@ -19,11 +19,6 @@ func NewTemplates() *Templates { t := &Templates{ 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 } @@ -32,6 +27,15 @@ func (t *Templates) Funcs(funcs template.FuncMap) { 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. func (t *Templates) LoadTemplate(path string, content string) { tmpl := template.New(path) @@ -76,26 +80,24 @@ func (t *Templates) FindTemplate(path string, tmpl string) *template.Template { } // Default index template -const index_gmi = `# Index of {{ .Path }} - -{{ range .Dirs }}=> {{ .Path }} +const index_gmi = `# {{ .Title }} +{{ if .Content }} +{{ .Content }}{{ end }} +{{ if .Dirs }}{{ range .Dirs }}=> {{ .Path }}{{ if .Title }} {{ .Title }}{{ end }} +{{ end }} {{ end -}} -{{ range .Pages }}=> {{ .Path }} +{{ range .Pages }}=> {{ .Path }} {{ if not .Date.IsZero -}} +{{.Date.Format "2006-01-02"}} {{end}}{{.Title}} {{ end -}}` // Default page template const page_gmi = `# {{ .Title }} +{{- if not .Date.IsZero }} +Posted on {{ .Date.Format "2006-01-02" }} +{{- if site.Title }} on {{ site.Title }}{{ end }}{{ end }} {{ .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 const output_html = ` @@ -103,3 +105,19 @@ const output_html = ` {{ .Title }} {{ .Content }}` + +// Default atom feed template +const atom_xml = ` + +{{ site.URL }}{{ .Path }} +{{ .Title }} +{{ .Updated.Format "2006-01-02T15:04:05Z07:00" }} + +{{ range .Entries }} + {{ site.URL }}{{ .Path }} + {{ .Title }} + {{ .Date.Format "2006-01-02T15:04:05Z07:00" }} + + +{{ end -}} +`