-
Notifications
You must be signed in to change notification settings - Fork 319
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
DOCS: admonition customization #1155
Changes from all commits
6102787
8b26bc8
2f58e3b
0a58f11
a3e64ab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
=================== | ||
Extending the theme | ||
=================== | ||
|
||
There are many extensions available for Sphinx that can enhance your site or provide powerful customization abilities. Here we describe a few customizations that are popular with ``pydata-sphinx-theme`` users. | ||
|
||
Collapsible admonitions | ||
======================= | ||
|
||
The `sphinx-togglebutton <https://sphinx-togglebutton.readthedocs.io/en/latest/>`__ extension provides optional show/hide behavior for admonitions. Follow their installation instructions, then add it to the ``extentions`` list in your ``conf.py``: | ||
|
||
.. code:: python | ||
|
||
extensions = [ | ||
# [...] | ||
"sphinx_togglebutton" | ||
] | ||
|
||
Then add the ``dropdown`` class to any admonition directive (shown here on a ``note`` admonition): | ||
|
||
.. begin-example-dropdown | ||
.. note:: | ||
:class: dropdown | ||
|
||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. | ||
.. end-example-dropdown | ||
|
||
.. tab-set:: | ||
|
||
.. tab-item:: rst | ||
|
||
.. include:: ./extending.rst | ||
:start-after: begin-example-dropdown | ||
:end-before: .. end-example-dropdown | ||
:code: rst | ||
:class: highlight-rst | ||
|
||
|
||
Custom admonition styles | ||
======================== | ||
|
||
A `limited set <https://docutils.sourceforge.io/docs/ref/rst/directives.html#admonitions>`__ of admonitions are built-in to docutils (the rST → HTML engine that underlies Sphinx). However, it is possible to create custom admonitions with their own default colors, icons, and titles. | ||
|
||
|
||
Customizing the title | ||
--------------------- | ||
|
||
Although most admonitions have a default title like ``note`` or ``warning``, a generic ``admonition`` directive is built-in to docutils/Sphinx. In this theme, its color defaults to the same color as ``note`` admonitions, and it has a bell icon: | ||
|
||
.. begin-example-title | ||
.. admonition:: Custom title! | ||
|
||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. | ||
.. end-example-title | ||
|
||
The title is specified on the same line as the ``.. admonition::`` directive: | ||
|
||
.. tab-set:: | ||
|
||
.. tab-item:: rst | ||
|
||
.. include:: ./extending.rst | ||
:start-after: begin-example-title | ||
:end-before: .. end-example-title | ||
:code: rst | ||
:class: highlight-rst | ||
|
||
|
||
Styling with semantic color names | ||
--------------------------------- | ||
|
||
You can re-style any admonition to match any of the built-in admonition types using any of the semantic color names as a class (this is most useful for custom-titled admonitions): | ||
|
||
.. begin-example-semantic | ||
.. admonition:: Custom title with "warning" style | ||
:class: warning | ||
|
||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. | ||
.. end-example-semantic | ||
|
||
Note that it updates both the color and the icon. | ||
|
||
.. tab-set:: | ||
|
||
.. tab-item:: rst | ||
|
||
.. include:: ./extending.rst | ||
:start-after: begin-example-semantic | ||
:end-before: .. end-example-semantic | ||
:code: rst | ||
:class: highlight-rst | ||
|
||
This theme defines classes for `the standard docutils admonition types <https://docutils.sourceforge.io/docs/ref/rst/directives.html#admonitions>`__ (``attention``, ``caution``, etc) and additionally supports ``seealso`` and ``todo`` admonitions (see :doc:`../examples/kitchen-sink/admonitions` for a demo of all built-in admonition styles). | ||
|
||
|
||
Customizing the color | ||
--------------------- | ||
|
||
Besides the pre-defined semantic color classes (see previous section) you can also add a bespoke color to any admonition by defining your own CSS class. Example: | ||
|
||
.. begin-example-color | ||
.. admonition:: Admonition with custom "olive" color | ||
:class: admonition-olive | ||
|
||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. | ||
.. end-example-color | ||
|
||
Add the new class to your `custom.css <https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-html_css_files>`__ file. As in the example below, be sure to use the same color for ``border-color``, ``background-color``, and ``color`` (the transparency effect is handled automatically by the theme). | ||
|
||
.. tab-set:: | ||
|
||
.. tab-item:: rst | ||
|
||
.. include:: ./extending.rst | ||
:start-after: begin-example-color | ||
:end-before: .. end-example-color | ||
:code: rst | ||
:class: highlight-rst | ||
|
||
.. tab-item:: css | ||
|
||
.. include:: ../_static/custom.css | ||
:start-after: begin-custom-color | ||
:end-before: /* end-custom-color | ||
:code: css | ||
:class: highlight-css | ||
|
||
|
||
Using a custom icon | ||
------------------- | ||
|
||
Customizing the icon uses a similar process to customizing the color: create a new CSS class in your `custom.css <https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-html_css_files>`__ file. The theme supports `fontawesome v6 icons <https://fontawesome.com/v6/search?o=r&m=free&f=brands>`__ ("free" and "brands" sets). To use an icon, copy its unicode value into your custom class as shown in the CSS tab below: | ||
|
||
.. begin-example-icon | ||
.. admonition:: Check out my custom icon | ||
:class: admonition-icon | ||
|
||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. | ||
.. end-example-icon | ||
|
||
.. tab-set:: | ||
|
||
.. tab-item:: rst | ||
|
||
.. include:: ./extending.rst | ||
:start-after: begin-example-icon | ||
:end-before: .. end-example-icon | ||
:code: rst | ||
:class: highlight-rst | ||
|
||
.. tab-item:: css | ||
|
||
.. include:: ../_static/custom.css | ||
:start-after: begin-custom-icon | ||
:end-before: /* end-custom-icon | ||
:code: css | ||
:class: highlight-css | ||
|
||
|
||
Combining all three customizations | ||
---------------------------------- | ||
|
||
Here we demonstrate an admonition with a custom icon, color, and title (and also make it collapsible). Note that the multiple admonition class names are space-separated: | ||
|
||
.. begin-example-youtube | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually created a tiny little sphinx extension to show off syntax examples, maybe it would be useful here? https://ebp-sphinx-examples.readthedocs.io/en/latest/ It gives you an example directive like:
And it create a little preview + the raw syntax to show how to make the preview. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that would be perfect but you can't display 2 files yet right (like in these examples css + rst) ? I will merge it and we'll see how to use it in the upcoming future |
||
.. admonition:: YouTube | ||
:class: dropdown admonition-youtube | ||
|
||
.. youtube:: dQw4w9WgXcQ | ||
.. end-example-youtube | ||
|
||
.. tab-set:: | ||
|
||
.. tab-item:: rst | ||
|
||
.. include:: ./extending.rst | ||
:start-after: begin-example-youtube | ||
:end-before: .. end-example-youtube | ||
:code: rst | ||
:class: highlight-rst | ||
|
||
.. tab-item:: css | ||
|
||
.. include:: ../_static/custom.css | ||
:start-after: begin-custom-youtube | ||
:end-before: /* end-custom-youtube | ||
:code: css | ||
:class: highlight-css |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,7 @@ keyboard-shortcuts | |
theme-elements | ||
ablog | ||
web-components | ||
extending | ||
``` | ||
|
||
```{toctree} | ||
|
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.
@12rambau any reason you picked
f26c
here instead of f167?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.
yes, the youtube icon is a brand icon, if you want to use it you also need to customize the font-family from "Font Awesome 6 Free" to "Font Awesome 6 Brands".
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.
Ah ok, so the brand icons don't work easily in an admonition? I didn't realize that.
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.
nope it doesn't, I saw it, tried to find an easy fix and realize we need to update fa font management first so I keep it in my mind and if someone complains about it I'll work on it but that's low priority I think