github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/pkg/go/doc/synopsis.go (about)

     1  // Copyright 2012 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package doc
     6  
     7  import (
     8  	"strings"
     9  	"unicode"
    10  )
    11  
    12  // firstSentenceLen returns the length of the first sentence in s.
    13  // The sentence ends after the first period followed by space and
    14  // not preceded by exactly one uppercase letter.
    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  		ppp, pp, p = pp, p, q
    26  	}
    27  	return len(s)
    28  }
    29  
    30  // clean replaces each sequence of space, \n, \r, or \t characters
    31  // with a single space and removes any trailing and leading spaces.
    32  func clean(s string) string {
    33  	var b []byte
    34  	p := byte(' ')
    35  	for i := 0; i < len(s); i++ {
    36  		q := s[i]
    37  		if q == '\n' || q == '\r' || q == '\t' {
    38  			q = ' '
    39  		}
    40  		if q != ' ' || p != ' ' {
    41  			b = append(b, q)
    42  			p = q
    43  		}
    44  	}
    45  	// remove trailing blank, if any
    46  	if n := len(b); n > 0 && p == ' ' {
    47  		b = b[0 : n-1]
    48  	}
    49  	return string(b)
    50  }
    51  
    52  // Synopsis returns a cleaned version of the first sentence in s.
    53  // That sentence ends after the first period followed by space and
    54  // not preceded by exactly one uppercase letter. The result string
    55  // has no \n, \r, or \t characters and uses only single spaces between
    56  // words. If s starts with any of the IllegalPrefixes, the result
    57  // is the empty string.
    58  //
    59  func Synopsis(s string) string {
    60  	s = clean(s[0:firstSentenceLen(s)])
    61  	for _, prefix := range IllegalPrefixes {
    62  		if strings.HasPrefix(strings.ToLower(s), prefix) {
    63  			return ""
    64  		}
    65  	}
    66  	return s
    67  }
    68  
    69  var IllegalPrefixes = []string{
    70  	"copyright",
    71  	"all rights",
    72  	"author",
    73  }