|
37 | 37 | sixu = lambda s: unicode(s, 'unicode_escape') |
38 | 38 |
|
39 | 39 |
|
40 | | -def mangle_docstrings(app, what, name, obj, options, lines, |
| 40 | +def rename_references(app, what, name, obj, options, lines, |
41 | 41 | 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): |
42 | 67 |
|
43 | 68 | cfg = {'use_plots': app.config.numpydoc_use_plots, |
44 | 69 | 'show_class_members': app.config.numpydoc_show_class_members, |
@@ -70,29 +95,9 @@ def mangle_docstrings(app, what, name, obj, options, lines, |
70 | 95 | lines += [sixu(' %s') % x for x in |
71 | 96 | (app.config.numpydoc_edit_link % v).split("\n")] |
72 | 97 |
|
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) |
96 | 101 |
|
97 | 102 |
|
98 | 103 | def mangle_signature(app, what, name, obj, options, sig, retann): |
@@ -129,6 +134,7 @@ def setup(app, get_doc_object_=get_doc_object): |
129 | 134 | app.add_config_value('numpydoc_show_class_members', True, True) |
130 | 135 | app.add_config_value('numpydoc_show_inherited_class_members', True, True) |
131 | 136 | app.add_config_value('numpydoc_class_members_toctree', True, True) |
| 137 | + app.add_config_value('numpydoc_citation_re', '[a-z0-9_.-]+', True) |
132 | 138 |
|
133 | 139 | # Extra mangling domains |
134 | 140 | app.add_domain(NumpyPythonDomain) |
|
0 commit comments