~adnano/kiln

9ffb7c0b72e0f4c527376e9a8716a573a1ab5f16 — Adnan Maolood 2 months ago 45d2f0e
page: Sort Dirs
1 files changed, 20 insertions(+), 15 deletions(-)

M page.go
M page.go => page.go +20 -15
@@ 286,34 286,39 @@ func (p *Page) writeTo(dstPath string, task *Task) error {

// sort sorts the directory's pages by weight, then date, then filepath.
func (p *Page) sort() {
	sort.Slice(p.Pages, func(i, j int) bool {
		pi, pj := p.Pages[i], p.Pages[j]
	sortPages(p.Pages)
	sortPages(p.Dirs)

	// Sort subdirectories
	for _, d := range p.Dirs {
		d.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(p.Pages, func(i, j int) bool {
		pi, pj := p.Pages[i], p.Pages[j]
	sort.SliceStable(pages, func(i, j int) bool {
		pi, pj := pages[i], 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]
	sort.SliceStable(pages, func(i, j int) bool {
		pi, pj := pages[i], pages[j]
		return pi.Weight < pj.Weight
	})

	for i := range p.Pages {
	for i := range pages {
		if i-1 >= 0 {
			p.Pages[i].Prev = p.Pages[i-1]
			pages[i].Prev = pages[i-1]
		}
		if i+1 < len(p.Pages) {
			p.Pages[i].Next = p.Pages[i+1]
		if i+1 < len(pages) {
			pages[i].Next = pages[i+1]
		}
	}

	// Sort subdirectories
	for _, d := range p.Dirs {
		d.sort()
	}
}

// execute runs a command.