dir: Combine handling of feeds and deprecated feeds

This commit is contained in:
adnano 2021-09-03 00:22:07 -04:00
parent bad156cad3
commit d300bc23c0
3 changed files with 69 additions and 67 deletions

89
dir.go
View file

@ -22,8 +22,7 @@ type Dir struct {
Pages []*Page
Dirs []*Dir
index *Page // The index page.
feed []byte // Atom feed (deprecated)
extraFiles map[string][]byte // Atom/RSS feeds
feeds map[string][]byte // Atom/RSS/Custom feeds.
path string // relative to the content dir
}
@ -159,7 +158,7 @@ func (d *Dir) process(cfg *Site, task *Task) error {
if err != nil {
return err
}
d.addExtraFile(feed.Output, b)
d.addFeed(feed.Output, b)
}
if task.TemplateExt != "" {
@ -188,34 +187,6 @@ func (d *Dir) process(cfg *Site, task *Task) error {
}
}
// Feed represents a feed.
type Feed struct {
Title string // Feed title.
Permalink string // Feed permalink.
Updated time.Time // Last updated time.
Entries []*Page // Feed entries.
}
// Create feeds
if title, ok := cfg.Feeds[d.Permalink]; ok {
var b bytes.Buffer
feed := &Feed{
Title: title,
Permalink: d.Permalink,
Updated: time.Now(),
Entries: d.Pages,
}
tmpl, ok := cfg.templates.FindTemplate(d.Permalink, "atom.xml")
if ok {
if err := tmpl.Execute(&b, feed); err != nil {
return err
}
d.feed = b.Bytes()
} else {
fmt.Printf("Warning: failed to generate feed %q: missing template \"atom.xml\"\n", title)
}
}
// Process subdirectories
for _, d := range d.Dirs {
if err := d.process(cfg, task); err != nil {
@ -227,35 +198,55 @@ func (d *Dir) process(cfg *Site, task *Task) error {
// buildFeed build the feed of the directory
func (d Dir) buildFeed(cfg *Site, feed Feed) ([]byte, error) {
// Feed represents a feed.
type Feed struct {
Title string
Permalink string
Updated time.Time
Pages []*Page
}
// DeprecatedFeed represents a deprecated feed.
type DeprecatedFeed struct {
Title string
Permalink string
Updated time.Time
Entries []*Page
}
tmpl, ok := cfg.templates.FindTemplate(d.Permalink, feed.Template)
if !ok {
return nil, fmt.Errorf("missing feed template %q to generate %q", feed.Template, feed.Title)
return nil, fmt.Errorf("failed to generate feed %q: missing feed template %q", feed.Title, feed.Template)
}
var b bytes.Buffer
data := struct {
Title string // Feed title.
Permalink string // Feed permalink.
Updated time.Time // Last updated time.
Pages []*Page // Feed pages.
}{
var data interface{}
if !feed.deprecated {
data = Feed{
Title: feed.Title,
Permalink: d.Permalink,
Updated: time.Now(),
Pages: d.Pages,
}
} else {
data = DeprecatedFeed{
Title: feed.Title,
Permalink: d.Permalink,
Updated: time.Now(),
Entries: d.Pages,
}
}
if err := tmpl.Execute(&b, data); err != nil {
return nil, err
}
return b.Bytes(), nil
}
func (d *Dir) addExtraFile(name string, content []byte) {
if d.extraFiles == nil {
d.extraFiles = map[string][]byte{name: content}
} else {
d.extraFiles[name] = content
func (d *Dir) addFeed(name string, content []byte) {
if d.feeds == nil {
d.feeds = map[string][]byte{}
}
d.feeds[name] = content
}
// write writes the directory's contents to the provided destination path.
@ -289,18 +280,8 @@ func (d *Dir) write(dstDir string, task *Task) error {
}
}
// Write the atom feed
if d.feed != nil {
const path = "atom.xml"
dstPath := pathpkg.Join(dirPath, path)
os.MkdirAll(dirPath, 0755)
if err := os.WriteFile(dstPath, d.feed, 0644); err != nil {
return err
}
}
// Write feeds
for name, content := range d.extraFiles {
for name, content := range d.feeds {
dstPath := pathpkg.Join(dstDir, name)
os.MkdirAll(dirPath, 0755)
if err := os.WriteFile(dstPath, content, 0644); err != nil {

View file

@ -51,11 +51,11 @@ func build() {
site, err := LoadSite(config)
if err != nil {
log.Fatal(err)
log.Fatalf("ERROR: %v", err)
}
if err := site.run(); err != nil {
log.Fatal(err)
log.Fatalf("ERROR: %v", err)
}
}

25
site.go
View file

@ -16,7 +16,7 @@ type Site struct {
Title string `toml:"title"`
URLs []string `toml:"urls"`
Tasks []*Task `toml:"tasks"`
Feeds map[string]string `toml:"feeds"`
Feeds map[string]string `toml:"feeds"` // Deprecated. Use Task.Feeds instead
Params map[string]string `toml:"params"`
Permalinks map[string]string `toml:"permalinks"`
permalinks map[string]*template.Template
@ -43,6 +43,9 @@ type Feed struct {
Title string `toml:"title"`
Template string `toml:"template"`
Output string `toml:"output"`
// if true, the feed was specified using deprecated configuration options.
deprecated bool
}
func (t *Task) Match(ext string) bool {
@ -96,7 +99,25 @@ func LoadSite(config string) (*Site, error) {
for _, task := range site.Tasks {
task.feeds = map[string][]Feed{}
for _, feed := range task.Feeds {
task.feeds[feed.InputDir] = append(task.feeds[feed.InputDir], feed)
dir := feed.InputDir
task.feeds[dir] = append(task.feeds[dir], feed)
}
}
// Populate task feeds map with deprecated feeds
for dir, title := range site.Feeds {
// Deprecated feeds apply to every task
for _, task := range site.Tasks {
if _, ok := task.feeds[dir]; !ok {
dir = strings.TrimSuffix(dir, "/")
task.feeds[dir] = append(task.feeds[dir], Feed{
InputDir: dir,
Title: title,
Template: "atom.xml",
Output: path.Join(dir, "atom.xml"),
deprecated: true,
})
}
}
}