page: Sort Dirs

This commit is contained in:
adnano 2021-10-02 18:08:01 -04:00
parent 250ff9131f
commit 5f16ca42d5

51
page.go
View file

@ -286,29 +286,8 @@ func (p *Page) writeTo(dstPath string, task *Task) error {
// sort sorts the directory's pages by weight, then date, then filepath. // sort sorts the directory's pages by weight, then date, then filepath.
func (p *Page) sort() { func (p *Page) sort() {
sort.Slice(p.Pages, func(i, j int) bool { sortPages(p.Pages)
pi, pj := p.Pages[i], p.Pages[j] sortPages(p.Dirs)
return pi.FilePath < pj.FilePath
})
sort.SliceStable(p.Pages, func(i, j int) bool {
pi, pj := p.Pages[i], p.Pages[j]
return pi.Date.After(pj.Date)
})
sort.SliceStable(p.Pages, func(i, j int) bool {
pi, pj := p.Pages[i], p.Pages[j]
return pi.Weight < pj.Weight
})
for i := range p.Pages {
if i-1 >= 0 {
p.Pages[i].Prev = p.Pages[i-1]
}
if i+1 < len(p.Pages) {
p.Pages[i].Next = p.Pages[i+1]
}
}
// Sort subdirectories // Sort subdirectories
for _, d := range p.Dirs { for _, d := range p.Dirs {
@ -316,6 +295,32 @@ func (p *Page) sort() {
} }
} }
func sortPages(pages []*Page) {
sort.Slice(pages, func(i, j int) bool {
pi, pj := pages[i], pages[j]
return pi.FilePath < pj.FilePath
})
sort.SliceStable(pages, func(i, j int) bool {
pi, pj := pages[i], pages[j]
return pi.Date.After(pj.Date)
})
sort.SliceStable(pages, func(i, j int) bool {
pi, pj := pages[i], pages[j]
return pi.Weight < pj.Weight
})
for i := range pages {
if i-1 >= 0 {
pages[i].Prev = pages[i-1]
}
if i+1 < len(pages) {
pages[i].Next = pages[i+1]
}
}
}
// execute runs a command. // execute runs a command.
func execute(command string, input io.Reader, output io.Writer) error { func execute(command string, input io.Reader, output io.Writer) error {
split := strings.Split(command, " ") split := strings.Split(command, " ")