22
22
23
23
def mangle_docstrings (app , what , name , obj , options , lines ,
24
24
reference_offset = [0 ]):
25
+
25
26
if what == 'module' :
26
27
# Strip top title
27
28
title_re = re .compile (r'^\s*[#*=]{4,}\n[a-z0-9 -]+\n[#*=]{4,}\s*' ,
@@ -43,25 +44,25 @@ def mangle_docstrings(app, what, name, obj, options, lines,
43
44
44
45
# replace reference numbers so that there are no duplicates
45
46
references = []
46
- for l in lines :
47
- l = l .strip ()
48
- if l .startswith ('.. [' ):
49
- try :
50
- references .append (int (l [len ('.. [' ):l .index (']' )]))
51
- except ValueError :
52
- print "WARNING: invalid reference in %s docstring" % name
53
-
54
- # Start renaming from the biggest number, otherwise we may
55
- # overwrite references.
56
- references .sort ()
47
+ for line in lines :
48
+ line = line .strip ()
49
+ m = re .match (r'^.. \[([a-z0-9_.-])\]' , line , re .I )
50
+ if m :
51
+ references .append (m .group (1 ))
52
+
53
+ # start renaming from the longest string, to avoid overwriting parts
54
+ references .sort (key = lambda x : - len (x ))
57
55
if references :
58
56
for i , line in enumerate (lines ):
59
57
for r in references :
60
- new_r = reference_offset [0 ] + r
61
- lines [i ] = lines [i ].replace ('[%d]_' % r ,
62
- '[%d]_' % new_r )
63
- lines [i ] = lines [i ].replace ('.. [%d]' % r ,
64
- '.. [%d]' % new_r )
58
+ if re .match (r'^\d+$' , r ):
59
+ new_r = "R%d" % (reference_offset [0 ] + int (r ))
60
+ else :
61
+ new_r = "%s%d" % (r , reference_offset [0 ])
62
+ lines [i ] = lines [i ].replace ('[%s]_' % r ,
63
+ '[%s]_' % new_r )
64
+ lines [i ] = lines [i ].replace ('.. [%s]' % r ,
65
+ '.. [%s]' % new_r )
65
66
66
67
reference_offset [0 ] += len (references )
67
68
0 commit comments