1
0
Fork 0
mirror of https://git.sr.ht/~adnano/kiln synced 2024-05-14 20:46:05 +02:00

Implement atom feeds

This commit is contained in:
Adnan Maolood 2020-11-22 15:14:50 -05:00
parent 832aed7ff2
commit aeea076d42
8 changed files with 50 additions and 48 deletions

View File

@ -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)
}

11
dir.go
View File

@ -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

View File

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

View File

@ -2,4 +2,4 @@
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{
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 = `<!DOCTYPE html>
<meta charset="utf-8">
@ -103,3 +105,19 @@ const output_html = `<!DOCTYPE html>
<title>{{ .Title }}</title>
{{ .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>`