Source file
src/go/doc/synopsis.go
1
2
3
4
5 package doc
6
7 import (
8 "go/doc/comment"
9 "strings"
10 "unicode"
11 )
12
13
14
15
16 func firstSentence(s string) string {
17 var ppp, pp, p rune
18 for i, q := range s {
19 if q == '\n' || q == '\r' || q == '\t' {
20 q = ' '
21 }
22 if q == ' ' && p == '.' && (!unicode.IsUpper(pp) || unicode.IsUpper(ppp)) {
23 return s[:i]
24 }
25 if p == '。' || p == '.' {
26 return s[:i]
27 }
28 ppp, pp, p = pp, p, q
29 }
30 return s
31 }
32
33
34
35
36
37 func Synopsis(text string) string {
38 var p Package
39 return p.Synopsis(text)
40 }
41
42
43
44
45
46
47 var IllegalPrefixes = []string{
48 "copyright",
49 "all rights",
50 "author",
51 }
52
53
54
55
56
57
58
59 func (p *Package) Synopsis(text string) string {
60 text = firstSentence(text)
61 lower := strings.ToLower(text)
62 for _, prefix := range IllegalPrefixes {
63 if strings.HasPrefix(lower, prefix) {
64 return ""
65 }
66 }
67 pr := p.Printer()
68 pr.TextWidth = -1
69 d := p.Parser().Parse(text)
70 if len(d.Content) == 0 {
71 return ""
72 }
73 if _, ok := d.Content[0].(*comment.Paragraph); !ok {
74 return ""
75 }
76 d.Content = d.Content[:1]
77 return strings.TrimSpace(string(pr.Text(d)))
78 }
79
View as plain text