sphinx icon indicating copy to clipboard operation
sphinx copied to clipboard

Allow to use a caption without pattern replacement with extlinks extension

Open n-peugnet opened this issue 1 year ago • 4 comments

Is your feature request related to a problem? Please describe. In our documentation, we have a lot of links to Wikipedia articles to learn more about some defined terms, for example in the glossary: https://club1.fr/docs/en/glossaire.html All these links simply show the text "Wikipedia", as it is not useful to add the term once again in the link.

Describe the solution you'd like Thus, I would like to be able to use a caption without pattern replacement with extlinks extension, so that I could use a configuration that looks like this:

extlinks = {'wikipedia': ('https://en.wikipedia.org/wiki/%s', 'Wikipedia')}

After looking at the code, I think it would be possible to simply surround this line in a try...except statement, and using caption as the title in the "except block". https://github.com/sphinx-doc/sphinx/blob/a408ec5f519ee316b53743a62c8d72ca646cce9f/sphinx/ext/extlinks.py#L109

I thought that it would be more reliable than trying to detect if there is a pattern in the caption. What do you think about it?

n-peugnet avatar Feb 13 '24 20:02 n-peugnet

How funny, I indeed looked for a similar feature some weeks ago so, as a totally biased maintainer, I'll say it's an important feature indeed !

I think we can enhance extlinks a bit differently by considering a more powerful pattern where we would use format strings instead of C-format styles. I prefer not to have a try-except block to be sure that we are not suppressing a real error (and I don't like matching the error message since those may change depending on the versions), so I think we can enhance the pattern logic by testing whether %s is in the caption or not (note that we don't want to match %%s but really %s).

In the future, it might be better do deprecate this format and switch to format-strings instead and maybe add some better placeholders.

picnixz avatar Feb 14 '24 10:02 picnixz

I prefer not to have a try-except block to be sure that we are not suppressing a real error (and I don't like matching the error message since those may change depending on the versions), so I think we can enhance the pattern logic by testing whether %s is in the caption or not (note that we don't want to match %%s but really %s).

I get your point, thank you for your insights. I could prepare a PR for this in a few days if you'd like.

n-peugnet avatar Feb 14 '24 14:02 n-peugnet

I won't be available this week-end but yes, you can do it and I'll review it next week.

picnixz avatar Feb 14 '24 16:02 picnixz

By the way, you might want to have a look at https://github.com/sphinx-doc/sphinx/issues/1622. I don't think your suggestion conflicts with what was suggested, but just keep in mind to have a simple approach (I'm not sure whether there is something in the string built-in module).

picnixz avatar Feb 14 '24 16:02 picnixz