diff --git a/doc/extdev/appapi.rst b/doc/extdev/appapi.rst index 961558600a..d5b1a8a982 100644 --- a/doc/extdev/appapi.rst +++ b/doc/extdev/appapi.rst @@ -230,11 +230,15 @@ connect handlers to the events. Example: .. event:: missing-reference (app, env, node, contnode) - Emitted when a cross-reference to a Python module or object cannot be - resolved. If the event handler can resolve the reference, it should return a + Emitted when a cross-reference to an object cannot be resolved. + If the event handler can resolve the reference, it should return a new docutils node to be inserted in the document tree in place of the node *node*. Usually this node is a :class:`reference` node containing *contnode* as a child. + If the handler can not resolve the cross-reference, + it can either return ``None`` to let other handlers try, + or raise :class:`NoUri` to prevent other handlers in trying and suppress + a warning about this cross-reference being unresolved. :param env: The build environment (``app.builder.env``). :param node: The :class:`pending_xref` node to be resolved. Its attributes diff --git a/sphinx/errors.py b/sphinx/errors.py index a9d027cb8d..d9a83712c0 100644 --- a/sphinx/errors.py +++ b/sphinx/errors.py @@ -116,7 +116,8 @@ class PycodeError(Exception): class NoUri(Exception): - """Raised by builder.get_relative_uri() if there is no URI available.""" + """Raised by builder.get_relative_uri() or from missing-reference handlers + if there is no URI available.""" pass diff --git a/sphinx/transforms/post_transforms/__init__.py b/sphinx/transforms/post_transforms/__init__.py index 5bd9723e04..742da9d82a 100644 --- a/sphinx/transforms/post_transforms/__init__.py +++ b/sphinx/transforms/post_transforms/__init__.py @@ -92,7 +92,8 @@ class ReferencesResolver(SphinxPostTransform): # no new node found? try the missing-reference event if newnode is None: newnode = self.app.emit_firstresult('missing-reference', self.env, - node, contnode) + node, contnode, + allowed_exceptions=(NoUri)) # still not found? warn if node wishes to be warned about or # we are in nit-picky mode if newnode is None: