diff --git a/cmd/godoc/godoc_test.go b/cmd/godoc/godoc_test.go index a228fae2..d54a6d95 100644 --- a/cmd/godoc/godoc_test.go +++ b/cmd/godoc/godoc_test.go @@ -266,6 +266,18 @@ func testWeb(t *testing.T, withIndex bool) { }, needIndex: true, }, + { + path: "/pkg/strings/", + match: []string{ + `href="/src/strings/strings.go"`, + }, + }, + { + path: "/cmd/compile/internal/amd64/", + match: []string{ + `href="/src/cmd/compile/internal/amd64/reg.go"`, + }, + }, } for _, test := range tests { if test.needIndex && !withIndex { diff --git a/godoc/pres.go b/godoc/pres.go index a8f8b2bc..11e0dd3e 100644 --- a/godoc/pres.go +++ b/godoc/pres.go @@ -119,14 +119,15 @@ func NewPresentation(c *Corpus) *Presentation { p: p, c: c, pattern: "/cmd/", - fsRoot: "/src/cmd", + fsRoot: "/src", } p.pkgHandler = handlerServer{ - p: p, - c: c, - pattern: "/pkg/", - fsRoot: "/src", - exclude: []string{"/src/cmd"}, + p: p, + c: c, + pattern: "/pkg/", + stripPrefix: "pkg/", + fsRoot: "/src", + exclude: []string{"/src/cmd"}, } p.cmdHandler.registerWithMux(p.mux) p.pkgHandler.registerWithMux(p.mux) diff --git a/godoc/server.go b/godoc/server.go index bbc3409c..62967637 100644 --- a/godoc/server.go +++ b/godoc/server.go @@ -34,11 +34,12 @@ import ( // handlerServer is a migration from an old godoc http Handler type. // This should probably merge into something else. type handlerServer struct { - p *Presentation - c *Corpus // copy of p.Corpus - pattern string // url pattern; e.g. "/pkg/" - fsRoot string // file system root to which the pattern is mapped; e.g. "/src" - exclude []string // file system paths to exclude; e.g. "/src/cmd" + p *Presentation + c *Corpus // copy of p.Corpus + pattern string // url pattern; e.g. "/pkg/" + stripPrefix string // prefix to strip from import path; e.g. "pkg/" + fsRoot string // file system root to which the pattern is mapped; e.g. "/src" + exclude []string // file system paths to exclude; e.g. "/src/cmd" } func (s *handlerServer) registerWithMux(mux *http.ServeMux) { @@ -235,7 +236,7 @@ func (h *handlerServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - relpath := pathpkg.Clean(r.URL.Path[len(h.pattern):]) + relpath := pathpkg.Clean(r.URL.Path[len(h.stripPrefix)+1:]) abspath := pathpkg.Join(h.fsRoot, relpath) mode := h.p.GetPageInfoMode(r) if relpath == builtinPkgPath {