1
0
mirror of https://git.sr.ht/~adnano/kiln synced 2024-11-08 10:09:18 +01:00

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

@ -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)

@ -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
}
}