@@ -178,41 +178,67 @@ class HyperlinkCollector(SphinxPostTransform):
178
178
default_priority = 800
179
179
180
180
def run (self , ** kwargs : Any ) -> None :
181
- builder = cast (CheckExternalLinksBuilder , self .app .builder )
182
- hyperlinks = builder .hyperlinks
183
- docname = self .env .docname
181
+ for node in self .document .findall ():
182
+ if uri := self .find_uri (node ):
183
+ self ._add_uri (uri , node )
184
+
185
+ def find_uri (self , node : nodes .Element ) -> str | None :
186
+ """Find a URI for a given node.
184
187
188
+ This call can be used to retrieve a URI from a provided node. If no
189
+ URI exists for a provided node, this call will return ``None``.
190
+
191
+ This method can be useful for extension developers who wish to
192
+ easily inject hyperlinks into a builder by only needing to override
193
+ this method.
194
+
195
+ :param node: A node class
196
+ :returns: URI of the node
197
+ """
185
198
# reference nodes
186
- for refnode in self .document .findall (nodes .reference ):
187
- if 'refuri' in refnode :
188
- uri = refnode ['refuri' ]
189
- _add_uri (self .app , uri , refnode , hyperlinks , docname )
199
+ if isinstance (node , nodes .reference ):
200
+ if 'refuri' in node :
201
+ return node ['refuri' ]
190
202
191
203
# image nodes
192
- for imgnode in self . document . findall ( nodes .image ):
193
- uri = imgnode ['candidates' ].get ('?' )
204
+ if isinstance ( node , nodes .image ):
205
+ uri = node ['candidates' ].get ('?' )
194
206
if uri and '://' in uri :
195
- _add_uri ( self . app , uri , imgnode , hyperlinks , docname )
207
+ return uri
196
208
197
209
# raw nodes
198
- for rawnode in self . document . findall ( nodes .raw ):
199
- uri = rawnode .get ('source' )
210
+ if isinstance ( node , nodes .raw ):
211
+ uri = node .get ('source' )
200
212
if uri and '://' in uri :
201
- _add_uri (self .app , uri , rawnode , hyperlinks , docname )
213
+ return uri
214
+
215
+ return None
202
216
217
+ def _add_uri (self , uri : str , node : nodes .Element ) -> None :
218
+ """Registers a node's URI into a builder's collection of hyperlinks.
203
219
204
- def _add_uri ( app : Sphinx , uri : str , node : nodes . Element ,
205
- hyperlinks : dict [ str , Hyperlink ], docname : str ) -> None :
206
- if newuri := app . emit_firstresult ( ' linkcheck-process-uri' , uri ):
207
- uri = newuri
220
+ Provides the ability to register a URI value determined from a node
221
+ into the linkcheck's builder. URI's processed through this call can
222
+ be manipulated through a `` linkcheck-process-uri`` event before the
223
+ builder attempts to validate.
208
224
209
- try :
210
- lineno = get_node_line (node )
211
- except ValueError :
212
- lineno = - 1
225
+ :param uri: URI to add
226
+ :param node: A node class where the URI was found
227
+ """
228
+ builder = cast (CheckExternalLinksBuilder , self .app .builder )
229
+ hyperlinks = builder .hyperlinks
230
+ docname = self .env .docname
231
+
232
+ if newuri := self .app .emit_firstresult ('linkcheck-process-uri' , uri ):
233
+ uri = newuri
234
+
235
+ try :
236
+ lineno = get_node_line (node )
237
+ except ValueError :
238
+ lineno = - 1
213
239
214
- if uri not in hyperlinks :
215
- hyperlinks [uri ] = Hyperlink (uri , docname , app .env .doc2path (docname ), lineno )
240
+ if uri not in hyperlinks :
241
+ hyperlinks [uri ] = Hyperlink (uri , docname , self .env .doc2path (docname ), lineno )
216
242
217
243
218
244
class Hyperlink (NamedTuple ):
0 commit comments