-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpages_handler.go
82 lines (66 loc) · 1.6 KB
/
pages_handler.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
82
package main
import (
"github.com/gorilla/mux"
"gopkg.in/src-d/go-git.v4/plumbing"
"net/http"
"path"
"strings"
)
func createBreadcrumb(mux *mux.Router, folderPath string) (hierarchy []*Folder, err error) {
currentPrefix := "/"
linkpath, err := mux.Get("pages#index").URL("folder", "")
if err != nil {
panic(err)
}
hierarchy = append(hierarchy, &Folder{
Title: "Home",
Linkpath: linkpath.String(),
})
if folderPath == "" {
return
}
for _, folder := range strings.Split(folderPath, "/") {
linkpath, err = mux.Get("pages#index").URL("folder", path.Join(currentPrefix, folder))
if err != nil {
panic(err)
}
hierarchy = append(hierarchy, &Folder{
Title: folder,
Linkpath: linkpath.String(),
})
currentPrefix = path.Join(currentPrefix, folder)
}
return
}
func (b *Bilbo) HandlePages(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
folder := ""
if len(vars["folder"]) > 0 {
folder = vars["folder"]
}
commit := r.Context().Value("GitHead").(plumbing.Hash)
folderStructure, err := b.getPagesAtCommit(folder, commit)
if err != nil {
panic(err)
}
pageTitle := path.Clean(folder)
if pageTitle == "." {
pageTitle = "all pages"
}
breadcrumb, err := createBreadcrumb(b.mux, folder)
if err != nil {
panic(err)
}
pageFolder := path.Clean(path.Join("/", folder))
if pageFolder == "/" {
pageFolder = ""
}
b.renderTemplate(w, r, "pages.html", hash{
"breadcrumb": breadcrumb,
"directories": folderStructure.Folders,
"pageFolder": pageFolder,
"pageLayout": "pages",
"pages": folderStructure.Pages,
"pageTitle": pageTitle,
})
}