From 772b801eeaecd9ce734eceddecfb0cdf1eeac5de Mon Sep 17 00:00:00 2001 From: adnano Date: Tue, 29 Sep 2020 11:22:54 -0400 Subject: [PATCH] Sort pages by date --- kiln.go | 21 +++++++++++++++++++-- main.go | 5 +++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/kiln.go b/kiln.go index 6e82a69..2390d12 100644 --- a/kiln.go +++ b/kiln.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" "regexp" + "sort" "strings" "text/template" "time" @@ -40,7 +41,7 @@ func LoadSite(srcDir string) (*Site, error) { } // 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 if err := os.RemoveAll(dstDir); err != nil { return err @@ -73,7 +74,7 @@ func (s *Site) Write(dstDir string) error { } // Write the directory - return s.Directory.Write(dstDir, OutputGemini) + return s.Directory.Write(dstDir, format) } // Manipulate processes and manipulates the site's content. @@ -114,6 +115,11 @@ func (s *Site) Manipulate(dir *Directory) error { return nil } +// Sort sorts the site's pages by date. +func (s *Site) Sort() { + s.Directory.Sort() +} + // Page represents a page. type Page struct { // The path to this page. @@ -296,6 +302,17 @@ func (d *Directory) Write(dstDir string, format OutputFormat) error { 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. type OutputFormat func(*Page) (path string, content []byte) diff --git a/main.go b/main.go index b80758c..228edad 100644 --- a/main.go +++ b/main.go @@ -35,14 +35,15 @@ func build() error { if err != nil { return err } + site.Sort() if err := site.Manipulate(site.Directory); err != nil { return err } - if err := site.Write("dst"); err != nil { + if err := site.Write("dst", OutputGemini); err != nil { return err } if toHtml { - if err := site.Directory.Write("dst.html", OutputHTML); err != nil { + if err := site.Write("dst.html", OutputHTML); err != nil { return err } }