mirror of
https://git.sr.ht/~adnano/kiln
synced 2024-11-23 18:51:10 +00:00
Sort pages by date
This commit is contained in:
parent
98dc3d769e
commit
772b801eea
21
kiln.go
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)
|
||||
|
||||
|
|
5
main.go
5
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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue