hswg: try to order entries by date, reverse order

This commit is contained in:
Přemysl Eric Janouch 2020-09-21 22:43:46 +02:00
parent 8f542c7120
commit 35974efe21
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 38 additions and 1 deletions

View File

@ -138,6 +138,19 @@ type Entry struct {
backlinks []string // what documents link back here 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(`\.[^/.]*$`) var extRE = regexp.MustCompile(`\.[^/.]*$`)
func stripExtension(path string) string { func stripExtension(path string) string {
@ -286,6 +299,30 @@ func main() {
e.Metadata.LastUpdated, e.PathSource)) 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. // Execute a template from the standard input.
var input []byte var input []byte
if input, err = ioutil.ReadAll(os.Stdin); err != nil { if input, err = ioutil.ReadAll(os.Stdin); err != nil {
@ -294,7 +331,7 @@ func main() {
// TODO(p): Splitting content to categories would be nice. // TODO(p): Splitting content to categories would be nice.
t, err := template.New("-").Parse(string(input)) 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) log.Fatalln(err)
} }
} }