Skip to content

Commit e2f6842

Browse files
Factor out function to rename references
* This makes the functionality easier to replace from other extensions, should this be needed
1 parent ef988a4 commit e2f6842

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

numpydoc/numpydoc.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,34 @@
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('^.. \\[([a-z0-9_.-])\\]'), line, re.I)
47+
if m:
48+
references.append(m.group(1))
49+
50+
# start renaming from the longest string, to avoid overwriting parts
51+
references.sort(key=lambda x: -len(x))
52+
if references:
53+
for i, line in enumerate(lines):
54+
for r in references:
55+
if re.match(sixu('^\\d+$'), r):
56+
new_r = sixu("R%d") % (reference_offset[0] + int(r))
57+
else:
58+
new_r = sixu("%s%d") % (r, reference_offset[0])
59+
lines[i] = lines[i].replace(sixu('[%s]_') % r,
60+
sixu('[%s]_') % new_r)
61+
lines[i] = lines[i].replace(sixu('.. [%s]') % r,
62+
sixu('.. [%s]') % new_r)
63+
64+
reference_offset[0] += len(references)
65+
66+
67+
def mangle_docstrings(app, what, name, obj, options, lines):
4268

4369
cfg = {'use_plots': app.config.numpydoc_use_plots,
4470
'show_class_members': app.config.numpydoc_show_class_members,
@@ -70,29 +96,9 @@ def mangle_docstrings(app, what, name, obj, options, lines,
7096
lines += [sixu(' %s') % x for x in
7197
(app.config.numpydoc_edit_link % v).split("\n")]
7298

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)
99+
# call function to replace reference numbers so that there are no
100+
# duplicates
101+
rename_references(app, what, name, obj, options, lines)
96102

97103

98104
def mangle_signature(app, what, name, obj, options, sig, retann):

0 commit comments

Comments
 (0)