Commit 3ca419e2 authored by Matthew Holt's avatar Matthew Holt

browse: Correct links when site defined with a path (fixes #1561)

parent 7d154353
...@@ -37,7 +37,7 @@ type Browse struct { ...@@ -37,7 +37,7 @@ type Browse struct {
// Config is a configuration for browsing in a particular path. // Config is a configuration for browsing in a particular path.
type Config struct { type Config struct {
PathScope string PathScope string // the base path the URL must match to enable browsing
Fs staticfiles.FileServer Fs staticfiles.FileServer
Variables interface{} Variables interface{}
Template *template.Template Template *template.Template
...@@ -78,10 +78,15 @@ type Listing struct { ...@@ -78,10 +78,15 @@ type Listing struct {
httpserver.Context httpserver.Context
} }
// BreadcrumbMap returns l.Path where every element is a map // Crumb represents part of a breadcrumb menu.
// of URLs and path segment names. type Crumb struct {
func (l Listing) BreadcrumbMap() map[string]string { Link, Text string
result := map[string]string{} }
// Breadcrumbs returns l.Path where every element maps
// the link to the text to display.
func (l Listing) Breadcrumbs() []Crumb {
var result []Crumb
if len(l.Path) == 0 { if len(l.Path) == 0 {
return result return result
...@@ -94,13 +99,12 @@ func (l Listing) BreadcrumbMap() map[string]string { ...@@ -94,13 +99,12 @@ func (l Listing) BreadcrumbMap() map[string]string {
} }
parts := strings.Split(lpath, "/") parts := strings.Split(lpath, "/")
for i, part := range parts { for i := range parts {
if i == 0 && part == "" { txt := parts[i]
// Leading slash (root) if i == 0 && parts[i] == "" {
result["/"] = "/" txt = "/"
continue
} }
result[strings.Join(parts[:i+1], "/")] = part result = append(result, Crumb{Link: strings.Repeat("../", len(parts)-i-1), Text: txt})
} }
return result return result
...@@ -247,12 +251,12 @@ func directoryListing(files []os.FileInfo, canGoUp bool, urlPath string, config ...@@ -247,12 +251,12 @@ func directoryListing(files []os.FileInfo, canGoUp bool, urlPath string, config
fileCount++ fileCount++
} }
url := url.URL{Path: "./" + name} // prepend with "./" to fix paths with ':' in the name
if config.Fs.IsHidden(f) { if config.Fs.IsHidden(f) {
continue continue
} }
url := url.URL{Path: "./" + name} // prepend with "./" to fix paths with ':' in the name
fileinfos = append(fileinfos, FileInfo{ fileinfos = append(fileinfos, FileInfo{
IsDir: f.IsDir(), IsDir: f.IsDir(),
Name: f.Name(), Name: f.Name(),
......
...@@ -323,7 +323,7 @@ footer { ...@@ -323,7 +323,7 @@ footer {
<header> <header>
<h1> <h1>
{{range $url, $name := .BreadcrumbMap}}<a href="{{html $url}}">{{html $name}}</a>{{if ne $url "/"}}/{{end}}{{end}} {{range $i, $crumb := .Breadcrumbs}}<a href="{{html $crumb.Link}}">{{html $crumb.Text}}</a>{{if ne $i 0}}/{{end}}{{end}}
</h1> </h1>
</header> </header>
<main> <main>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment