1
0
mirror of https://github.com/golang/go synced 2024-11-26 00:28:00 -07:00

- 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
This commit is contained in:
Robert Griesemer 2009-11-07 13:17:53 -08:00
parent 3317697df4
commit 1ac60ddd10
2 changed files with 26 additions and 3 deletions

View File

@ -1,5 +1,5 @@
<table class="layout"> <table class="layout">
<tr><td colspan="2"><a href="{Path|path}">{Name|html}</a></td></tr> <tr><td colspan="2"><a href="{Path|path}">{Name|html}</a></td><td width="10"></td><td>{Text|html}</td></tr>
{.repeated section Dirs} {.repeated section Dirs}
<tr><td width="25"></td><td>{@|dir}</td></tr> <tr><td width="25"></td><td>{@|dir}</td></tr>
{.end} {.end}

View File

@ -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 // Package directories
type Directory struct { type Directory struct {
Path string; // includes Name Path string; // includes Name
Name string; Name string;
Text string; // package documentation, if any
Dirs []*Directory; Dirs []*Directory;
} }
@ -150,7 +163,7 @@ func newDirTree(path, name string, depth int) *Directory {
// return a dummy directory so that the parent directory // return a dummy directory so that the parent directory
// doesn't get discarded just because we reached the max // doesn't get discarded just because we reached the max
// directory depth // directory depth
return &Directory{path, name, nil}; return &Directory{path, name, "", nil};
} }
list, _ := io.ReadDir(path); // ignore errors 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 // determine number of subdirectories and package files
ndirs := 0; ndirs := 0;
nfiles := 0; nfiles := 0;
text := "";
for _, d := range list { for _, d := range list {
switch { switch {
case isPkgDir(d): case isPkgDir(d):
ndirs++; ndirs++;
case isPkgFile(d): case isPkgFile(d):
nfiles++; 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 nil;
} }
return &Directory{path, name, dirs}; return &Directory{path, name, text, dirs};
} }