From 35974efe21e2ce9974d882cf70b9e4feebfebb8a Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch Date: Mon, 21 Sep 2020 22:43:46 +0200 Subject: hswg: try to order entries by date, reverse order --- hswg/main.go | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) 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) } } -- cgit v1.2.3-70-g09d2