python-sphinxcross-referencedocutils

Supress cross reference hyperlink using exclamation mark


Prefixing the link with an ! suppresses the creation of a reference (e.g. :ref:`!no link` will be simply rendered as no link):

If you prefix the content with !, no reference/hyperlink will be created.

However, I can't think of any practical usage of this. Why should I first create a reference and then don't want to use it - it would be far easier to write plain text from the very beginning.

So - what is a typical use case of such a suppressed reference?

(Sphinx itself for instance doesn't use it in its docs.)


Solution

  • I can't think of any practical usage of this.

    The use I can think of is before a build if you wanted to "turn of" hyperlink generation for one given cross-reference (that appears multiple times) how would you do it?

    Well, the simplest way might be using some text editor's "find and replace", and arguably the least invasive way would be to add or remove a single character !. That way the length and structure of the cross-reference is kept in the source (and the title is still rendered in place). This could be convenient in several places, like a table where removing the whole cross-reference could misalign the source.

    The most economical change possible would be turning this:

    :ref:`a very long title <an.extremely.long.link.target>`
    

    into this:

    :ref:`!a very long title <an.extremely.long.link.target>`
    

    The same could possibly be achieved programmatically using the Sphinx API, but a lot of Sphinx users are likely to prefer a text editing solution over a programmatic one.