/
__init__.py
executable file
·534 lines (438 loc) · 15.4 KB
/
__init__.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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
from docutils import nodes, transforms
from docutils.parsers.rst import directives
from sphinx import addnodes
from sphinx.domains import Domain, ObjType
from sphinx.locale import l_, _
from sphinx.roles import XRefRole
from sphinx.util.compat import Directive
from pybtex.database.input import bibtex
import collections
import latex_codec
import os
import re
latex_codec.register()
DEFAULT_CONF = {
'file': '',
'brackets': '()',
'separator': ';',
'style': 'authoryear', # 'numbers', 'super'
'sort': False,
'sort_compress': False
}
ROLES = [
'p', 'ps', 'alp', 'alps',
't', 'ts', 'alt', 'alts',
'author', 'authors', 'year', 'yearpar', 'text'
]
KEY, PREV, NEXT = range(3)
class OrderedSet(collections.MutableSet):
"""
From: http://code.activestate.com/recipes/576694/
"""
def __init__(self, iterable=None):
self.end = end = []
end += [None, end, end] # sentinel node for doubly linked list
self.map = {} # key --> [key, prev, next]
if iterable is not None:
self |= iterable
def __len__(self):
return len(self.map)
def __contains__(self, key):
return key in self.map
def add(self, key):
if key not in self.map:
end = self.end
curr = end[PREV]
curr[NEXT] = end[PREV] = self.map[key] = [key, curr, end]
def discard(self, key):
if key in self.map:
key, prev, next = self.map.pop(key)
prev[NEXT] = next
next[PREV] = prev
def __iter__(self):
end = self.end
curr = end[NEXT]
while curr is not end:
yield curr[KEY]
curr = curr[NEXT]
def __reversed__(self):
end = self.end
curr = end[PREV]
while curr is not end:
yield curr[KEY]
curr = curr[PREV]
def pop(self, last=True):
if not self:
raise KeyError('set is empty')
key = next(reversed(self)) if last else next(iter(self))
self.discard(key)
return key
def __repr__(self):
if not self:
return '%s()' % (self.__class__.__name__,)
return '%s(%r)' % (self.__class__.__name__, list(self))
def __eq__(self, other):
if isinstance(other, OrderedSet):
return len(self) == len(other) and list(self) == list(other)
return set(self) == set(other)
def __del__(self):
self.clear() # remove circular references
SUBSUP_RE = re.compile(r'([\s\S^\^\_]*)([\^\_]){?([\S\s^}]*)}?')
def latex_to_nodes(text):
"""
Convert '_{}' and '^{}' text to node representation.
TODO: This doesn't work yet
"""
return nodes.inline(text, text)
m = SUBSUP_RE.findall(text)
if m:
n += latex_to_nodes(m[0][0])
if m[0][1] == '^':
n += nodes.superscript(latex_to_nodes(m[0][2]))
elif m[0][1] == '_':
n += nodes.subscript(latex_to_nodes(m[0][2]))
else:
n = nodes.inline(text, text)
return n
def parse_keys(rawtext):
# Get the keys and any pre- and post-ciation text
# Spaces nor commas are allowed in cite keys, so we split on commas
# first. This will give us a list of keys, however, the last item may have
# pre- and post-citation text (in brackets "[pre][post]")
#
# TODO: This isn't the best implementation and this should also
# handle errors
pre = u''
post = u''
keys = []
for k in rawtext.split(','):
k = k.strip() # Remove leading and trailing whitespace
k = k.split(' ', 1) # Split on the first space in the key, if any
if len(k) > 1:
# We have some extra text here
k, text = k
bo, bc = 0, 0
for c in text.strip():
if c == "[" and bo == bc:
bo += 1
elif c == "]" and bo == bc+1:
bc += 1
else:
if bc == 0:
pre += c
else:
post += c
if bc == bo and bc == 1:
post = pre
pre = u''
else:
k = k[0]
keys.append(k)
return (keys, pre, post)
class Citations(object):
def __init__(self, env):
self.conf = DEFAULT_CONF.copy()
self.conf.update(env.config.natbib)
self.file_name = None
self.parser = None
self.data = None
self.ref_map = {}
file_name = self.conf.get('file')
if file_name:
self.file_name = file_name
self.parser = bibtex.Parser()
self.data = self.parser.parse_file(self.file_name)
def get(self, key):
return self.data.entries.get(key)
class CitationTransform(object):
"""
This class is meant to be applied to a ``docutils.nodes.pending`` node when a
``cite`` role is encountered. Later (during the resolve_xref stage) this
class can be used to generate the proper citation reference nodes for
insertion into the actual document.
"""
def __init__(self, refs, pre, post, typ, global_keys, config):
self.refs = refs
self.pre = pre
self.post = post
self.config = config
self.typ = typ
self.global_keys = global_keys
def __repr__(self):
return '<%s>' % self.__str__()
def __str__(self):
return ','.join([r.key for r in self.refs])
def get_ref_num(self, key):
for i, k in enumerate(self.global_keys):
if k == key:
return i+1
def get_author(self, authors, all_authors=False):
if len(authors) > 2 and not all_authors:
author = u'%s et al.' % authors[0].last()[0].strip('{}')
elif len(authors) == 2 and not all_authors:
author = u"%s and %s" % (u', '.join([a.last()[0] for a in authors[:-1]]),authors[-1].last()[0].strip('{}'))
else:
""" Brent edit: """
author = u''.join(authors[0].last()[0].strip('{}'))
return author
def cite(self, cmd, refuri):
"""
Return a docutils Node consisting of properly formatted citations children
nodes.
Brent edit: replaced 'inline' with 'generated'.
"""
bo, bc = self.config['brackets']
sep = u'%s ' % self.config['separator']
style = self.config['style']
all_auths = (cmd.endswith('s'))
alt = (cmd.startswith('alt') or \
(cmd.startswith('alp')) or \
(style == 'citeyear'))
if (cmd.startswith('p') or cmd == 'yearpar') and style != 'super':
node = nodes.generated(bo, bo, classes=['citation'])
else:
node = nodes.generated('', '', classes=['citation'])
if self.pre:
pre = u"%s " % self.pre.decode('latex')
node += nodes.generated(pre, pre, classes=['pre'])
for i, ref in enumerate(self.refs):
authors = ref.persons.get('author', [])
author_text = self.get_author(authors, all_auths).decode('latex')
lrefuri = refuri + '#citation-' + nodes.make_id(ref.key)
if i > 0 and i < len(self.refs):
if style == "authoryear":
node += nodes.generated(sep, sep)
else:
if style == "super":
node += nodes.superscript(', ', ', ')
else:
node += nodes.generated(', ', ', ')
if (style == "authoryear" and (cmd.startswith('p') or cmd.startswith('alp'))) or \
(cmd.startswith('t') or cmd.startswith('alt') or cmd.startswith('author')):
node += nodes.reference(author_text, author_text, internal=True, refuri=lrefuri)
if cmd.startswith('p') or cmd.startswith('alp'):
node += nodes.generated(', ', ', ')
else:
node += nodes.generated(' ', ' ')
# Add in either the year or the citation number
if not cmd.startswith('author'):
if style != 'authoryear':
num = self.get_ref_num(ref.key)
else:
num = ref.fields.get('year')
refnode = nodes.reference(str(num), str(num), internal=True, refuri=lrefuri)
if cmd.startswith('t') and style != 'super':
node += nodes.generated(bo, bo)
if style == 'super':
node += nodes.superscript('', '', refnode)
else:
node += refnode
if cmd.startswith('t') and style != 'super':
node += nodes.generated(bc, bc)
if self.post:
post = u", %s" % self.post.decode('latex')
node += nodes.generated(post, post, classes=['post'])
if (cmd.startswith('p') or cmd == 'yearpar') and style != 'super':
node += nodes.generated(bc, bc, classes=['citation'])
return node
class CitationXRefRole(XRefRole):
def __call__(self, typ, rawtext, text, lineno, inliner, options={},\
content=[]):
"""
When a ``cite`` role is encountered, we replace it with a
``docutils.nodes.pending`` node that uses a ``CitationTrasform`` for
generating the proper citation reference representation during the
resolve_xref phase.
"""
rnodes = super(CitationXRefRole, self).__call__(typ, rawtext, text, lineno,
inliner, options, content)
rootnode = rnodes[0][0]
env = inliner.document.settings.env
citations = env.domains['cite'].citations
# Get the config at this point in the document
config = {}
for opt in ['style', 'brackets', 'separator', 'sort', 'sort_compress']:
config[opt] = env.temp_data.get("cite_%s" % opt, env.domaindata['cite']['conf'].get(opt, DEFAULT_CONF[opt]))
if typ == "cite:text":
# A ``text`` citation is unique because it doesn't reference a cite-key
keys = []
pre, post = text, ''
else:
keys, pre, post = parse_keys(text)
for key in keys:
if citations.get(key) is None:
env.warn(env.docname, "cite-key `%s` not found in bibtex file" % key, lineno)
continue
env.domaindata['cite']['keys'].add(key)
data = {'keys': keys,
'pre': pre,
'post': post,
'typ': typ,
'global_keys': env.domaindata['cite']['keys'],
'config': config}
rootnode += nodes.pending(CitationTransform, data)
return [rootnode], []
class CitationConfDirective(Directive):
"""
Allows the user to change the citation style on a per-page or
per-block basis.
"""
has_content = False
required_arguments = 0
optional_arguments = 1
option_spec = {
'brackets': directives.unchanged,
'separator': directives.unchanged,
'style': directives.unchanged,
'sort': directives.flag,
'sort_compress': directives.flag,
}
def run(self):
env = self.state.document.settings.env
# TODO: verify options
if self.arguments:
env.temp_data['cite_style'] = self.arguments[0]
else:
env.temp_data['cite_style'] = self.options.get('style', DEFAULT_CONF['style'])
try:
self.options.pop('style')
except:
pass
for k, v in self.options.items():
env.temp_data['cite_%s' % k] = v
return []
class CitationReferencesDirective(Directive):
"""
Generates the actual reference list.
"""
has_content = False
required_arguments = 0
optional_arguments = 0
# TODO: Implement support for multiple bib files
option_spec = {
'path': directives.unchanged,
}
def get_reference_node(self, ref):
node = nodes.inline('','', classes=[ref.type, 'reference'])
# Authors
authors = ref.persons.get('author', [])
for i, author in enumerate(authors):
names = [author.first(), author.middle(), author.last()]
text = u''
for part in names:
if part:
text += u' '.join(n.decode('latex').strip('{}') for n in part)
text += u' '
text = text.strip()
auth_node = latex_to_nodes(text)
auth_node['classes'].append('author')
node += auth_node
#node += nodes.inline(text, text, classes=['author'])
if i+1 < len(authors):
node += nodes.inline(', ', ', ')
else:
node += nodes.inline('. ', '. ')
# Title
title = ref.fields.get('title')
if title:
title = title.decode('latex').strip('{}.')
node += nodes.inline(title, title, classes=['title'])
node += nodes.inline('. ', '. ')
# Publication
# TODO: handle other types of publications
pub = ref.fields.get('journal')
if pub:
pub = pub.decode('latex')
node += nodes.emphasis(pub, pub, classes=['publication'])
node += nodes.inline(', ', ', ')
vol = ref.fields.get('volume')
if vol:
vol = vol.decode('latex')
node += nodes.inline(vol, vol, classes=['volume'])
node += nodes.inline(':', ':')
pages = ref.fields.get('pages')
if pages:
pages = pages.decode('latex')
node += nodes.inline(pages, pages, classes=['pages'])
node += nodes.inline(', ', ', ')
year = ref.fields.get('year')
if year:
year = year.decode('latex')
node += nodes.inline(year, year, classes=['year'])
node += nodes.inline('.', '.')
return node
def run(self):
"""
Generate the definition list that displays the actual references.
"""
env = self.state.document.settings.env
keys = env.domaindata['cite']['keys']
env.domaindata['cite']['refdoc'] = env.docname
citations = env.domains['cite'].citations
# TODO: implement
#env.domaindata['cite']['refdocs'][env.docname] = Citations(env, path)
# Build the references list
# TODO: Make this an enumerated_list or field_list maybe?
node = nodes.definition_list()
node.document = self.state.document
node['classes'].append('references')
items = []
for i, key in enumerate(keys):
term = nodes.term('', '')
# TODO: Allow the format of the reference list be configurable
if env.domaindata['cite']['conf']['style'] == 'super':
term.children = [nodes.superscript('', i+1)]
else:
term.children = [nodes.inline('', "%s) " % (i+1))]
nid = "citation-%s" % nodes.make_id(key)
definition = self.get_reference_node(citations.get(key))
li = nodes.definition_list_item('', term, definition)
li[0]['ids'].append(nid)
li[0]['names'].append(nid)
items.append(li)
node.extend(item for item in items)
return [node]
class CitationDomain(Domain):
name = "cite"
label = "citation"
object_types = {
'citation': ObjType(l_('citation'), *ROLES, searchprio=-1),
}
directives = {
'conf': CitationConfDirective,
'refs': CitationReferencesDirective
}
roles = dict([(r, CitationXRefRole()) for r in ROLES])
initial_data = {
'keys': OrderedSet(), # Holds cite-keys in order of reference
'conf': DEFAULT_CONF,
'refdocs': {}
}
def __init__(self, env):
super(CitationDomain, self).__init__(env)
# Update conf
env.domaindata['cite']['conf'].update(env.config.natbib)
# TODO: warn if citations can't parse bibtex file
self.citations = Citations(env)
def resolve_xref(self, env, fromdocname, builder,
typ, target, node, contnode):
refdoc = env.domaindata['cite'].get('refdoc')
if not refdoc:
env.warn(fromdocname , 'no `refs` directive found; citations will have dead links', node.line)
refuri = ''
else:
refuri = builder.get_relative_uri(fromdocname, refdoc)
for nd in node.children:
if isinstance(nd, nodes.pending):
nd.details['refs'] = []
for key in nd.details.pop('keys'):
ref = self.citations.get(key)
if ref is None:
continue
nd.details['refs'].append(ref)
transform = nd.transform(**nd.details)
node = transform.cite(typ, refuri)
return node
def setup(app):
app.add_config_value('natbib', DEFAULT_CONF, 'env')
app.add_domain(CitationDomain)