Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3fe382d

Browse files
committed
Document sphinxext.roles
1 parent 8e95749 commit 3fe382d

File tree

4 files changed

+66
-9
lines changed

4 files changed

+66
-9
lines changed

doc/api/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ Alphabetical list of modules:
126126
sphinxext_mathmpl_api.rst
127127
sphinxext_plot_directive_api.rst
128128
sphinxext_figmpl_directive_api.rst
129+
sphinxext_roles.rst
129130
spines_api.rst
130131
style_api.rst
131132
table_api.rst

doc/api/next_api_changes/development/28289-ES.rst

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,5 @@ Documentation-specific custom Sphinx roles are now semi-public
33

44
For third-party packages that derive types from Matplotlib, our use of custom roles may
55
prevent Sphinx from building their docs. These custom Sphinx roles are now public solely
6-
for the purposes of use within projects that derive from Matplotlib types, and may be
7-
added to Sphinx via ``conf.py``::
8-
9-
extensions = [
10-
'matplotlib.sphinxext.roles',
11-
# Other extensions.
12-
]
13-
14-
Any other use of these roles is not supported.
6+
for the purposes of use within projects that derive from Matplotlib types. See
7+
:mod:`matplotlib.sphinxext.roles` for details.

doc/api/sphinxext_roles.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
==============================
2+
``matplotlib.sphinxext.roles``
3+
==============================
4+
5+
.. automodule:: matplotlib.sphinxext.roles
6+
:no-undoc-members:
7+
:private-members: _rcparam_role, _mpltype_role

lib/matplotlib/sphinxext/roles.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,40 @@
1+
"""
2+
Custom roles for the Matplotlib documentation.
3+
4+
.. warning::
5+
6+
These roles are considered semi-public. They are only intended to be used in
7+
the Matplotlib documentation.
8+
9+
However, it can happen that downstream packages end up pulling these roles into
10+
their documentation, which will result in documentation build errors. The following
11+
describes the exact mechanism and how to fix the errors.
12+
13+
There are two ways, Matplotlib docstrings can end up in downstream documentation.
14+
You have to subclass a Matplotlib class and either use the ``:inherited-members:``
15+
option in your autodoc configuration, or you have to override a method without
16+
specifying a new docstring; the new method will inherit the original docstring and
17+
still render in your autodoc. If the docstring contains one of the custom sphinx
18+
roles, you'll see one of the following error messages:
19+
20+
.. code-block:: none
21+
22+
Unknown interpreted text role "mpltype".
23+
Unknown interpreted text role "rc".
24+
25+
To fix this, you can add this module as extension to your sphinx :file:`conf.py`::
26+
27+
extensions = [
28+
'matplotlib.sphinxext.roles',
29+
# Other extensions.
30+
]
31+
32+
.. warning::
33+
34+
Direct use of these roles in other packages is not officially supported. We
35+
reserve the right to modify or remove these roles without prior notification.
36+
"""
37+
138
from urllib.parse import urlsplit, urlunsplit
239

340
from docutils import nodes
@@ -42,6 +79,13 @@ def _depart_query_reference_node(self, node):
4279

4380

4481
def _rcparam_role(name, rawtext, text, lineno, inliner, options=None, content=None):
82+
"""
83+
Sphinx role ``:rc:`` to highlight and link ``rcParams`` entries.
84+
85+
Usage: Give the desired ``rcParams`` key as parameter.
86+
87+
:code:`:rc:`figure.dpi`` will render as: :rc:`figure.dpi`
88+
"""
4589
# Generate a pending cross-reference so that Sphinx will ensure this link
4690
# isn't broken at some point in the future.
4791
title = f'rcParams["{text}"]'
@@ -66,6 +110,18 @@ def _rcparam_role(name, rawtext, text, lineno, inliner, options=None, content=No
66110

67111

68112
def _mpltype_role(name, rawtext, text, lineno, inliner, options=None, content=None):
113+
"""
114+
Sphinx role ``:mpltype:`` for custom matplotlib types.
115+
116+
In Matplotlib, there are a number of type-like concepts that do not have a
117+
direct type representation; example: color. This role allows to properly
118+
highlight them in the docs and link to their definition.
119+
120+
Currently supported values:
121+
122+
- :code:`:mpltype:`color`` will render as: :mpltype:`color`
123+
124+
"""
69125
mpltype = text
70126
type_to_link_target = {
71127
'color': 'colors_def',

0 commit comments

Comments
 (0)