xonecas.io
8356
Wx

www-xonecas-io / files

www-xonecas-io / internal / site / sidebar.go

1.0 KB
1package site
2
3import (
4	"path"
5	"strings"
6)
7
8// SideEntry is one line of the file browser's sidebar, indented by its depth.
9type SideEntry struct {
10	TreeEntry
11	Depth int
12	Here  bool // the page being shown
13	Open  bool // a folder whose entries follow
14}
15
16// Children is every folder's entries in t, keyed by folder ("" for the root).
17func Children(name string, t Tree) map[string][]TreeEntry {
18	kids := map[string][]TreeEntry{"": entries(name, "", t)}
19	for d := range t.Dirs {
20		kids[d] = entries(name, d, t)
21	}
22	return kids
23}
24
25// sidebar is the root's entries, with every folder on the way to path p opened.
26func sidebar(kids map[string][]TreeEntry, p string) []SideEntry {
27	var list []SideEntry
28	var walk func(dir string, depth int)
29	walk = func(dir string, depth int) {
30		for _, e := range kids[dir] {
31			q := path.Join(dir, e.Name)
32			open := e.Dir && (q == p || strings.HasPrefix(p, q+"/"))
33			list = append(list, SideEntry{e, depth, q == p, open})
34			if open {
35				walk(q, depth+1)
36			}
37		}
38	}
39	walk("", 0)
40	return list
41}