-
Notifications
You must be signed in to change notification settings - Fork 6
/
author.go
267 lines (239 loc) · 6.03 KB
/
author.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package bibtex
import (
"fmt"
"github.com/jschaf/bibtex/ast"
"strings"
"unicode"
"unicode/utf8"
)
const authorSep = "and"
// ExtractAuthors extracts the authors from the parsed text of a bibtex field,
// usually from the author or editor field of bibtex entry.
func ExtractAuthors(txt *ast.ParsedText) (ast.Authors, error) {
// Pop tokens until we get the author separator then hand off the tokens
// to extractAuthor to get the Author.
authors := make([]*ast.Author, 0, 4)
aExprs := make([]ast.Expr, 0, 8)
for _, v := range txt.Values {
switch t := v.(type) {
case *ast.Text:
if t.Value == authorSep {
a := extractAuthor(aExprs)
if a.IsEmpty() {
return nil, fmt.Errorf("found an empty author")
}
authors = append(authors, a)
aExprs = aExprs[:0]
continue
}
}
aExprs = append(aExprs, v)
}
final := extractAuthor(aExprs)
if final.IsEmpty() {
return nil, fmt.Errorf("found an empty author")
}
authors = append(authors, final)
return authors, nil
}
func trimSpaces(xs []ast.Expr) []ast.Expr {
lo, hi := 0, len(xs)
for i := 0; i < len(xs); i++ {
if _, ok := xs[i].(*ast.TextSpace); !ok {
break
}
lo++
}
for i := len(xs) - 1; i >= 0; i-- {
if _, ok := xs[i].(*ast.TextSpace); !ok {
break
}
hi--
}
if hi <= lo {
return xs[lo:lo]
}
return xs[lo:hi]
}
func extractAuthor(xs []ast.Expr) *ast.Author {
xs = trimSpaces(xs)
commas := findCommas(xs)
if len(commas) == 0 {
return resolveAuthor0(xs)
} else {
return resolveAuthorN(xs, commas)
}
}
type resolveAction int
const (
resolveContinue resolveAction = iota
resolveNextPart
)
func parseFirstName(idx int, xs []ast.Expr) (string, resolveAction) {
x := xs[idx]
if _, ok := x.(*ast.Text); !ok {
return parseDefault(idx, xs), resolveContinue
}
t := x.(*ast.Text)
if !hasUpperPrefix(t.Value) {
return "", resolveNextPart // lowercase means we're out of the first name
}
return t.Value, resolveContinue
}
func hasUpperPrefix(s string) bool {
r, _ := utf8.DecodeRuneInString(s)
return unicode.IsUpper(r)
}
func parsePrefixName(idx int, xs []ast.Expr) (string, resolveAction) {
x := xs[idx]
if _, ok := x.(*ast.Text); !ok {
return parseDefault(idx, xs), resolveContinue
}
t := x.(*ast.Text)
if hasUpperPrefix(t.Value) {
return "", resolveNextPart // Only lowercase goes in prefix
}
return t.Value, resolveContinue
}
func parseLastName(idx int, xs []ast.Expr) (string, resolveAction) {
return parseDefault(idx, xs), resolveContinue
}
// TODO: replace with single resolver
func parseDefault(idx int, xs []ast.Expr) string {
x := xs[idx]
switch t := x.(type) {
case *ast.ParsedText:
sb := strings.Builder{}
sb.Grow(16)
for i := range t.Values {
d := parseDefault(i, t.Values)
sb.WriteString(d)
}
return sb.String()
case *ast.TextComma:
return ","
case *ast.TextEscaped:
return `\` + t.Value
case *ast.TextHyphen:
return "-"
case *ast.TextNBSP, *ast.TextSpace:
return " "
case *ast.TextMath:
return "$" + t.Value + "$"
case *ast.Text:
return t.Value
default:
panic("unhandled ast.Expr value")
}
}
// resolveAuthor0 resolves an author for an entry with no commas, like
// "First von Last".
func resolveAuthor0(xs []ast.Expr) *ast.Author {
first := strings.Builder{}
first.Grow(16)
idx := 0
for ; idx < len(xs); idx++ {
if idx == len(xs)-1 {
// If we're on the last part, it belongs to the last name.
break
}
val, action := parseFirstName(idx, xs)
if action == resolveNextPart {
break
}
first.WriteString(val)
}
prefix := strings.Builder{}
for ; idx < len(xs); idx++ {
if idx == len(xs)-1 {
// If we're on the last part, it belongs to the last name.
break
}
val, action := parsePrefixName(idx, xs)
if action == resolveNextPart {
break
}
prefix.WriteString(val)
}
last := strings.Builder{}
last.Grow(16)
for ; idx < len(xs); idx++ {
val, action := parseLastName(idx, xs)
if action == resolveNextPart {
break
}
last.WriteString(val)
}
return &ast.Author{
First: &ast.Text{Value: strings.TrimSpace(first.String())},
Prefix: &ast.Text{Value: strings.TrimSpace(prefix.String())},
Last: &ast.Text{Value: strings.TrimSpace(last.String())},
Suffix: &ast.Text{Value: ""},
}
}
// resolveAuthorN resolves an author entry that contains 1 or more commas.
//
// 1 comma: last, first => Author{first, "", last, ""}
// 1 comma: von last, first => Author(first, von, last, "")
// 2 commas: last, suffix, first => Author{first, "", last, suffix}
// 2 commas: von last, suffix, first => Author{first, von, last, suffix}
func resolveAuthorN(xs []ast.Expr, commas []int) *ast.Author {
part1 := xs[:commas[0]]
idx1 := 0
prefix := strings.Builder{}
for ; idx1 < len(part1); idx1++ {
if idx1 == len(part1)-1 {
// If we're on the last part, it belongs to the last name.
break
}
val, action := parsePrefixName(idx1, part1)
if action == resolveNextPart {
break
}
prefix.WriteString(val)
}
last := strings.Builder{}
last.Grow(16)
for ; idx1 < len(part1); idx1++ {
val, action := parseLastName(idx1, part1)
if action == resolveNextPart {
break
}
last.WriteString(val)
}
part2 := xs[commas[0]+1:]
suffix := strings.Builder{}
suffix.Grow(16)
if len(commas) > 1 {
val := parseDefault(0, xs[commas[0]+1:])
suffix.WriteString(val)
part2 = xs[commas[1]+1:]
}
idx2 := 0
first := strings.Builder{}
first.Grow(16)
for ; idx2 < len(part2); idx2++ {
val, action := parseFirstName(idx2, part2)
if action == resolveNextPart {
break
}
first.WriteString(val)
}
return &ast.Author{
First: &ast.Text{Value: strings.TrimSpace(first.String())},
Prefix: &ast.Text{Value: strings.TrimSpace(prefix.String())},
Last: &ast.Text{Value: strings.TrimSpace(last.String())},
Suffix: &ast.Text{Value: strings.TrimSpace(suffix.String())},
}
}
// findCommas returns the offsets of all commas in xs. Only searches
// 1 level deep.
func findCommas(xs []ast.Expr) []int {
idxs := make([]int, 0, 2)
for i, x := range xs {
if _, ok := x.(*ast.TextComma); ok {
idxs = append(idxs, i)
}
}
return idxs
}