mirror of
https://git.sr.ht/~adnano/kiln
synced 2024-11-23 18:51:10 +00:00
dir: Combine handling of feeds and deprecated feeds
This commit is contained in:
parent
bad156cad3
commit
d300bc23c0
107
dir.go
107
dir.go
|
@ -18,13 +18,12 @@ import (
|
|||
|
||||
// Dir represents a directory.
|
||||
type Dir struct {
|
||||
Permalink string
|
||||
Pages []*Page
|
||||
Dirs []*Dir
|
||||
index *Page // The index page.
|
||||
feed []byte // Atom feed (deprecated)
|
||||
extraFiles map[string][]byte // Atom/RSS feeds
|
||||
path string // relative to the content dir
|
||||
Permalink string
|
||||
Pages []*Page
|
||||
Dirs []*Dir
|
||||
index *Page // The index page.
|
||||
feeds map[string][]byte // Atom/RSS/Custom feeds.
|
||||
path string // relative to the content dir
|
||||
}
|
||||
|
||||
// Page represents a page.
|
||||
|
@ -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,22 +198,43 @@ 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.
|
||||
}{
|
||||
Title: feed.Title,
|
||||
Permalink: d.Permalink,
|
||||
Updated: time.Now(),
|
||||
Pages: d.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
|
||||
|
@ -250,12 +242,11 @@ func (d Dir) buildFeed(cfg *Site, feed Feed) ([]byte, error) {
|
|||
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 {
|
||||
|
|
4
main.go
4
main.go
|
@ -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
25
site.go
|
@ -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,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue