Sort pages by date

This commit is contained in:
adnano 2020-09-29 11:22:54 -04:00
parent 98dc3d769e
commit 772b801eea
2 changed files with 22 additions and 4 deletions

21
kiln.go
View file

@ -6,6 +6,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"sort"
"strings" "strings"
"text/template" "text/template"
"time" "time"
@ -40,7 +41,7 @@ func LoadSite(srcDir string) (*Site, error) {
} }
// Write writes the contents of the Index to the provided destination directory. // Write writes the contents of the Index to the provided destination directory.
func (s *Site) Write(dstDir string) error { func (s *Site) Write(dstDir string, format OutputFormat) error {
// Empty the destination directory // Empty the destination directory
if err := os.RemoveAll(dstDir); err != nil { if err := os.RemoveAll(dstDir); err != nil {
return err return err
@ -73,7 +74,7 @@ func (s *Site) Write(dstDir string) error {
} }
// Write the directory // Write the directory
return s.Directory.Write(dstDir, OutputGemini) return s.Directory.Write(dstDir, format)
} }
// Manipulate processes and manipulates the site's content. // Manipulate processes and manipulates the site's content.
@ -114,6 +115,11 @@ func (s *Site) Manipulate(dir *Directory) error {
return nil return nil
} }
// Sort sorts the site's pages by date.
func (s *Site) Sort() {
s.Directory.Sort()
}
// Page represents a page. // Page represents a page.
type Page struct { type Page struct {
// The path to this page. // The path to this page.
@ -296,6 +302,17 @@ func (d *Directory) Write(dstDir string, format OutputFormat) error {
return nil return nil
} }
// Sort sorts the directory's pages by date.
func (d *Directory) Sort() {
sort.Slice(d.Pages, func(i, j int) bool {
return d.Pages[i].Date.After(d.Pages[j].Date)
})
// Sort subdirectories
for _, d := range d.Directories {
d.Sort()
}
}
// OutputFormat represents an output format. // OutputFormat represents an output format.
type OutputFormat func(*Page) (path string, content []byte) type OutputFormat func(*Page) (path string, content []byte)

View file

@ -35,14 +35,15 @@ func build() error {
if err != nil { if err != nil {
return err return err
} }
site.Sort()
if err := site.Manipulate(site.Directory); err != nil { if err := site.Manipulate(site.Directory); err != nil {
return err return err
} }
if err := site.Write("dst"); err != nil { if err := site.Write("dst", OutputGemini); err != nil {
return err return err
} }
if toHtml { if toHtml {
if err := site.Directory.Write("dst.html", OutputHTML); err != nil { if err := site.Write("dst.html", OutputHTML); err != nil {
return err return err
} }
} }