diff options
author | Přemysl Eric Janouch <p@janouch.name> | 2020-09-21 22:43:46 +0200 |
---|---|---|
committer | Přemysl Eric Janouch <p@janouch.name> | 2020-09-21 22:44:12 +0200 |
commit | 35974efe21e2ce9974d882cf70b9e4feebfebb8a (patch) | |
tree | 4d012037b337a00adff84917953694e57311548c /hswg/main.go | |
parent | 8f542c71208b1944979b2e50ad1a328aab98f230 (diff) | |
download | haven-35974efe21e2ce9974d882cf70b9e4feebfebb8a.tar.gz haven-35974efe21e2ce9974d882cf70b9e4feebfebb8a.tar.xz haven-35974efe21e2ce9974d882cf70b9e4feebfebb8a.zip |
hswg: try to order entries by date, reverse order
Diffstat (limited to 'hswg/main.go')
-rw-r--r-- | hswg/main.go | 39 |
1 files changed, 38 insertions, 1 deletions
diff --git a/hswg/main.go b/hswg/main.go index fef3278..21ca569 100644 --- a/hswg/main.go +++ b/hswg/main.go @@ -138,6 +138,19 @@ type Entry struct { backlinks []string // what documents link back here } +// Published returns the date when the entry was published, or nil if unknown. +func (e *Entry) Published() *time.Time { + if d, ok := e.Metadata.Attributes.GetAsString("date"); !ok { + return nil + } else if t, err := time.Parse(time.RFC3339, d); err == nil { + return &t + } else if t, err := time.Parse("2006-01-02", d); err == nil { + return &t + } else { + return nil + } +} + var extRE = regexp.MustCompile(`\.[^/.]*$`) func stripExtension(path string) string { @@ -286,6 +299,30 @@ func main() { e.Metadata.LastUpdated, e.PathSource)) } + // Reorder entries reversely, primarily by date, secondarily by filename. + ordered := []*Entry{} + for _, e := range entries { + ordered = append(ordered, e) + } + + sort.Slice(ordered, func(i, j int) bool { + a, b := ordered[i], ordered[j] + p1, p2 := a.Published(), b.Published() + if p1 == nil && p2 != nil { + return true + } + if p1 == nil && p2 == nil { + return a.PathSource > b.PathSource + } + if p2 == nil { + return false + } + if p1.Equal(*p2) { + return a.PathSource > b.PathSource + } + return p2.Before(*p1) + }) + // Execute a template from the standard input. var input []byte if input, err = ioutil.ReadAll(os.Stdin); err != nil { @@ -294,7 +331,7 @@ func main() { // TODO(p): Splitting content to categories would be nice. t, err := template.New("-").Parse(string(input)) - if err = t.Execute(os.Stdout, entries); err != nil { + if err = t.Execute(os.Stdout, ordered); err != nil { log.Fatalln(err) } } |