Skip to content

Commit 4835c31

Browse files
authored
Merge pull request #50 from callegar/ref_rename_enhance
Fix/enhance replacement of citation reference numbers to avoid duplicates
2 parents 8dd78ec + 8ee66bd commit 4835c31

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

numpydoc/numpydoc.py

+30-24
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,33 @@
3737
sixu = lambda s: unicode(s, 'unicode_escape')
3838

3939

40-
def mangle_docstrings(app, what, name, obj, options, lines,
40+
def rename_references(app, what, name, obj, options, lines,
4141
reference_offset=[0]):
42+
# replace reference numbers so that there are no duplicates
43+
references = []
44+
for line in lines:
45+
line = line.strip()
46+
m = re.match(sixu('^.. \\[(%s)\\]') % app.config.numpydoc_citation_re,
47+
line, re.I)
48+
if m:
49+
references.append(m.group(1))
50+
51+
if references:
52+
for i, line in enumerate(lines):
53+
for r in references:
54+
if re.match(sixu('^\\d+$'), r):
55+
new_r = sixu("R%d") % (reference_offset[0] + int(r))
56+
else:
57+
new_r = sixu("%s%d") % (r, reference_offset[0])
58+
lines[i] = lines[i].replace(sixu('[%s]_') % r,
59+
sixu('[%s]_') % new_r)
60+
lines[i] = lines[i].replace(sixu('.. [%s]') % r,
61+
sixu('.. [%s]') % new_r)
62+
63+
reference_offset[0] += len(references)
64+
65+
66+
def mangle_docstrings(app, what, name, obj, options, lines):
4267

4368
cfg = {'use_plots': app.config.numpydoc_use_plots,
4469
'show_class_members': app.config.numpydoc_show_class_members,
@@ -70,29 +95,9 @@ def mangle_docstrings(app, what, name, obj, options, lines,
7095
lines += [sixu(' %s') % x for x in
7196
(app.config.numpydoc_edit_link % v).split("\n")]
7297

73-
# replace reference numbers so that there are no duplicates
74-
references = []
75-
for line in lines:
76-
line = line.strip()
77-
m = re.match(sixu('^.. \\[([a-z0-9_.-])\\]'), line, re.I)
78-
if m:
79-
references.append(m.group(1))
80-
81-
# start renaming from the longest string, to avoid overwriting parts
82-
references.sort(key=lambda x: -len(x))
83-
if references:
84-
for i, line in enumerate(lines):
85-
for r in references:
86-
if re.match(sixu('^\\d+$'), r):
87-
new_r = sixu("R%d") % (reference_offset[0] + int(r))
88-
else:
89-
new_r = sixu("%s%d") % (r, reference_offset[0])
90-
lines[i] = lines[i].replace(sixu('[%s]_') % r,
91-
sixu('[%s]_') % new_r)
92-
lines[i] = lines[i].replace(sixu('.. [%s]') % r,
93-
sixu('.. [%s]') % new_r)
94-
95-
reference_offset[0] += len(references)
98+
# call function to replace reference numbers so that there are no
99+
# duplicates
100+
rename_references(app, what, name, obj, options, lines)
96101

97102

98103
def mangle_signature(app, what, name, obj, options, sig, retann):
@@ -129,6 +134,7 @@ def setup(app, get_doc_object_=get_doc_object):
129134
app.add_config_value('numpydoc_show_class_members', True, True)
130135
app.add_config_value('numpydoc_show_inherited_class_members', True, True)
131136
app.add_config_value('numpydoc_class_members_toctree', True, True)
137+
app.add_config_value('numpydoc_citation_re', '[a-z0-9_.-]+', True)
132138

133139
# Extra mangling domains
134140
app.add_domain(NumpyPythonDomain)

0 commit comments

Comments
 (0)