-
Notifications
You must be signed in to change notification settings - Fork 328
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
Handle dots in id
attributes for Python module members with custom HTMLTranslator
#1207
Labels
needs: more information
Needs more information from the author before we can move forward
Comments
this issue is bugging me for so long (#1026) thank you so much for bringing the solution on a plate ! I'm +10000 to make it part of our translator mixin. Would you have time to start a PR ? |
Yes, no problem! I can submit a PR this week. |
bheberlein
added a commit
to bheberlein/pydata-sphinx-theme
that referenced
this issue
Feb 21, 2023
This will replace dots with underscores in qualified names of Python modules & functions (e.g. `mypackage.mymodule.myfunc`), allowing ScrollSpy to identify these tags as navigation targets. Addresses pydata#1207 and fixes pydata#1026
bheberlein
added a commit
to bheberlein/pydata-sphinx-theme
that referenced
this issue
Feb 23, 2023
This will replace dots with underscores in qualified names of Python modules & functions (e.g. `mypackage.mymodule.myfunc`), allowing ScrollSpy to identify these tags as navigation targets. Addresses pydata#1207 and fixes pydata#1026
12rambau
added a commit
that referenced
this issue
Apr 25, 2023
…#1208) * Override generation of `id` & `href` attributes for API documentation This will replace dots with underscores in qualified names of Python modules & functions (e.g. `mypackage.mymodule.myfunc`), allowing ScrollSpy to identify these tags as navigation targets. Addresses #1207 and fixes #1026 * Apply linter corrections & add comment referencing open Sphinx issue --------- Co-authored-by: Rambaud Pierrick <[email protected]>
I think the merge of your proposed PR solve the issue? Could you confirm and if not the case let us know what remain to be done? |
12rambau
added
the
needs: more information
Needs more information from the author before we can move forward
label
May 19, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'll preface this by saying I am not entirely sure if this is more properly a request for PST or for Sphinx.
When I view my generated package index from
sphinx-apidoc
, I'd like to have a responsive secondary sidebar that expands & collapses individual submodules in the navigation TOC. However, because of Python namespace conventions, theid
attributes of generated HTML<section>
&<dt>
tags contain dots, e.g.<section id="mypackage.submodule1"> ... </section>
and<dt id="mypackage.submodule1.func_a"> ... </dt>
. Because of this, ScrollSpy does not detect these tags as targets, and the auto-expand & auto-collapse functionality we see on the sidebars of 'normal' pages do not work here.Because of PST's reliance on ScrollSpy for responsive layouts, and because un-escaped
.
in a CSSid
attribute is incompatible with ScrollSpy (and not really valid CSS anyway) it seems to make sense to handle this for documented package members.I thought
sphinx-build
might provide options to handle this out-of-the-box, but I wasn't able to discover any way to configure this.Sketch of a solution
The
id
attributes can be made valid by either escaping with a backslash (mypackage\.submodule1
) or by substituting some other character, e.g. an underscore (mypackage_submodule1
). I opted for the latter here, but it's really a question of preference. Maybe escaping the.
would be more faithful to the Python namespace conventions (though I consider the underscore a bit nicer myself).I'll note that it may also be possible to achieve the desired results by modifying
bootstrap.js
, though JavaScript is not my area of expertise. It would be necessary to modify thegetSelectorFromElement()
function, which returnsnull
becausequerySelector()
fails on IDs containing dots.Anyway, I believe we can also handle this with a custom
HTMLTranslator
class that implements the following methods:Of course, PST already implements
BootstrapHTML5TranslatorMixin
to do some customization of the HTML translation process. So the above methods can be added directly to this mixin class to achieve my desired functionality. I am not sure if adding these would mess things up anywhere else, but I haven't found any such downsides.I'm curious what others think about this. Is it better for the end-user to implement this in their own custom
HTMLTranslator
class? If so, how would this even work? I ran into some difficulty with it since it seems to conflict with PST's own initialization routine (see below).Attempt to implement solution on the user end
First I tried to add the following to my
conf.py
, along with the custom class definition shown above:This does not work, maybe because it is overridden by PST's custom translator? The documentation builds successfully, but I don't see any changes reflected in the output HTML. So I tried
but this gives an error:
Finally I tried
but then I get
I've never tried to implement a custom translator for Sphinx, and I'll admit I don't really know where to go from here. I'll note that this might suggest an issue with PST — how are end-users supposed to implement a custom translator class if they want to? Is there a way to extend the PST custom
BootstrapHTML5Translator
class? If so, can we add some explanation of how to do this to the docs? I can open a separate issue for this if appropriate.It might also make sense to open an issue with the Sphinx maintainers. But in the interest of supporting a responsive theme that is compatible with existing or older Sphinx versions, I thought I would bring this up here first.
Proposed changes to PST
Add the custom
visit_section
,visit_reference
andvisit_desc_signature
methods shown above toBootstrapHTML5TranslatorMixin
. I tried it, and everything works just as desired.The text was updated successfully, but these errors were encountered: