-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnav.go
81 lines (69 loc) · 1.56 KB
/
nav.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"log"
"sort"
"time"
)
type wikiNav struct {
Name string
URL string
ID string
IsDir bool
SubNav []wikiNav
Mod time.Time
ModStr string
Summary string
}
type nav struct {
Pages []string
Wikis []wikiNav
Tags TagIndex
Recents []wikiNav
}
type navFunc func(storage) nav
type byModTime []wikiNav
func (m byModTime) Len() int { return len(m) }
func (m byModTime) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
func (m byModTime) Less(i, j int) bool { return m[i].Mod.Before(m[j].Mod) }
func contains(target string, in []string) bool {
for _, d := range in {
if target == d {
return true
}
}
return false
}
func flattenWikis(current []wikiNav) []wikiNav {
var newList []wikiNav
for _, v := range current {
if v.IsDir {
newList = append(newList, flattenWikis(v.SubNav)...)
} else {
newList = append(newList, v)
}
}
return newList
}
func genRecents(current []wikiNav) []wikiNav {
var newList []wikiNav
newList = flattenWikis(current)
sort.Sort(sort.Reverse(byModTime(newList)))
return newList
}
func getNav(s storage) nav {
start := time.Now()
wikis := s.IndexWikiFiles("", wikiDir)
loadwikis := time.Now()
tags := s.IndexTags(tagDir)
loadtags := time.Now()
indexedTags := s.IndexRawFiles(wikiDir, "PDF", tags)
indexTags := time.Now()
log.Printf("[nav] wikis %v", loadwikis.Sub(start))
log.Printf("[nav] tags %v", loadtags.Sub(loadwikis))
log.Printf("[nav] idxtags %v", indexTags.Sub(loadtags))
return nav{
Wikis: wikis,
Tags: indexedTags,
Recents: genRecents(wikis),
}
}