| 1 |
.. highlight:: rst |
| 2 |
|
| 3 |
===== |
| 4 |
Roles |
| 5 |
===== |
| 6 |
|
| 7 |
Sphinx uses interpreted text roles to insert semantic markup into documents. |
| 8 |
They are written as ``:rolename:`content```. |
| 9 |
|
| 10 |
.. note:: |
| 11 |
|
| 12 |
The default role (```content```) has no special meaning by default. You are |
| 13 |
free to use it for anything you like, e.g. variable names; use the |
| 14 |
:confval:`default_role` config value to set it to a known role -- the |
| 15 |
:rst:role:`any` role to find anything or the :rst:role:`py:obj` role to find |
| 16 |
Python objects are very useful for this. |
| 17 |
|
| 18 |
See :doc:`/usage/domains/index` for roles added by domains. |
| 19 |
|
| 20 |
|
| 21 |
Cross-references |
| 22 |
---------------- |
| 23 |
|
| 24 |
See :doc:`/usage/referencing/`. |
| 25 |
|
| 26 |
Cross-reference roles include: |
| 27 |
|
| 28 |
* :rst:role:`any` |
| 29 |
* :rst:role:`doc` |
| 30 |
* :rst:role:`download` |
| 31 |
* :rst:role:`envvar` |
| 32 |
* :rst:role:`keyword` |
| 33 |
* :rst:role:`numref` |
| 34 |
* :rst:role:`option` (and the deprecated :rst:role:`!cmdoption`) |
| 35 |
* :rst:role:`ref` |
| 36 |
* :rst:role:`term` |
| 37 |
* :rst:role:`token` |
| 38 |
|
| 39 |
|
| 40 |
Inline code highlighting |
| 41 |
------------------------ |
| 42 |
|
| 43 |
.. rst:role:: code |
| 44 |
|
| 45 |
An *inline* code example. When used directly, this role just displays the |
| 46 |
text *without* syntax highlighting, as a literal. |
| 47 |
|
| 48 |
.. code-block:: rst |
| 49 |
|
| 50 |
By default, inline code such as :code:`1 + 2` just displays without |
| 51 |
highlighting. |
| 52 |
|
| 53 |
Displays: By default, inline code such as :code:`1 + 2` just displays without |
| 54 |
highlighting. |
| 55 |
|
| 56 |
Unlike the :rst:dir:`code-block` directive, this role does not respect the |
| 57 |
default language set by the :rst:dir:`highlight` directive. |
| 58 |
|
| 59 |
To enable syntax highlighting, you must first use the Docutils :dudir:`role` |
| 60 |
directive to define a custom role associated with a specific language: |
| 61 |
|
| 62 |
.. code-block:: rst |
| 63 |
|
| 64 |
.. role:: python(code) |
| 65 |
:language: python |
| 66 |
|
| 67 |
In Python, :python:`1 + 2` is equal to :python:`3`. |
| 68 |
|
| 69 |
To display a multi-line code example, use the :rst:dir:`code-block` directive |
| 70 |
instead. |
| 71 |
|
| 72 |
Math |
| 73 |
---- |
| 74 |
|
| 75 |
.. rst:role:: math |
| 76 |
|
| 77 |
Role for inline math. Use like this:: |
| 78 |
|
| 79 |
Since Pythagoras, we know that :math:`a^2 + b^2 = c^2`. |
| 80 |
|
| 81 |
Displays: Since Pythagoras, we know that :math:`a^2 + b^2 = c^2`. |
| 82 |
|
| 83 |
.. rst:role:: eq |
| 84 |
|
| 85 |
Same as :rst:role:`math:numref`. |
| 86 |
|
| 87 |
|
| 88 |
Other semantic markup |
| 89 |
--------------------- |
| 90 |
|
| 91 |
The following roles don't do anything special except formatting the text in a |
| 92 |
different style: |
| 93 |
|
| 94 |
.. rst:role:: abbr |
| 95 |
|
| 96 |
An abbreviation. If the role content contains a parenthesized explanation, |
| 97 |
it will be treated specially: it will be shown in a tool-tip in HTML, and |
| 98 |
output only once in LaTeX. |
| 99 |
|
| 100 |
For example: ``:abbr:`LIFO (last-in, first-out)``` displays |
| 101 |
:abbr:`LIFO (last-in, first-out)`. |
| 102 |
|
| 103 |
.. versionadded:: 0.6 |
| 104 |
|
| 105 |
.. rst:role:: command |
| 106 |
|
| 107 |
The name of an OS-level command, such as ``rm``. |
| 108 |
|
| 109 |
For example: :command:`rm` |
| 110 |
|
| 111 |
.. rst:role:: dfn |
| 112 |
|
| 113 |
Mark the defining instance of a term in the text. (No index entries are |
| 114 |
generated.) |
| 115 |
|
| 116 |
For example: :dfn:`binary mode` |
| 117 |
|
| 118 |
.. rst:role:: file |
| 119 |
|
| 120 |
The name of a file or directory. Within the contents, you can use curly |
| 121 |
braces to indicate a "variable" part, for example:: |
| 122 |
|
| 123 |
... is installed in :file:`/usr/lib/python3.{x}/site-packages` ... |
| 124 |
|
| 125 |
Displays: ... is installed in :file:`/usr/lib/python3.{x}/site-packages` ... |
| 126 |
|
| 127 |
In the built documentation, the ``x`` will be displayed differently to |
| 128 |
indicate that it is to be replaced by the Python minor version. |
| 129 |
|
| 130 |
.. rst:role:: guilabel |
| 131 |
|
| 132 |
Labels presented as part of an interactive user interface should be marked |
| 133 |
using ``guilabel``. This includes labels from text-based interfaces such as |
| 134 |
those created using :mod:`curses` or other text-based libraries. Any label |
| 135 |
used in the interface should be marked with this role, including button |
| 136 |
labels, window titles, field names, menu and menu selection names, and even |
| 137 |
values in selection lists. |
| 138 |
|
| 139 |
.. versionchanged:: 1.0 |
| 140 |
An accelerator key for the GUI label can be included using an ampersand; |
| 141 |
this will be stripped and displayed underlined in the output (for example: |
| 142 |
``:guilabel:`&Cancel``` displays :guilabel:`&Cancel`). To include a literal |
| 143 |
ampersand, double it. |
| 144 |
|
| 145 |
.. rst:role:: kbd |
| 146 |
|
| 147 |
Mark a sequence of keystrokes. What form the key sequence takes may depend |
| 148 |
on platform- or application-specific conventions. When there are no |
| 149 |
relevant conventions, the names of modifier keys should be spelled out, to |
| 150 |
improve accessibility for new users and non-native speakers. For example, |
| 151 |
an *xemacs* key sequence may be marked like ``:kbd:`C-x C-f```, but without |
| 152 |
reference to a specific application or platform, the same sequence should be |
| 153 |
marked as ``:kbd:`Control-x Control-f```, displaying :kbd:`C-x C-f` and |
| 154 |
:kbd:`Control-x Control-f` respectively. |
| 155 |
|
| 156 |
.. rst:role:: mailheader |
| 157 |
|
| 158 |
The name of an RFC 822-style mail header. This markup does not imply that |
| 159 |
the header is being used in an email message, but can be used to refer to |
| 160 |
any header of the same "style." This is also used for headers defined by |
| 161 |
the various MIME specifications. The header name should be entered in the |
| 162 |
same way it would normally be found in practice, with the camel-casing |
| 163 |
conventions being preferred where there is more than one common usage. For |
| 164 |
example: ``:mailheader:`Content-Type``` displays :mailheader:`Content-Type`. |
| 165 |
|
| 166 |
.. rst:role:: makevar |
| 167 |
|
| 168 |
The name of a :command:`make` variable. |
| 169 |
|
| 170 |
For example: :makevar:`help` |
| 171 |
|
| 172 |
.. rst:role:: manpage |
| 173 |
|
| 174 |
A reference to a Unix manual page including the section, e.g. |
| 175 |
``:manpage:`ls(1)``` displays :manpage:`ls(1)`. Creates a hyperlink to an |
| 176 |
external site rendering the manpage if :confval:`manpages_url` is defined. |
| 177 |
|
| 178 |
.. versionchanged:: 7.3 |
| 179 |
Allow specifying a target with ``<>``, like hyperlinks. |
| 180 |
For example, ``:manpage:`blah <ls(1)>``` displays :manpage:`blah <ls(1)>`. |
| 181 |
|
| 182 |
.. rst:role:: menuselection |
| 183 |
|
| 184 |
Menu selections should be marked using the ``menuselection`` role. This is |
| 185 |
used to mark a complete sequence of menu selections, including selecting |
| 186 |
submenus and choosing a specific operation, or any subsequence of such a |
| 187 |
sequence. The names of individual selections should be separated by |
| 188 |
``-->``. |
| 189 |
|
| 190 |
For example, to mark the selection "Start > Programs", use this markup:: |
| 191 |
|
| 192 |
:menuselection:`Start --> Programs` |
| 193 |
|
| 194 |
Displays: :menuselection:`Start --> Programs` |
| 195 |
|
| 196 |
When including a selection that includes some trailing indicator, such as |
| 197 |
the ellipsis some operating systems use to indicate that the command opens a |
| 198 |
dialog, the indicator should be omitted from the selection name. |
| 199 |
|
| 200 |
``menuselection`` also supports ampersand accelerators just like |
| 201 |
:rst:role:`guilabel`. |
| 202 |
|
| 203 |
.. rst:role:: mimetype |
| 204 |
|
| 205 |
The name of a MIME type, or a component of a MIME type (the major or minor |
| 206 |
portion, taken alone). |
| 207 |
|
| 208 |
For example: :mimetype:`text/plain` |
| 209 |
|
| 210 |
.. rst:role:: newsgroup |
| 211 |
|
| 212 |
The name of a Usenet newsgroup. |
| 213 |
|
| 214 |
For example: :newsgroup:`comp.lang.python` |
| 215 |
|
| 216 |
.. todo:: Is this not part of the standard domain? |
| 217 |
|
| 218 |
.. rst:role:: program |
| 219 |
|
| 220 |
The name of an executable program. This may differ from the file name for |
| 221 |
the executable for some platforms. In particular, the ``.exe`` (or other) |
| 222 |
extension should be omitted for Windows programs. |
| 223 |
|
| 224 |
For example: :program:`curl` |
| 225 |
|
| 226 |
.. rst:role:: regexp |
| 227 |
|
| 228 |
A regular expression. Quotes should not be included. |
| 229 |
|
| 230 |
For example: :regexp:`([abc])+` |
| 231 |
|
| 232 |
.. rst:role:: samp |
| 233 |
|
| 234 |
A piece of literal text, such as code. Within the contents, you can use |
| 235 |
curly braces to indicate a "variable" part, as in :rst:role:`file`. For |
| 236 |
example, in ``:samp:`print(1+{variable})```, the part ``variable`` would be |
| 237 |
emphasized: :samp:`print(1+{variable})` |
| 238 |
|
| 239 |
If you don't need the "variable part" indication, use the standard |
| 240 |
:rst:role:`code` role instead. |
| 241 |
|
| 242 |
.. versionchanged:: 1.8 |
| 243 |
Allowed to escape curly braces with double backslash. For example, in |
| 244 |
``:samp:`print(f"answer=\\{1+{variable}*2\\}")```, the part ``variable`` |
| 245 |
would be emphasized and the escaped curly braces would be displayed: |
| 246 |
:samp:`print(f"answer=\\{1+{variable}*2\\}")` |
| 247 |
|
| 248 |
There is also an :rst:role:`index` role to generate index entries. |
| 249 |
|
| 250 |
The following roles generate external links: |
| 251 |
|
| 252 |
.. rst:role:: cve |
| 253 |
|
| 254 |
A reference to a `Common Vulnerabilities and Exposures`_ record. |
| 255 |
This generates appropriate index entries. |
| 256 |
The text "CVE *number*\ " is generated; |
| 257 |
with a link to an online copy of the specified CVE. |
| 258 |
You can link to a specific section by using ``:cve:`number#anchor```. |
| 259 |
|
| 260 |
.. _Common Vulnerabilities and Exposures: https://www.cve.org/ |
| 261 |
|
| 262 |
For example: :cve:`2020-10735` |
| 263 |
|
| 264 |
.. versionadded:: 8.1 |
| 265 |
|
| 266 |
.. rst:role:: cwe |
| 267 |
|
| 268 |
A reference to a `Common Weakness Enumeration`_. |
| 269 |
This generates appropriate index entries. |
| 270 |
The text "CWE *number*\ " is generated; in the HTML output, |
| 271 |
with a link to an online copy of the specified CWE. |
| 272 |
You can link to a specific section by using ``:cwe:`number#anchor```. |
| 273 |
|
| 274 |
.. _Common Weakness Enumeration: https://cwe.mitre.org/ |
| 275 |
|
| 276 |
For example: :cwe:`787` |
| 277 |
|
| 278 |
.. versionadded:: 8.1 |
| 279 |
|
| 280 |
.. rst:role:: pep |
| 281 |
|
| 282 |
A reference to a Python Enhancement Proposal. This generates appropriate |
| 283 |
index entries. The text "PEP *number*\ " is generated; in the HTML output, |
| 284 |
this text is a hyperlink to an online copy of the specified PEP. You can |
| 285 |
link to a specific section by saying ``:pep:`number#anchor```. |
| 286 |
|
| 287 |
For example: :pep:`8` |
| 288 |
|
| 289 |
.. rst:role:: rfc |
| 290 |
|
| 291 |
A reference to an Internet Request for Comments. This generates appropriate |
| 292 |
index entries. The text "RFC *number*\ " is generated; in the HTML output, |
| 293 |
this text is a hyperlink to an online copy of the specified RFC. You can |
| 294 |
link to a specific section by saying ``:rfc:`number#anchor```. |
| 295 |
|
| 296 |
For example: :rfc:`2324` |
| 297 |
|
| 298 |
Note that there are no special roles for including hyperlinks as you can use |
| 299 |
the standard reStructuredText markup for that purpose. |
| 300 |
|
| 301 |
|
| 302 |
.. _default-substitutions: |
| 303 |
|
| 304 |
Substitutions |
| 305 |
------------- |
| 306 |
|
| 307 |
The documentation system provides some substitutions that are defined by |
| 308 |
default. They are set in the build configuration file. |
| 309 |
|
| 310 |
.. describe:: |release| |
| 311 |
|
| 312 |
Replaced by the project release the documentation refers to. This is meant |
| 313 |
to be the full version string including alpha/beta/release candidate tags, |
| 314 |
e.g. ``2.5.2b3``. Set by :confval:`release`. |
| 315 |
|
| 316 |
.. describe:: |version| |
| 317 |
|
| 318 |
Replaced by the project version the documentation refers to. This is meant to |
| 319 |
consist only of the major and minor version parts, e.g. ``2.5``, even for |
| 320 |
version 2.5.1. Set by :confval:`version`. |
| 321 |
|
| 322 |
.. describe:: |today| |
| 323 |
|
| 324 |
Replaced by either today's date (the date on which the document is read), or |
| 325 |
the date set in the build configuration file. Normally has the format |
| 326 |
``April 14, 2007``. Set by :confval:`today_fmt` and :confval:`today`. |
| 327 |
|
| 328 |
.. describe:: |translation progress| |
| 329 |
|
| 330 |
Replaced by the translation progress of the document. |
| 331 |
This substitution is intended for use by document translators |
| 332 |
as a marker for the translation progress of the document. |