hswg: try to order entries by date, reverse order
This commit is contained in:
		
							
								
								
									
										39
									
								
								hswg/main.go
									
									
									
									
									
								
							
							
						
						
									
										39
									
								
								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)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user