From 396c176f119e89f957290de823600e03660610ce Mon Sep 17 00:00:00 2001 From: Ted Kornish Date: Wed, 18 Jan 2017 10:52:36 -0800 Subject: [PATCH] godoc: preserve ?m=... query string Right now, clicking around packages in the godoc web interface strips the URL of any query strings it may have, which makes traversing internal packages a clumsy experience (constantly have to re-add ?m=...). This revision preserves the ?m=... flag in links between packages by examining the current PageInfoMode and converting it to a query string. Change-Id: I4e28279d8cbf221bcc7d5bce8de04c90cc907678 Reviewed-on: https://go-review.googlesource.com/34982 Reviewed-by: Brad Fitzpatrick Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- cmd/godoc/godoc_test.go | 2 +- godoc/godoc.go | 5 +++++ godoc/server.go | 29 ++++++++++++++++++++++++++--- godoc/static/package.html | 4 ++-- godoc/static/static.go | 4 ++-- 5 files changed, 36 insertions(+), 8 deletions(-) diff --git a/cmd/godoc/godoc_test.go b/cmd/godoc/godoc_test.go index 8ab07f52..483b8643 100644 --- a/cmd/godoc/godoc_test.go +++ b/cmd/godoc/godoc_test.go @@ -252,7 +252,7 @@ func testWeb(t *testing.T, withIndex bool) { match: []string{ "Standard library", "Package fmt implements formatted I/O", - "internal/syscall", + "internal/syscall/?m=all", }, dontmatch: []string{ "cmd/gc", diff --git a/godoc/godoc.go b/godoc/godoc.go index 1063244a..d2deeec4 100644 --- a/godoc/godoc.go +++ b/godoc/godoc.go @@ -101,6 +101,9 @@ func (p *Presentation) initFuncMap() { // Number operation "multiply": multiply, + + // formatting of PageInfoMode query string + "modeQueryString": modeQueryString, } if p.URLForSrc != nil { p.funcMap["srcLink"] = p.URLForSrc @@ -442,6 +445,8 @@ type PageInfo struct { Err error // error or nil Share bool // show share button on examples + Mode PageInfoMode // display metadata from query string + // package info FSet *token.FileSet // nil if no package documentation PDoc *doc.Package // nil if no package documentation diff --git a/godoc/server.go b/godoc/server.go index 294610ed..104e75b6 100644 --- a/godoc/server.go +++ b/godoc/server.go @@ -55,7 +55,7 @@ func (s *handlerServer) registerWithMux(mux *http.ServeMux) { // set to the respective error but the error is not logged. // func (h *handlerServer) GetPageInfo(abspath, relpath string, mode PageInfoMode, goos, goarch string) *PageInfo { - info := &PageInfo{Dirname: abspath} + info := &PageInfo{Dirname: abspath, Mode: mode} // Restrict to the package files that would be used when building // the package on this system. This makes sure that if there are @@ -203,6 +203,7 @@ func (h *handlerServer) GetPageInfo(abspath, relpath string, mode PageInfoMode, timestamp = time.Now() } info.Dirs = dir.listing(true, func(path string) bool { return h.includePath(path, mode) }) + info.DirTime = timestamp info.DirFlat = mode&FlatDir != 0 @@ -320,6 +321,8 @@ func (h *handlerServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { type PageInfoMode uint const ( + PageInfoModeQueryString = "m" // query string where PageInfoMode is stored + NoFiltering PageInfoMode = 1 << iota // do not filter exports AllMethods // show all embedded methods ShowSource // show source code, do not extract documentation @@ -337,12 +340,32 @@ var modeNames = map[string]PageInfoMode{ "flat": FlatDir, } +// generate a query string for persisting PageInfoMode between pages. +func modeQueryString(mode PageInfoMode) string { + if modeNames := mode.names(); len(modeNames) > 0 { + return "?m=" + strings.Join(modeNames, ",") + } + return "" +} + +// alphabetically sorted names of active flags for a PageInfoMode. +func (m PageInfoMode) names() []string { + var names []string + for name, mode := range modeNames { + if m&mode != 0 { + names = append(names, name) + } + } + sort.Strings(names) + return names +} + // GetPageInfoMode computes the PageInfoMode flags by analyzing the request // URL form value "m". It is value is a comma-separated list of mode names // as defined by modeNames (e.g.: m=src,text). func (p *Presentation) GetPageInfoMode(r *http.Request) PageInfoMode { var mode PageInfoMode - for _, k := range strings.Split(r.FormValue("m"), ",") { + for _, k := range strings.Split(r.FormValue(PageInfoModeQueryString), ",") { if m, found := modeNames[strings.TrimSpace(k)]; found { mode |= m } @@ -519,7 +542,7 @@ func (p *Presentation) serveTextFile(w http.ResponseWriter, r *http.Request, abs return } - if r.FormValue("m") == "text" { + if r.FormValue(PageInfoModeQueryString) == "text" { p.ServeText(w, src) return } diff --git a/godoc/static/package.html b/godoc/static/package.html index 674196de..1d2d1e39 100644 --- a/godoc/static/package.html +++ b/godoc/static/package.html @@ -277,7 +277,7 @@ {{if .HasPkg}} - {{html .Path}} + {{html .Path}} {{html .Synopsis}} @@ -287,7 +287,7 @@ {{else}} - {{html .Name}} + {{html .Name}} {{html .Synopsis}} diff --git a/godoc/static/static.go b/godoc/static/static.go index b2e56f9d..5855c1db 100644 --- a/godoc/static/static.go +++ b/godoc/static/static.go @@ -1914,7 +1914,7 @@ function cgAddChild(tree, ul, cgn) { {{if .HasPkg}} - {{html .Path}} + {{html .Path}} {{html .Synopsis}} @@ -1924,7 +1924,7 @@ function cgAddChild(tree, ul, cgn) { {{else}} - {{html .Name}} + {{html .Name}} {{html .Synopsis}}