-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
[rst] Add collapsible
option to admonition directives
#12507
base: master
Are you sure you want to change the base?
[rst] Add collapsible
option to admonition directives
#12507
Conversation
collabsible
option to admonition directives
I'd be happy to accomodate for this downstream. |
For what it is worth, in Pydata Sphinx Theme we have equivalent "dropdown" we use from sphinx-design: https://pydata-sphinx-theme.readthedocs.io/en/stable/user_guide/web-components.html#dropdowns Not pronouncing for everybody of the core team, but I think we would be happy with having this in core sphinx. I don't know if it's of use but there was an accessibility audit of PST, and I believe one of the findings was that the I guess that's more of a theming question. Would it make sens ":collapsible:" to take a boolean instead of a flag in case you want to make all collapsible by default and override with false ? |
In sphinx-immaterial theme, we conditionally override the admonitions to include this feature (among others). I implemented the .. admonition:: An optional title in sphinx-immaterial
:collapsible:
Closed by default.
.. admonition:: An optional title in sphinx-immaterial
:collapsible: open
Opened by default. Where the optional title becomes required if the It makes sense to have a separate |
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.
This doesn't effect the sphinx.ext.todo
admonitions. I imagine people would make an assumption that this feature is extended to .. todo::
directives as well.
if node.get('open'): | ||
self.body.append( | ||
self.starttag(node, 'details', CLASS=('admonition ' + name), open='open') | ||
) | ||
else: | ||
self.body.append(self.starttag(node, 'details', CLASS=('admonition ' + name))) |
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.
The repetition here could be reduced with a dict expanded for kwargs to starttag()
.
if node.get('open'): | |
self.body.append( | |
self.starttag(node, 'details', CLASS=('admonition ' + name), open='open') | |
) | |
else: | |
self.body.append(self.starttag(node, 'details', CLASS=('admonition ' + name))) | |
kwargs = {"CLASS": ('admonition ' + name)} | |
if node.get('open'): | |
kwargs["open"] = "open" | |
self.body.append(self.starttag(node, 'details', **kwargs)) |
We, at sphinx-immaterial, also satisfied a feature request to style the version directives as admonitions. Ironically, the request came from the author of the sphinx-material theme (sphinx-immaterial's ancestor) in which he assumed the version directives were types of admonitions. He isn't wrong, but the fact that the sphinx docs grouped all admonitions in with the version directives (as "Paragraph-level markup") led to the confusion (and he cited the pydata theme's styling of version directives). In resolving the request, I extended the collapsible behavior to the version directives as well. But I had to mandate the version directives had at least 2 directive arguments specified as well as directive content if the |
This makes sense and I am sure we can accommodate this in PST. A couple of thoughts/suggestions (for usability and accessibility)
Ref in PST: https://pydata-sphinx-theme.readthedocs.io/en/stable/user_guide/web-components.html#dropdowns Note that per the issue linked above by Matthias we are making some interaction design improvements (designed not implemented though). |
Thanks for the feedback @trallard, @Carreau, but would these not be theme specific concerns, i.e. these considerations would not change the actual HTML that is written, only the CSS? TBH I kind of feel the chevron is sufficient; |
Yes. This is using the The one difference is that the example here has got the browser-provided outline that PST's example does not have due to sphinx-design providing an |
Yeah that outline none was just changed in the latest Sphinx Design release which messed up with our visible focus indicator. And we yet have to fix. |
The admonition directive and admonition nodes in general allow for any title |
This is true about specific admonitions ( .. admonition:: Important Implementation Details
:collapsible:
:class: important
End-users probably won't know this unless they dig through our source code... @chrisjsewell You kinda beat me to this answer 🤣 But I added the bit about CSS classes |
Ok, you are right. It works for
It's worth documenting / illustrating the |
Yeah we overhauled the admonitions in sphinx-immaterial to allow for a custom title in our added I also think that docutils' generic admonition shouldn't mandate a title at all (a requirement sphinx-immaterial theme has removed), but that's getting off topic.
This tactic might be theme-specific. It depends on how the CSS for a theme is structured. |
Yeh... obviously a bit out of scope for this PR, but basically, these are in essence equivalent: .. note:: Content
.. admonition:: Note
:class: note
Content It's good to have the "shorthand", but indeed I feel there should be a middle ground:
I also don't really get in docutils, why they need to have separate, specific nodes for each admonition type, rather than a single sphinx/sphinx/writers/html5.py Lines 788 to 789 in e439c6f
|
collabsible
option to admonition directivescollapsible
option to admonition directives
yep, apready do this in myst-parser 😉 https://myst-parser.readthedocs.io/en/latest/syntax/admonitions.html#admonition-titles
it would only be theme specific, if they overrode the HTML writer, which I don't really think they should do |
This comment was marked as off-topic.
This comment was marked as off-topic.
Getting back on topic...
Is I'm also interested with what to do about the |
I would tend to gravitte toward
And let people play with true|false Then say "you can add a :collapsible:" option. I think that OTOH, I can see what you don't want an One thing that is not discussed is wether we can set the default state globally. maybe both collapsible and open should be a 3 state |
I'm afraid I'm not that adamant about what the option's value is. Rather, I'm more adamant about having a value instead of 2 new options to control the same feature.
exactly |
Since this feature directly relates to HTML I did come across an implied suggestion that the sphinx-immaterial theme's custom admonitions feature's configuration should allow certain admonition directives to be collapsible by default. I wasn't interested in maintaining the idea because that theme's implementation for collapsible behavior adds requirements about other admonition properties (like requiring a title and requiring 2 directive arguments + directive content for the version directives). |
I agree with having the default behaviour as collapsed and allowing the users to change the state of a certain accordion (collapsible admonition) if needed. As for the state wording/indicator, I'd like to suggest |
I have no problems with that idea. I think the <details><summary>Title</summary>
collapsed by default.
</details>
<details open="false"><summary>Title</summary>
expanded by default.
</details> renders as: Titlecollapsed by default. Titleexpanded by default. This is why the term |
I understand that is the HTML specification and that should be followed.
As this is an option to be surfaced to the user to control whether a collapsible admonition would be presented as collapsed/expanded by default in a documentation page (not globally). |
Sphinx's extensions often go for |
Keeping the vocabulary simple would be more cohesive with non-English speakers as well. Behaviorally speaking, I still think it doesn't matter what the vocabulary is for a "collapsed" state... |
edit: see https://sphinx--12507.org.readthedocs.build/en/12507/usage/restructuredtext/directives.html#directive-note, for an example usage and rendering
This PR adds
collapsible
andopen
flag options to the core admonition type directives, which are propagated to the nodes, e.g.For the HTML5 writer, this replaces the outer
div
with adetails
tag, and the titlep
with asummary
tag (with anopen
attribute if set), e.g.For all other writers, these attributes are ignored.
I feel this a better alternative to #10532, since:
This would inherently require additions to theme extensions, to correctly style such admonitions.
But I feel this would be worth the effort (and already necessary anyway for #10532),
(in fact, it has already been added in https://jbms.github.io/sphinx-immaterial/admonitions.html#directive-option-admonition-collapsible,
which in-turn mirrors https://squidfunk.github.io/mkdocs-material/reference/admonitions/#collapsible-blocks)
cc @pradyunsg for comment
TODO: tests, documentation, addition of CSS styling to core/docs theme