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
+
1
38
from urllib .parse import urlsplit , urlunsplit
2
39
3
40
from docutils import nodes
@@ -42,6 +79,13 @@ def _depart_query_reference_node(self, node):
42
79
43
80
44
81
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
+ """
45
89
# Generate a pending cross-reference so that Sphinx will ensure this link
46
90
# isn't broken at some point in the future.
47
91
title = f'rcParams["{ text } "]'
@@ -66,6 +110,18 @@ def _rcparam_role(name, rawtext, text, lineno, inliner, options=None, content=No
66
110
67
111
68
112
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
+ """
69
125
mpltype = text
70
126
type_to_link_target = {
71
127
'color' : 'colors_def' ,
0 commit comments