can be set with the -maxresults flag; if set to 0, no full text results are
shown, and only an identifier index but no full text search index is created.
+The presentation mode of web pages served by godoc can be controlled with the
+"m" URL parameter; it accepts a comma-separated list of flag names as value:
+
+ all show documentation for all (not just exported) declarations
+ src show the original source code rather then the extracted documentation
+ text present the page in textual (command-line) form rather than HTML
+
+For instance, http://golang.org/pkg/big/?m=all,text shows the documentation for
+all (not just the exported) declarations of package big, in textual form (as
+it would appear when using godoc from the command line: "godoc -src big .*").
+
By default, godoc serves files from the file system of the underlying OS.
Instead, a .zip file may be provided via the -zip flag, which contains
the file system to serve. The file paths stored in the .zip file must use
godoc -http=:6060 -zip=go.zip -goroot=$HOME/go
-
See "Godoc: documenting Go code" for how to write good comments for godoc:
http://blog.golang.org/2011/03/godoc-documenting-go-code.html
*/
type PageInfoMode uint
const (
- exportsOnly PageInfoMode = 1 << iota // only keep exported stuff
- genDoc // generate documentation
+ noFiltering PageInfoMode = 1 << iota // do not filter exports
+ showSource // show source code, do not extract documentation
+ noHtml // show result in textual form, do not generate HTML
)
+// modeNames defines names for each PageInfoMode flag.
+var modeNames = map[string]PageInfoMode{
+ "all": noFiltering,
+ "src": showSource,
+ "text": noHtml,
+}
+
+// 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 getPageInfoMode(r *http.Request) (mode PageInfoMode) {
+ for _, k := range strings.Split(r.FormValue("m"), ",") {
+ if m, found := modeNames[strings.TrimSpace(k)]; found {
+ mode |= m
+ }
+ }
+ return
+}
+
type PageInfo struct {
Dirname string // directory containing the package
PList []string // list of package names found
return false
}
+func stripFunctionBodies(pkg *ast.Package) {
+ for _, f := range pkg.Files {
+ for _, d := range f.Decls {
+ if f, ok := d.(*ast.FuncDecl); ok {
+ f.Body = nil
+ }
+ }
+ }
+}
+
// getPageInfo returns the PageInfo for a package directory abspath. If the
// parameter genAST is set, an AST containing only the package exports is
// computed (PageInfo.PAst), otherwise package documentation (PageInfo.Doc)
var past *ast.File
var pdoc *doc.PackageDoc
if pkg != nil {
- if mode&exportsOnly != 0 {
+ if mode&noFiltering == 0 {
ast.PackageExports(pkg)
}
- if mode&genDoc != 0 {
+ stripFunctionBodies(pkg)
+ if mode&showSource == 0 {
pdoc = doc.NewPackageDoc(pkg, path.Clean(relpath)) // no trailing '/' in importpath
} else {
past = ast.MergePackageFiles(pkg, ast.FilterUnassociatedComments)
relpath := r.URL.Path[len(h.pattern):]
abspath := absolutePath(relpath, h.fsRoot)
- var mode PageInfoMode
- if relpath != builtinPkgPath {
- mode = exportsOnly
- }
- if r.FormValue("m") != "src" {
- mode |= genDoc
+ mode := getPageInfoMode(r)
+ if relpath == builtinPkgPath {
+ mode = noFiltering
}
info := h.getPageInfo(abspath, relpath, r.FormValue("p"), mode)
if info.Err != nil {
return
}
- if r.FormValue("f") == "text" {
+ if mode&noHtml != 0 {
contents := applyTemplate(packageText, "packageText", info)
serveText(w, contents)
return
query := strings.TrimSpace(r.FormValue("q"))
result := lookup(query)
- if r.FormValue("f") == "text" {
+ if getPageInfoMode(r)&noHtml != 0 {
contents := applyTemplate(searchText, "searchText", result)
serveText(w, contents)
return