New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sphinx.ext.extlinks: Allow %s
in link caption string
#8898
Conversation
Tweak syntax of extlinks to also allow ``%s`` in the link caption part. Like for the base URL ``%s`` will be substituted with the content of the role. This allows configurations like extlinks = {'quarter': ('https://example.org/quaters/%s', '%s. quarter')} with ``:quarter:`2``` getting replaced by a link titled `2. quarter`. The requirement for the caption string is to be either None or contain exactly one ``%s``. If neither is the case, then we emit a warning and fall back to the old behaviour which is concatenating the caption string with the role content.
Is there any interest in picking this up? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the late response. Good idea. But it would be better to keep compatibility. Could you reconsider the behavior?
Hi @tk0miya Thanks for coming back to me. @tk0miya in the review:
For an old config the behaviour of Sphinx is the same as previous (i.e. use it as a prefix) except additionally a warning will be printed. In particular, Sphinx generates the same documentation output as previously, so there will be no breakage and no one is forced to upgrade their config to continue building their project. Instead, we gently tip them off to the newer syntax using the warning, so they might update if they fell like it. This is the same change as was done when upgrading the URL-string to allow a I admit the documentation is strongly worded with a "must have" and "exactly one", if you like, I rephrase that to the more handwavy phrasing used for the URL part (which I didn't touch but has exactly the behaviour now proposed for If you still feel that we should (silently) fall back to the old behaviour instead of printing a warning. Then I'll redo that part and the documentation. Maybe we can add a "Falling back to old prefix behaviour" to the warning text, to make it clear what happened. |
Thank you for your explanation. Finally, I understand what you're doing. And my last comment was bad. Indeed, the code keeps compatibility. Now I'm debating about the deprecation step of the configuration. I hesitate the new warning causes CI errors in many projects (because they use |
We use Pythons %-formatting, so literal ``%`` must be escaped as ``%%``. Clarify this behaviour for the caption and base URL strings.
The old syntax will be deprecated in 4.x and 5.x and removed in Sphinx 6.0.
@tk0miya wrote:
Reading through the projects Deprecation Policy [0] (I assume this feature hits Sphinx 4.0 or 4.1).
To me this looks like a clean and consistent way of getting rid of old cruft while giving users enough time to adapt (Well done!). The most affected ones are arguably developers and CI, which already know the drill. I updated the code accordingly and took inspiration from [0] https://www.sphinx-doc.org/en/master/internals/release-process.html |
Thank you for the updates. LGTM! |
Note: To let users know the deprecation, I'll use |
I wrongly merged this into the 3.5.x branch... To fix my mistake up, I reverted and cherry-picked these commits on our repository directly (without force push). |
Thank you for your contribution! |
Thanks to you for merging and reviewing! |
docs: Add versionchanged tag to extlinks (refs: #8898)
Subject: sphinx.ext.extlinks: Allow
%s
in link caption stringFeature or Bugfix
Purpose
%s
in link caption string of the sphinx.ext.extlinks extensions, which allows arbitrary placement of the role content in the link caption.prefix + rolecontent
withprefix % rolecontent
.Detail
Currently sphinx.ext.extlinks ("extlinks") allows placing the role contents (often a number)
only at the end of the link caption, like so "issue 123". However, sometimes it is needed
to place the number (or text in general) at a different location, e.g. "1. quarter", "xyz house".
This patch changes the behaviour of the second part in the extlinks config. The caption
string now allows
%s
which gets replaced with the role content (the same substitutionwhich happens for base URL). Until now this string was called prefix this is changed to
caption. (In fact most of the changes in this diff are renaming prefix to caption.) If
you feel title is a more appropriate name, go ahead and complain.
Example:
conf.py
:index.rst
Output
Note on backwards compatibility:
It is now required that the second entry in the tuple is either None or a string which contains
exactly one
%s
. If this is not the case, then a warning is printed and we fall back to theold behaviour, i.e. concatenating the caption label with the role content. This is the same
mechanism as for the base URL. Thus, module a warning this is backwards compatible.
I've filed this merge request against 3.5.x, if you feel this is too much breakage, please
reassign to merge with 3.x.