-
Notifications
You must be signed in to change notification settings - Fork 13
/
author_year.py
184 lines (166 loc) · 6.74 KB
/
author_year.py
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
# -*- coding: utf-8 -*-
# Copyright (c) 2015 Chris MacMackin
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
Defines an author-year inline citation style for Pybtex. This is
modified from the alpha style built into Pybtex, written by
Andrey Golovizin.
"""
from collections import Counter
import re
import string
import unicodedata
from pybtex.style.labels import BaseLabelStyle
import sys
if (sys.version_info[0]>2):
unicode = str
_nonalnum_pattern = re.compile('[^\w]+', re.UNICODE)
def _strip_accents(s):
return u''.join(
(c for c in unicodedata.normalize('NFD', s)
if not unicodedata.combining(c)))
def _strip_nonalnum(parts):
"""Strip all non-alphanumerical characters from a list of strings.
>>> _strip_nonalnum([u"ÅA. B. Testing 12+}[.@~_", u" 3%"])
u'AABTesting123'
"""
s = u''.join(parts)
return _nonalnum_pattern.sub(u'', s)
class LabelStyle(BaseLabelStyle):
name = 'alpha'
def format_labels(self, sorted_entries):
labels = [self.format_label(entry) for entry in sorted_entries]
for i in range(len(labels)):
labels[i] = labels[i].replace('\\{', '{')
labels[i] = labels[i].replace('\\}', '}')
labels[i] = labels[i].replace('{', '')
labels[i] = labels[i].replace('}', '')
count = Counter(labels)
counted = Counter()
for label in labels:
if count[label]:
yield '(' + label + ')'
else:
yield '(' + label + chr(ord('a') + counted[label]) + ')'
counted.update([label])
# note: this currently closely follows the alpha.bst code
# we should eventually refactor it
def format_label(self, entry):
# see alpha.bst calc.label
if entry.type == "book" or entry.type == "inbook":
label = self.author_editor_key_label(entry)
elif entry.type == "proceedings":
label = self.editor_key_organization_label(entry)
elif entry.type == "manual":
label = self.author_key_organization_label(entry)
else:
label = self.author_key_label(entry)
if "year" in entry.fields:
return label.strip() + ', ' + entry.fields["year"]
else:
return label.strip()
# bst additionally sets sort.label
def author_key_label(self, entry):
# see alpha.bst author.key.label
if not "author" in entry.persons:
if not "key" in entry.fields:
return entry.key[:] # entry.key is bst cite$
else:
# for entry.key, bst actually uses text.prefix$
return entry.fields["key"][:]
else:
return self.format_lab_names(entry.persons["author"])
def author_editor_key_label(self, entry):
# see alpha.bst author.editor.key.label
if not "author" in entry.persons:
if not "editor" in entry.persons:
if not "key" in entry.fields:
return entry.key[:] # entry.key is bst cite$
else:
# for entry.key, bst actually uses text.prefix$
return entry.fields["key"][:]
else:
return self.format_lab_names(entry.persons["editor"])
else:
return self.format_lab_names(entry.persons["author"])
def author_key_organization_label(self, entry):
if not "author" in entry.persons:
if not "key" in entry.fields:
if not "organization" in entry.fields:
return entry.key[:] # entry.key is bst cite$
else:
result = entry.fields["organization"]
if result.startswith("The "):
result = result[4:]
return result
else:
return entry.fields["key"][:]
else:
return self.format_lab_names(entry.persons["author"])
def editor_key_organization_label(self, entry):
if not "editor" in entry.persons:
if not "key" in entry.fields:
if not "organization" in entry.fields:
return entry.key[:] # entry.key is bst cite$
else:
result = entry.fields["organization"]
if result.startswith("The "):
result = result[4:]
return result
else:
return entry.fields["key"][:]
else:
return self.format_lab_names(entry.persons["editor"])
def format_lab_names(self, persons):
# see alpha.bst format.lab.names
# s = persons
numnames = len(persons)
if numnames > 1:
if numnames > 2:
namesleft = 1
else:
namesleft = numnames
result = ""
nameptr = 1
while namesleft:
person = persons[nameptr - 1]
if nameptr == numnames:
if unicode(person) == "others":
result += "et al. "
else:
result += _strip_nonalnum(
person.prelast(abbr=True) + [' '] + person.last())
else:
result += _strip_nonalnum(
person.prelast(abbr=True) + [' '] + person.last())
if numnames == 2 and nameptr == 1:
result += ' and '
else:
result += ' '
nameptr += 1
namesleft -= 1
if numnames > 2:
result += "et al."
else:
person = persons[0]
result = _strip_nonalnum(
person.prelast(abbr=True) + [' '] + person.last())
return result