Commit f596eb5d authored by Robert Griesemer's avatar Robert Griesemer

godoc: slightly smarter synopsis extraction

Ignore synopses that start with
"Copyright", "All rights", and "Author".

R=rsc
CC=golang-dev
https://golang.org/cl/6218047
parent 7b9a6d8d
...@@ -4,7 +4,10 @@ ...@@ -4,7 +4,10 @@
package doc package doc
import "unicode" import (
"strings"
"unicode"
)
// firstSentenceLen returns the length of the first sentence in s. // firstSentenceLen returns the length of the first sentence in s.
// The sentence ends after the first period followed by space and // The sentence ends after the first period followed by space and
...@@ -24,17 +27,12 @@ func firstSentenceLen(s string) int { ...@@ -24,17 +27,12 @@ func firstSentenceLen(s string) int {
return len(s) return len(s)
} }
// Synopsis returns a cleaned version of the first sentence in s. // clean replaces each sequence of space, \n, \r, or \t characters
// That sentence ends after the first period followed by space and // with a single space and removes any trailing and leading spaces.
// not preceded by exactly one uppercase letter. The result string func clean(s string) string {
// has no \n, \r, or \t characters and uses only single spaces between
// words.
//
func Synopsis(s string) string {
n := firstSentenceLen(s)
var b []byte var b []byte
p := byte(' ') p := byte(' ')
for i := 0; i < n; i++ { for i := 0; i < len(s); i++ {
q := s[i] q := s[i]
if q == '\n' || q == '\r' || q == '\t' { if q == '\n' || q == '\r' || q == '\t' {
q = ' ' q = ' '
...@@ -50,3 +48,26 @@ func Synopsis(s string) string { ...@@ -50,3 +48,26 @@ func Synopsis(s string) string {
} }
return string(b) return string(b)
} }
// Synopsis returns a cleaned version of the first sentence in s.
// That sentence ends after the first period followed by space and
// not preceded by exactly one uppercase letter. The result string
// has no \n, \r, or \t characters and uses only single spaces between
// words. If s starts with any of the IllegalPrefixes, the result
// is the empty string.
//
func Synopsis(s string) string {
s = clean(s[0:firstSentenceLen(s)])
for _, prefix := range IllegalPrefixes {
if strings.HasPrefix(strings.ToLower(s), prefix) {
return ""
}
}
return s
}
var IllegalPrefixes = []string{
"copyright",
"all rights",
"author",
}
...@@ -28,6 +28,11 @@ var tests = []struct { ...@@ -28,6 +28,11 @@ var tests = []struct {
{"P. Q. ", 8, "P. Q."}, {"P. Q. ", 8, "P. Q."},
{"Package Καλημέρα κόσμε.", 36, "Package Καλημέρα κόσμε."}, {"Package Καλημέρα κόσμε.", 36, "Package Καλημέρα κόσμε."},
{"Package こんにちは 世界\n", 31, "Package こんにちは 世界"}, {"Package こんにちは 世界\n", 31, "Package こんにちは 世界"},
{"Package foo does bar.", 21, "Package foo does bar."},
{"Copyright 2012 Google, Inc. Package foo does bar.", 27, ""},
{"All Rights reserved. Package foo does bar.", 20, ""},
{"All rights reserved. Package foo does bar.", 20, ""},
{"Authors: foo@bar.com. Package foo does bar.", 21, ""},
} }
func TestSynopsis(t *testing.T) { func TestSynopsis(t *testing.T) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment