Repository navigation
Expand file tree
/
Copy pathatom.go
More file actions
73 lines (60 loc) · 1.27 KB
/
Copy pathatom.go
File metadata and controls
73 lines (60 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package atom
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/gorilla/feeds"
"github.com/verless/verless/model"
)
const (
Key string = "atom"
filename string = "atom.xml"
)
func New(meta *model.Meta, outputDir string) *atom {
a := atom{
meta: meta,
feed: &feeds.Feed{
Title: meta.Title,
Link: &feeds.Link{Href: meta.Base},
Description: meta.Description,
Author: &feeds.Author{Name: meta.Author},
Updated: time.Time{},
Created: time.Now(),
Subtitle: meta.Subtitle,
},
outputDir: outputDir,
}
return &a
}
type atom struct {
meta *model.Meta
feed *feeds.Feed
outputDir string
}
func (a *atom) ProcessPage(page *model.Page) error {
if page.Hidden() {
return nil
}
canonical := fmt.Sprintf("%s%s/%s", a.meta.Base, page.Route, page.ID)
item := &feeds.Item{
Title: page.Title,
Link: &feeds.Link{Href: canonical},
Description: page.Description,
Id: canonical,
Created: page.Date,
}
a.feed.Add(item)
return nil
}
func (a *atom) PreWrite(_ *model.Site) error {
return nil
}
func (a *atom) PostWrite() error {
path := filepath.Join(a.outputDir, filename)
atomFile, err := os.Create(path)
if err != nil {
return err
}
return a.feed.WriteAtom(atomFile)
}