diff options
| author | Robert Griesemer <gri@golang.org> | 2009-11-07 13:17:53 -0800 |
|---|---|---|
| committer | Robert Griesemer <gri@golang.org> | 2009-11-07 13:17:53 -0800 |
| commit | c5d1ab2aed7adb17e46a6ec9c4adbb5984c29740 (patch) | |
| tree | 90c162eb069a581b93c324f0e4946e8ecea0c13d /src/cmd/godoc/godoc.go | |
| parent | 5189ea90c09777ac51fc5e8d6e679d955b0a757d (diff) | |
| download | golang-c5d1ab2aed7adb17e46a6ec9c4adbb5984c29740.tar.gz | |
- support to extract one-line package synopsis for package listings
- formatting in dirs.html is crude, needs better html (open to suggestions),
but shows the synopsis
- many package comments should probably be adjusted such that the first
sentence is more concise
R=rsc, iant
http://go/go-review/1025014
Diffstat (limited to 'src/cmd/godoc/godoc.go')
| -rw-r--r-- | src/cmd/godoc/godoc.go | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/src/cmd/godoc/godoc.go b/src/cmd/godoc/godoc.go index 9dcdd83c4..c1dd4ead8 100644 --- a/src/cmd/godoc/godoc.go +++ b/src/cmd/godoc/godoc.go @@ -135,12 +135,25 @@ func htmlEscape(s string) string { } +func firstSentence(s string) string { + i := strings.Index(s, ". "); + if i < 0 { + i = strings.Index(s, "."); + if i < 0 { + i = len(s)-1; // compensate for i+1 below + } + } + return s[0 : i+1]; // include ".", if any +} + + // ---------------------------------------------------------------------------- // Package directories type Directory struct { Path string; // includes Name Name string; + Text string; // package documentation, if any Dirs []*Directory; } @@ -150,7 +163,7 @@ func newDirTree(path, name string, depth int) *Directory { // return a dummy directory so that the parent directory // doesn't get discarded just because we reached the max // directory depth - return &Directory{path, name, nil}; + return &Directory{path, name, "", nil}; } list, _ := io.ReadDir(path); // ignore errors @@ -158,12 +171,22 @@ func newDirTree(path, name string, depth int) *Directory { // determine number of subdirectories and package files ndirs := 0; nfiles := 0; + text := ""; for _, d := range list { switch { case isPkgDir(d): ndirs++; case isPkgFile(d): nfiles++; + if text == "" { + // no package documentation yet; take the first found + file, err := parser.ParseFile(pathutil.Join(path, d.Name), nil, + parser.ParseComments | parser.PackageClauseOnly); + if err == nil && file.Name.Value == name && file.Doc != nil { + // found documentation; extract a synopsys + text = firstSentence(doc.CommentText(file.Doc)); + } + } } } @@ -190,7 +213,7 @@ func newDirTree(path, name string, depth int) *Directory { return nil; } - return &Directory{path, name, dirs}; + return &Directory{path, name, text, dirs}; } |
