From: Robert Griesemer Date: Fri, 3 Apr 2009 04:59:37 +0000 (-0700) Subject: - moved functions before types in doc output (per rsc) X-Git-Tag: weekly.2009-11-06~1898 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=91238c5bfe29d71f6b605e2102f663d80084277d;p=gostls13.git - moved functions before types in doc output (per rsc) - use /src and /doc prefix in URL to distinguish output type (per rsc) - fixed a bug in an internal string function - ignore files ending in _test.go (consider them test files) R=rsc OCL=27054 CL=27054 --- diff --git a/usr/gri/pretty/docprinter.go b/usr/gri/pretty/docprinter.go index a5f6b4b44b..a901bdee08 100644 --- a/usr/gri/pretty/docprinter.go +++ b/usr/gri/pretty/docprinter.go @@ -440,12 +440,6 @@ func (doc *PackageDoc) Print(writer io.Write) { } } - // types - for name, t := range doc.types { - fmt.Fprintln(writer, "
"); - t.print(&p); - } - // variables if doc.vars.Len() > 0 { fmt.Fprintln(writer, "
"); @@ -462,4 +456,10 @@ func (doc *PackageDoc) Print(writer io.Write) { f.print(&p, 2); } } + + // types + for name, t := range doc.types { + fmt.Fprintln(writer, "
"); + t.print(&p); + } } diff --git a/usr/gri/pretty/godoc.go b/usr/gri/pretty/godoc.go index 143fd09611..947b42cdaa 100644 --- a/usr/gri/pretty/godoc.go +++ b/usr/gri/pretty/godoc.go @@ -32,6 +32,13 @@ import ( // TODO // - uniform use of path, filename, dirname, pakname, etc. +// - fix weirdness with double-/'s in paths + + +const ( + docPrefix = "/doc/"; + srcPrefix = "/src/"; +) func getenv(varname string) string { @@ -81,27 +88,31 @@ func cleanPath(s string) string { // strip any trailing '/' (may result in the empty string). func sanitizePath(s string) string { s = cleanPath(s); - if s[len(s)-1] == '/' { // strip trailing '/' + if len(s) > 0 && s[len(s)-1] == '/' { // strip trailing '/' s = s[0 : len(s)-1]; } return s; } -func contains(s, sub string, pos int) bool { - end := pos + len(sub); - return pos >= 0 && end <= len(s) && s[pos : end] == sub; +func hasPrefix(s, prefix string) bool { + return len(prefix) <= len(s) && s[0 : len(prefix)] == prefix; +} + + +func hasPostfix(s, postfix string) bool { + pos := len(s) - len(postfix); + return pos >= 0 && s[pos : len(s)] == postfix; } func isGoFile(dir *os.Dir) bool { - const ext = ".go"; - return dir.IsRegular() && contains(dir.Name, ext, len(dir.Name) - len(ext)); + return dir.IsRegular() && hasPostfix(dir.Name, ".go"); } func printLink(c *http.Conn, path, name string) { - fmt.Fprintf(c, "%s
\n", path + name, name); + fmt.Fprintf(c, "%s
\n", srcPrefix + path + name, name); } @@ -360,7 +371,7 @@ func serveGoFile(c *http.Conn, dirname string, filenames []string) { } -func serveFile(c *http.Conn, path string) { +func serveSrc(c *http.Conn, path string) { dir, err := os.Stat(*root + path); if err != nil { c.WriteHeader(http.StatusNotFound); @@ -403,6 +414,10 @@ var ( func addFile(dirname string, filename string) { + if hasPostfix(filename, "_test.go") { + // ignore package tests + return; + } // determine package name path := *root + "/" + dirname + "/" + filename; prog, errors := compile(path, parser.PackageClauseOnly); @@ -517,14 +532,14 @@ func servePackageList(c *http.Conn, list *vector.Vector) { for i := 0; i < list.Len(); i++ { p := list.At(i).(*pakDesc); link := p.dirname + "/" + p.pakname; - fmt.Fprintf(c, "%s (%s)
\n", link + "?p", p.pakname, link); + fmt.Fprintf(c, "%s (%s)
\n", docPrefix + link, p.pakname, link); } } }); } -func servePackage(c *http.Conn, path string) { +func serveDoc(c *http.Conn, path string) { // make regexp for package matching rex, err := regexp.Compile(path); if err != nil { @@ -551,18 +566,24 @@ func servePackage(c *http.Conn, path string) { // ---------------------------------------------------------------------------- // Server -func serve(c *http.Conn, req *http.Request) { - if *verbose { - log.Stdoutf("%s\t%s", req.Host, req.RawUrl); - } - - path := sanitizePath(req.Url.Path); +func installHandler(prefix string, handler func(c *http.Conn, path string)) { + // customized handler with prefix + f := func(c *http.Conn, req *http.Request) { + path := req.Url.Path; + if *verbose { + log.Stdoutf("%s\t%s", req.Host, path); + } + if hasPrefix(path, prefix) { + path = sanitizePath(path[len(prefix) : len(path)]); + //log.Stdoutf("sanitized path %s", path); + handler(c, path); + } else { + log.Stdoutf("illegal path %s", path); + } + }; - if len(req.Url.Query) > 0 { // for now any query will do - servePackage(c, path); - } else { - serveFile(c, path); - } + // install the customized handler + http.Handle(prefix, http.HandlerFunc(f)); } @@ -584,7 +605,8 @@ func main() { makePackageMap(); - http.Handle("/", http.HandlerFunc(serve)); + installHandler(docPrefix, serveDoc); + installHandler(srcPrefix, serveSrc); { err := http.ListenAndServe(":" + *port, nil); if err != nil { log.Exitf("ListenAndServe: %v", err)