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