43
43
HASH_LEN = 12
44
44
45
45
46
+ def _traverse_or_findall (node , condition , ** kwargs ):
47
+ """Triage node.traverse (docutils <0.18.1) vs node.findall.
48
+
49
+ TODO: This check can be removed when the minimum supported docutils version
50
+ for numpydoc is docutils>=0.18.1
51
+ """
52
+ return (
53
+ node .findall (condition , ** kwargs )
54
+ if hasattr (node , "findall" )
55
+ else node .traverse (condition , ** kwargs )
56
+ )
57
+
58
+
46
59
def rename_references (app , what , name , obj , options , lines ):
47
60
# decorate reference numbers so that there are no duplicates
48
61
# these are later undecorated in the doctree, in relabel_references
@@ -81,8 +94,12 @@ def is_docstring_section(node):
81
94
return False
82
95
83
96
sibling_sections = itertools .chain (
84
- section_node .traverse (
85
- is_docstring_section , include_self = True , descend = False , siblings = True
97
+ _traverse_or_findall (
98
+ section_node ,
99
+ is_docstring_section ,
100
+ include_self = True ,
101
+ descend = False ,
102
+ siblings = True ,
86
103
)
87
104
)
88
105
for sibling_section in sibling_sections :
@@ -101,7 +118,7 @@ def is_docstring_section(node):
101
118
102
119
def relabel_references (app , doc ):
103
120
# Change 'hash-ref' to 'ref' in label text
104
- for citation_node in doc . traverse ( citation ):
121
+ for citation_node in _traverse_or_findall ( doc , citation ):
105
122
if not _is_cite_in_numpydoc_docstring (citation_node ):
106
123
continue
107
124
label_node = citation_node [0 ]
@@ -121,18 +138,18 @@ def matching_pending_xref(node):
121
138
and node [0 ].astext () == f"[{ ref_text } ]"
122
139
)
123
140
124
- for xref_node in ref .parent . traverse ( matching_pending_xref ):
141
+ for xref_node in _traverse_or_findall ( ref .parent , matching_pending_xref ):
125
142
xref_node .replace (xref_node [0 ], Text (f"[{ new_text } ]" ))
126
143
ref .replace (ref_text , new_text .copy ())
127
144
128
145
129
146
def clean_backrefs (app , doc , docname ):
130
147
# only::latex directive has resulted in citation backrefs without reference
131
148
known_ref_ids = set ()
132
- for ref in doc . traverse ( reference , descend = True ):
149
+ for ref in _traverse_or_findall ( doc , reference , descend = True ):
133
150
for id_ in ref ["ids" ]:
134
151
known_ref_ids .add (id_ )
135
- for citation_node in doc . traverse ( citation , descend = True ):
152
+ for citation_node in _traverse_or_findall ( doc , citation , descend = True ):
136
153
# remove backrefs to non-existent refs
137
154
citation_node ["backrefs" ] = [
138
155
id_ for id_ in citation_node ["backrefs" ] if id_ in known_ref_ids
0 commit comments