...
Source file
src/go/doc/synopsis.go
Documentation: go/doc
1
2
3
4
5 package doc
6
7 import (
8 "strings"
9 "unicode"
10 )
11
12
13
14
15
16 func firstSentenceLen(s string) int {
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 i
24 }
25 if p == '。' || p == '.' {
26 return i
27 }
28 ppp, pp, p = pp, p, q
29 }
30 return len(s)
31 }
32
33 const (
34 keepNL = 1 << iota
35 )
36
37
38
39
40
41 func clean(s string, flags int) string {
42 var b []byte
43 p := byte(' ')
44 for i := 0; i < len(s); i++ {
45 q := s[i]
46 if (flags&keepNL) == 0 && q == '\n' || q == '\r' || q == '\t' {
47 q = ' '
48 }
49 if q != ' ' || p != ' ' {
50 b = append(b, q)
51 p = q
52 }
53 }
54
55 if n := len(b); n > 0 && p == ' ' {
56 b = b[0 : n-1]
57 }
58 return string(b)
59 }
60
61
62
63
64
65
66
67
68 func Synopsis(s string) string {
69 s = clean(s[0:firstSentenceLen(s)], 0)
70 for _, prefix := range IllegalPrefixes {
71 if strings.HasPrefix(strings.ToLower(s), prefix) {
72 return ""
73 }
74 }
75 s = convertQuotes(s)
76 return s
77 }
78
79 var IllegalPrefixes = []string{
80 "copyright",
81 "all rights",
82 "author",
83 }
84
View as plain text