-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathinitials.go
106 lines (96 loc) · 3.22 KB
/
initials.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package avatar
import (
"bufio"
"bytes"
"io"
"regexp"
"strings"
"unicode"
)
const email = "^(((([a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+(\\.([a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+)*)|((\\x22)((((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(([\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]|\\x21|[\\x23-\\x5b]|[\\x5d-\\x7e]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(\\([\\x01-\\x09\\x0b\\x0c\\x0d-\\x7f]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}]))))*(((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(\\x22)))@((([a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(([a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])([a-zA-Z]|\\d|-|\\.|_|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*([a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.)+(([a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(([a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])([a-zA-Z]|\\d|-|\\.|_|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*([a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.?$"
var regxEmail *regexp.Regexp
func init() {
regxEmail = regexp.MustCompile(email)
}
// This controls how parsing of initials is handled.
type opts struct {
// set all initials to capital letters
allCaps bool
// allow parsing of initials from email messages
allowEmail bool
// set the maximum number of initials allowed
limit int
}
// Tries to find initials in a given src. The src is a name, the logic that is
// used to decide which characters are used as initials is adopted from the
// initials project https://github.com/gr2m/initials.
//
// You can pass an opts object to contorl the parsing like setting maximum
// number of initials and allowing parsing of initials from emails etc.
func parseInitials(src io.Reader, o opts) (string, error) {
scanner := bufio.NewScanner(src)
scanner.Split(bufio.ScanWords)
var words []string
for scanner.Scan() {
words = append(words, scanner.Text())
}
if err := scanner.Err(); err != nil {
return "", err
}
buf := &bytes.Buffer{}
count := 0
for i, w := range words {
if count >= o.limit {
break
}
if regxEmail.MatchString(w) {
if i == 0 && o.allowEmail {
s := strings.Split(w, "@")
sr := strings.NewReader(s[0])
return parseInitials(sr, o)
}
continue
}
r := strings.NewReader(w)
x, _, err := r.ReadRune()
if err != nil {
return "", err
}
switch {
case unicode.IsLetter(x):
if o.allCaps {
if unicode.IsLower(x) {
x = unicode.ToUpper(x)
}
}
_, _ = buf.WriteRune(x)
count++
case x == '(':
if i > 0 {
rb := &bytes.Buffer{}
DONE:
for {
next, _, err := r.ReadRune()
switch next {
case ')':
_, _, err = r.ReadRune()
if err != nil {
if err != io.EOF {
rb.Reset()
}
}
break DONE
default:
if err != nil {
rb.Reset()
break DONE
}
_, _ = rb.WriteRune(next)
}
}
return rb.String(), nil
}
}
}
return buf.String(), nil
}