Skip to content
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

Wrap annotations with an html tag #395

Closed
pfebrer opened this issue Nov 3, 2023 · 10 comments
Closed

Wrap annotations with an html tag #395

pfebrer opened this issue Nov 3, 2023 · 10 comments

Comments

@pfebrer
Copy link

pfebrer commented Nov 3, 2023

Is it possible to wrap the annotation in some custom html tag?

I want to style the whole annotation to have the same background, but looking at the generated HTML it looks like an unnecessarily daunting task, with parenthesis and commas being just text and merged with the description text. This is Union[str, int]:

Screenshot from 2023-11-03 14-16-03

which results in background changes for [, ] and , making it very hard to read:

Screenshot from 2023-11-03 14-18-36

@gaborbernat
Copy link
Member

PR welcome.

@pfebrer
Copy link
Author

pfebrer commented Nov 3, 2023

I would like to, but I don't know where to begin 😅

@pfebrer
Copy link
Author

pfebrer commented Nov 3, 2023

Should I be able to acheive this with typehints_formatter ?

@hoodmane
Copy link
Collaborator

hoodmane commented Nov 3, 2023

I did a similar thing in sphinx-js. It's largely a limitation of restructured text / the way that sphinx works. What I did is

  1. make a role which takes escaped rst, unescapes it, renders it into a docutils tree, and then wraps it with css span
    https://github.com/pyodide/sphinx-js-fork/blob/master/sphinx_js/directives.py#L55-L66
  2. when we render the type, escape your original rst and wrap it with the css anchor role:
    https://github.com/pyodide/sphinx-js-fork/blob/master/sphinx_js/renderers.py#L432

I'm not sure if this is the best way, it feels a bit hacky but this was the best way I could figure out to make it work.

You can see the result here: all the types have this css wrapper. If you adjust the css for .sphinx_js-type you can add your box:
https://pyodide.org/en/latest/usage/api/js-api.html#pyodide

Screenshot from 2023-11-03 11-51-48

@pfebrer
Copy link
Author

pfebrer commented Nov 4, 2023

Thanks for the comments!

After some investigation I realized that as you point out I have to go too deep into sphinx to implement this and I think it's not worth it for me to spend time on. I thought it would be easier, I didn't know the inner workings of sphinx were so complicated 😅

@hoodmane
Copy link
Collaborator

hoodmane commented Nov 7, 2023

I can try to do this when I get around to it. I think it's a very helpful feature.

@gaborbernat
Copy link
Member

Agreed 👍

hoodmane added a commit to hoodmane/sphinx-autodoc-typehints that referenced this issue Nov 8, 2023
hoodmane added a commit to hoodmane/sphinx-autodoc-typehints that referenced this issue Nov 8, 2023
hoodmane added a commit to hoodmane/sphinx-autodoc-typehints that referenced this issue Nov 8, 2023
@pfebrer
Copy link
Author

pfebrer commented Nov 8, 2023

By the way I wonder if it would make more sense to contribute this to sphinx.ext.autodoc. I don't really know exactly what sphinx.ext.autodoc wants to support in terms of typehints, I'm new to this sphinx typehint world. But it seems that its support for typehints is quite good, no? Maybe it wasn't like this in the past (?)

@gaborbernat
Copy link
Member

Their style of choice is different. Both packages can coexist.

@hoodmane
Copy link
Collaborator

hoodmane commented Nov 9, 2023

Resolved by #397.

@hoodmane hoodmane closed this as completed Nov 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants