Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | .. highlightlang:: rest |
| 2 | |
| 3 | Additional Markup Constructs |
| 4 | ============================ |
| 5 | |
| 6 | Sphinx adds a lot of new directives and interpreted text roles to standard reST |
| 7 | markup. This section contains the reference material for these facilities. |
| 8 | Documentation for "standard" reST constructs is not included here, though |
| 9 | they are used in the Python documentation. |
| 10 | |
Benjamin Peterson | f608c61 | 2008-11-16 18:33:53 +0000 | [diff] [blame] | 11 | .. note:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 12 | |
Benjamin Peterson | f608c61 | 2008-11-16 18:33:53 +0000 | [diff] [blame] | 13 | This is just an overview of Sphinx' extended markup capabilities; full |
| 14 | coverage can be found in `its own documentation |
| 15 | <http://sphinx.pocoo.org/contents.html>`_. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 16 | |
| 17 | |
| 18 | Meta-information markup |
| 19 | ----------------------- |
| 20 | |
| 21 | .. describe:: sectionauthor |
| 22 | |
| 23 | Identifies the author of the current section. The argument should include |
| 24 | the author's name such that it can be used for presentation (though it isn't) |
| 25 | and email address. The domain name portion of the address should be lower |
| 26 | case. Example:: |
| 27 | |
| 28 | .. sectionauthor:: Guido van Rossum <guido@python.org> |
| 29 | |
| 30 | Currently, this markup isn't reflected in the output in any way, but it helps |
| 31 | keep track of contributions. |
| 32 | |
| 33 | |
| 34 | Module-specific markup |
| 35 | ---------------------- |
| 36 | |
| 37 | The markup described in this section is used to provide information about a |
| 38 | module being documented. Each module should be documented in its own file. |
| 39 | Normally this markup appears after the title heading of that file; a typical |
| 40 | file might start like this:: |
| 41 | |
| 42 | :mod:`parrot` -- Dead parrot access |
| 43 | =================================== |
| 44 | |
| 45 | .. module:: parrot |
| 46 | :platform: Unix, Windows |
| 47 | :synopsis: Analyze and reanimate dead parrots. |
| 48 | .. moduleauthor:: Eric Cleese <eric@python.invalid> |
| 49 | .. moduleauthor:: John Idle <john@python.invalid> |
| 50 | |
| 51 | As you can see, the module-specific markup consists of two directives, the |
| 52 | ``module`` directive and the ``moduleauthor`` directive. |
| 53 | |
| 54 | .. describe:: module |
| 55 | |
Brett Cannon | df50106 | 2009-01-20 02:09:18 +0000 | [diff] [blame] | 56 | This directive marks the beginning of the description of a module, package, |
| 57 | or submodule. The name should be fully qualified (i.e. including the |
| 58 | package name for submodules). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 59 | |
| 60 | The ``platform`` option, if present, is a comma-separated list of the |
| 61 | platforms on which the module is available (if it is available on all |
| 62 | platforms, the option should be omitted). The keys are short identifiers; |
| 63 | examples that are in use include "IRIX", "Mac", "Windows", and "Unix". It is |
| 64 | important to use a key which has already been used when applicable. |
| 65 | |
| 66 | The ``synopsis`` option should consist of one sentence describing the |
| 67 | module's purpose -- it is currently only used in the Global Module Index. |
| 68 | |
Guido van Rossum | da27fd2 | 2007-08-17 00:24:54 +0000 | [diff] [blame] | 69 | The ``deprecated`` option can be given (with no value) to mark a module as |
| 70 | deprecated; it will be designated as such in various locations then. |
| 71 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 72 | .. describe:: moduleauthor |
| 73 | |
| 74 | The ``moduleauthor`` directive, which can appear multiple times, names the |
| 75 | authors of the module code, just like ``sectionauthor`` names the author(s) |
| 76 | of a piece of documentation. It too does not result in any output currently. |
| 77 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 78 | .. note:: |
| 79 | |
| 80 | It is important to make the section title of a module-describing file |
| 81 | meaningful since that value will be inserted in the table-of-contents trees |
| 82 | in overview files. |
| 83 | |
| 84 | |
| 85 | Information units |
| 86 | ----------------- |
| 87 | |
| 88 | There are a number of directives used to describe specific features provided by |
| 89 | modules. Each directive requires one or more signatures to provide basic |
| 90 | information about what is being described, and the content should be the |
| 91 | description. The basic version makes entries in the general index; if no index |
| 92 | entry is desired, you can give the directive option flag ``:noindex:``. The |
| 93 | following example shows all of the features of this directive type:: |
| 94 | |
| 95 | .. function:: spam(eggs) |
| 96 | ham(eggs) |
| 97 | :noindex: |
| 98 | |
| 99 | Spam or ham the foo. |
| 100 | |
Éric Araujo | b8f2ad0 | 2011-06-08 00:47:49 +0200 | [diff] [blame] | 101 | The signatures of object methods or data attributes should not include the |
| 102 | class name, but be nested in a class directive. The generated files will |
| 103 | reflect this nesting, and the target identifiers (for HTML output) will use |
| 104 | both the class and method name, to enable consistent cross-references. If you |
| 105 | describe methods belonging to an abstract protocol such as context managers, |
| 106 | use a class directive with a (pseudo-)type name too to make the |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 107 | index entries more informative. |
| 108 | |
| 109 | The directives are: |
| 110 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 111 | .. describe:: c:function |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 112 | |
| 113 | Describes a C function. The signature should be given as in C, e.g.:: |
| 114 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 115 | .. c:function:: PyObject* PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 116 | |
| 117 | This is also used to describe function-like preprocessor macros. The names |
| 118 | of the arguments should be given so they may be used in the description. |
| 119 | |
| 120 | Note that you don't have to backslash-escape asterisks in the signature, |
| 121 | as it is not parsed by the reST inliner. |
| 122 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 123 | .. describe:: c:member |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 124 | |
| 125 | Describes a C struct member. Example signature:: |
| 126 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 127 | .. c:member:: PyObject* PyTypeObject.tp_bases |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 128 | |
| 129 | The text of the description should include the range of values allowed, how |
| 130 | the value should be interpreted, and whether the value can be changed. |
| 131 | References to structure members in text should use the ``member`` role. |
| 132 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 133 | .. describe:: c:macro |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 134 | |
| 135 | Describes a "simple" C macro. Simple macros are macros which are used |
| 136 | for code expansion, but which do not take arguments so cannot be described as |
| 137 | functions. This is not to be used for simple constant definitions. Examples |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 138 | of its use in the Python documentation include :c:macro:`PyObject_HEAD` and |
| 139 | :c:macro:`Py_BEGIN_ALLOW_THREADS`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 140 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 141 | .. describe:: c:type |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 142 | |
| 143 | Describes a C type. The signature should just be the type name. |
| 144 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 145 | .. describe:: c:var |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 146 | |
| 147 | Describes a global C variable. The signature should include the type, such |
| 148 | as:: |
| 149 | |
| 150 | .. cvar:: PyObject* PyClass_Type |
| 151 | |
| 152 | .. describe:: data |
| 153 | |
| 154 | Describes global data in a module, including both variables and values used |
| 155 | as "defined constants." Class and object attributes are not documented |
Éric Araujo | a0b3c32 | 2011-04-16 23:47:53 +0200 | [diff] [blame] | 156 | using this directive. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 157 | |
| 158 | .. describe:: exception |
| 159 | |
| 160 | Describes an exception class. The signature can, but need not include |
| 161 | parentheses with constructor arguments. |
| 162 | |
| 163 | .. describe:: function |
| 164 | |
| 165 | Describes a module-level function. The signature should include the |
| 166 | parameters, enclosing optional parameters in brackets. Default values can be |
| 167 | given if it enhances clarity. For example:: |
| 168 | |
Éric Araujo | a0b3c32 | 2011-04-16 23:47:53 +0200 | [diff] [blame] | 169 | .. function:: repeat([repeat=3[, number=1000000]]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 170 | |
| 171 | Object methods are not documented using this directive. Bound object methods |
| 172 | placed in the module namespace as part of the public interface of the module |
| 173 | are documented using this, as they are equivalent to normal functions for |
| 174 | most purposes. |
| 175 | |
| 176 | The description should include information about the parameters required and |
| 177 | how they are used (especially whether mutable objects passed as parameters |
| 178 | are modified), side effects, and possible exceptions. A small example may be |
| 179 | provided. |
| 180 | |
Georg Brandl | 8a1caa2 | 2010-07-29 16:01:11 +0000 | [diff] [blame] | 181 | .. describe:: decorator |
| 182 | |
| 183 | Describes a decorator function. The signature should *not* represent the |
| 184 | signature of the actual function, but the usage as a decorator. For example, |
| 185 | given the functions |
| 186 | |
| 187 | .. code-block:: python |
| 188 | |
| 189 | def removename(func): |
| 190 | func.__name__ = '' |
| 191 | return func |
| 192 | |
| 193 | def setnewname(name): |
| 194 | def decorator(func): |
| 195 | func.__name__ = name |
| 196 | return func |
| 197 | return decorator |
| 198 | |
| 199 | the descriptions should look like this:: |
| 200 | |
| 201 | .. decorator:: removename |
| 202 | |
| 203 | Remove name of the decorated function. |
| 204 | |
| 205 | .. decorator:: setnewname(name) |
| 206 | |
| 207 | Set name of the decorated function to *name*. |
| 208 | |
Georg Brandl | bfc8fe4 | 2010-08-02 12:54:24 +0000 | [diff] [blame] | 209 | There is no ``deco`` role to link to a decorator that is marked up with |
| 210 | this directive; rather, use the ``:func:`` role. |
| 211 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 212 | .. describe:: class |
| 213 | |
| 214 | Describes a class. The signature can include parentheses with parameters |
| 215 | which will be shown as the constructor arguments. |
| 216 | |
| 217 | .. describe:: attribute |
| 218 | |
| 219 | Describes an object data attribute. The description should include |
| 220 | information about the type of the data to be expected and whether it may be |
Éric Araujo | a0b3c32 | 2011-04-16 23:47:53 +0200 | [diff] [blame] | 221 | changed directly. This directive should be nested in a class directive, |
| 222 | like in this example:: |
| 223 | |
| 224 | .. class:: Spam |
| 225 | |
| 226 | Description of the class. |
| 227 | |
| 228 | .. data:: ham |
| 229 | |
| 230 | Description of the attribute. |
| 231 | |
| 232 | If is also possible to document an attribute outside of a class directive, |
| 233 | for example if the documentation for different attributes and methods is |
| 234 | split in multiple sections. The class name should then be included |
| 235 | explicitly:: |
| 236 | |
| 237 | .. data:: Spam.eggs |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 238 | |
| 239 | .. describe:: method |
| 240 | |
| 241 | Describes an object method. The parameters should not include the ``self`` |
| 242 | parameter. The description should include similar information to that |
Éric Araujo | 3bba57d | 2011-05-01 02:14:48 +0200 | [diff] [blame] | 243 | described for ``function``. This directive should be nested in a class |
| 244 | directive, like in the example above. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 245 | |
Georg Brandl | 8a1caa2 | 2010-07-29 16:01:11 +0000 | [diff] [blame] | 246 | .. describe:: decoratormethod |
| 247 | |
| 248 | Same as ``decorator``, but for decorators that are methods. |
| 249 | |
Georg Brandl | bfc8fe4 | 2010-08-02 12:54:24 +0000 | [diff] [blame] | 250 | Refer to a decorator method using the ``:meth:`` role. |
| 251 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 252 | .. describe:: opcode |
| 253 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 254 | Describes a Python :term:`bytecode` instruction. |
| 255 | |
| 256 | .. describe:: cmdoption |
| 257 | |
Georg Brandl | f5ae1ef | 2010-07-26 21:12:13 +0000 | [diff] [blame] | 258 | Describes a Python command line option or switch. Option argument names |
| 259 | should be enclosed in angle brackets. Example:: |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 260 | |
| 261 | .. cmdoption:: -m <module> |
| 262 | |
| 263 | Run a module as a script. |
| 264 | |
| 265 | .. describe:: envvar |
| 266 | |
| 267 | Describes an environment variable that Python uses or defines. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 268 | |
| 269 | |
| 270 | There is also a generic version of these directives: |
| 271 | |
| 272 | .. describe:: describe |
| 273 | |
| 274 | This directive produces the same formatting as the specific ones explained |
| 275 | above but does not create index entries or cross-referencing targets. It is |
| 276 | used, for example, to describe the directives in this document. Example:: |
| 277 | |
| 278 | .. describe:: opcode |
| 279 | |
| 280 | Describes a Python bytecode instruction. |
| 281 | |
| 282 | |
| 283 | Showing code examples |
| 284 | --------------------- |
| 285 | |
| 286 | Examples of Python source code or interactive sessions are represented using |
| 287 | standard reST literal blocks. They are started by a ``::`` at the end of the |
| 288 | preceding paragraph and delimited by indentation. |
| 289 | |
| 290 | Representing an interactive session requires including the prompts and output |
| 291 | along with the Python code. No special markup is required for interactive |
| 292 | sessions. After the last line of input or output presented, there should not be |
| 293 | an "unused" primary prompt; this is an example of what *not* to do:: |
| 294 | |
| 295 | >>> 1 + 1 |
| 296 | 2 |
| 297 | >>> |
| 298 | |
| 299 | Syntax highlighting is handled in a smart way: |
| 300 | |
| 301 | * There is a "highlighting language" for each source file. Per default, |
| 302 | this is ``'python'`` as the majority of files will have to highlight Python |
| 303 | snippets. |
| 304 | |
| 305 | * Within Python highlighting mode, interactive sessions are recognized |
| 306 | automatically and highlighted appropriately. |
| 307 | |
| 308 | * The highlighting language can be changed using the ``highlightlang`` |
| 309 | directive, used as follows:: |
| 310 | |
| 311 | .. highlightlang:: c |
| 312 | |
| 313 | This language is used until the next ``highlightlang`` directive is |
| 314 | encountered. |
| 315 | |
Benjamin Peterson | f608c61 | 2008-11-16 18:33:53 +0000 | [diff] [blame] | 316 | * The values normally used for the highlighting language are: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 317 | |
| 318 | * ``python`` (the default) |
| 319 | * ``c`` |
| 320 | * ``rest`` |
| 321 | * ``none`` (no highlighting) |
| 322 | |
| 323 | * If highlighting with the current language fails, the block is not highlighted |
| 324 | in any way. |
| 325 | |
| 326 | Longer displays of verbatim text may be included by storing the example text in |
| 327 | an external file containing only plain text. The file may be included using the |
| 328 | ``literalinclude`` directive. [1]_ For example, to include the Python source file |
| 329 | :file:`example.py`, use:: |
| 330 | |
| 331 | .. literalinclude:: example.py |
| 332 | |
| 333 | The file name is relative to the current file's path. Documentation-specific |
| 334 | include files should be placed in the ``Doc/includes`` subdirectory. |
| 335 | |
| 336 | |
| 337 | Inline markup |
| 338 | ------------- |
| 339 | |
| 340 | As said before, Sphinx uses interpreted text roles to insert semantic markup in |
| 341 | documents. |
| 342 | |
Benjamin Peterson | aa06900 | 2009-01-23 03:26:36 +0000 | [diff] [blame] | 343 | Names of local variables, such as function/method arguments, are an exception, |
| 344 | they should be marked simply with ``*var*``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 345 | |
| 346 | For all other roles, you have to write ``:rolename:`content```. |
| 347 | |
Benjamin Peterson | c4bbc8d | 2009-01-30 03:39:35 +0000 | [diff] [blame] | 348 | There are some additional facilities that make cross-referencing roles more |
| 349 | versatile: |
Guido van Rossum | f10aa98 | 2007-08-17 18:30:38 +0000 | [diff] [blame] | 350 | |
Benjamin Peterson | c4bbc8d | 2009-01-30 03:39:35 +0000 | [diff] [blame] | 351 | * You may supply an explicit title and reference target, like in reST direct |
| 352 | hyperlinks: ``:role:`title <target>``` will refer to *target*, but the link |
| 353 | text will be *title*. |
| 354 | |
| 355 | * If you prefix the content with ``!``, no reference/hyperlink will be created. |
| 356 | |
| 357 | * For the Python object roles, if you prefix the content with ``~``, the link |
| 358 | text will only be the last component of the target. For example, |
| 359 | ``:meth:`~Queue.Queue.get``` will refer to ``Queue.Queue.get`` but only |
| 360 | display ``get`` as the link text. |
| 361 | |
| 362 | In HTML output, the link's ``title`` attribute (that is e.g. shown as a |
| 363 | tool-tip on mouse-hover) will always be the full target name. |
Guido van Rossum | f10aa98 | 2007-08-17 18:30:38 +0000 | [diff] [blame] | 364 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 365 | The following roles refer to objects in modules and are possibly hyperlinked if |
| 366 | a matching identifier is found: |
| 367 | |
| 368 | .. describe:: mod |
| 369 | |
| 370 | The name of a module; a dotted name may be used. This should also be used for |
| 371 | package names. |
| 372 | |
| 373 | .. describe:: func |
| 374 | |
| 375 | The name of a Python function; dotted names may be used. The role text |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 376 | should not include trailing parentheses to enhance readability. The |
| 377 | parentheses are stripped when searching for identifiers. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 378 | |
| 379 | .. describe:: data |
| 380 | |
Benjamin Peterson | aa06900 | 2009-01-23 03:26:36 +0000 | [diff] [blame] | 381 | The name of a module-level variable or constant. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 382 | |
| 383 | .. describe:: const |
| 384 | |
| 385 | The name of a "defined" constant. This may be a C-language ``#define`` |
| 386 | or a Python variable that is not intended to be changed. |
| 387 | |
| 388 | .. describe:: class |
| 389 | |
| 390 | A class name; a dotted name may be used. |
| 391 | |
| 392 | .. describe:: meth |
| 393 | |
| 394 | The name of a method of an object. The role text should include the type |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 395 | name and the method name. A dotted name may be used. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 396 | |
| 397 | .. describe:: attr |
| 398 | |
| 399 | The name of a data attribute of an object. |
| 400 | |
| 401 | .. describe:: exc |
| 402 | |
| 403 | The name of an exception. A dotted name may be used. |
| 404 | |
| 405 | The name enclosed in this markup can include a module name and/or a class name. |
| 406 | For example, ``:func:`filter``` could refer to a function named ``filter`` in |
| 407 | the current module, or the built-in function of that name. In contrast, |
| 408 | ``:func:`foo.filter``` clearly refers to the ``filter`` function in the ``foo`` |
| 409 | module. |
| 410 | |
Guido van Rossum | da27fd2 | 2007-08-17 00:24:54 +0000 | [diff] [blame] | 411 | Normally, names in these roles are searched first without any further |
| 412 | qualification, then with the current module name prepended, then with the |
| 413 | current module and class name (if any) prepended. If you prefix the name with a |
| 414 | dot, this order is reversed. For example, in the documentation of the |
| 415 | :mod:`codecs` module, ``:func:`open``` always refers to the built-in function, |
| 416 | while ``:func:`.open``` refers to :func:`codecs.open`. |
| 417 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 418 | A similar heuristic is used to determine whether the name is an attribute of |
| 419 | the currently documented class. |
| 420 | |
| 421 | The following roles create cross-references to C-language constructs if they |
| 422 | are defined in the API documentation: |
| 423 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 424 | .. describe:: c:data |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 425 | |
| 426 | The name of a C-language variable. |
| 427 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 428 | .. describe:: c:func |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 429 | |
| 430 | The name of a C-language function. Should include trailing parentheses. |
| 431 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 432 | .. describe:: c:macro |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 433 | |
| 434 | The name of a "simple" C macro, as defined above. |
| 435 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 436 | .. describe:: c:type |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 437 | |
| 438 | The name of a C-language type. |
| 439 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 440 | .. describe:: c:member |
| 441 | |
| 442 | The name of a C type member, as defined above. |
| 443 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 444 | |
| 445 | The following role does possibly create a cross-reference, but does not refer |
| 446 | to objects: |
| 447 | |
| 448 | .. describe:: token |
| 449 | |
| 450 | The name of a grammar token (used in the reference manual to create links |
| 451 | between production displays). |
| 452 | |
Guido van Rossum | f10aa98 | 2007-08-17 18:30:38 +0000 | [diff] [blame] | 453 | |
| 454 | The following role creates a cross-reference to the term in the glossary: |
| 455 | |
| 456 | .. describe:: term |
| 457 | |
| 458 | Reference to a term in the glossary. The glossary is created using the |
| 459 | ``glossary`` directive containing a definition list with terms and |
| 460 | definitions. It does not have to be in the same file as the ``term`` |
| 461 | markup, in fact, by default the Python docs have one global glossary |
| 462 | in the ``glossary.rst`` file. |
| 463 | |
| 464 | If you use a term that's not explained in a glossary, you'll get a warning |
| 465 | during build. |
| 466 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 467 | --------- |
| 468 | |
| 469 | The following roles don't do anything special except formatting the text |
| 470 | in a different style: |
| 471 | |
| 472 | .. describe:: command |
| 473 | |
| 474 | The name of an OS-level command, such as ``rm``. |
| 475 | |
| 476 | .. describe:: dfn |
| 477 | |
| 478 | Mark the defining instance of a term in the text. (No index entries are |
| 479 | generated.) |
| 480 | |
| 481 | .. describe:: envvar |
| 482 | |
| 483 | An environment variable. Index entries are generated. |
| 484 | |
| 485 | .. describe:: file |
| 486 | |
| 487 | The name of a file or directory. Within the contents, you can use curly |
| 488 | braces to indicate a "variable" part, for example:: |
| 489 | |
| 490 | ... is installed in :file:`/usr/lib/python2.{x}/site-packages` ... |
| 491 | |
| 492 | In the built documentation, the ``x`` will be displayed differently to |
| 493 | indicate that it is to be replaced by the Python minor version. |
| 494 | |
| 495 | .. describe:: guilabel |
| 496 | |
| 497 | Labels presented as part of an interactive user interface should be marked |
| 498 | using ``guilabel``. This includes labels from text-based interfaces such as |
| 499 | those created using :mod:`curses` or other text-based libraries. Any label |
| 500 | used in the interface should be marked with this role, including button |
| 501 | labels, window titles, field names, menu and menu selection names, and even |
| 502 | values in selection lists. |
| 503 | |
| 504 | .. describe:: kbd |
| 505 | |
| 506 | Mark a sequence of keystrokes. What form the key sequence takes may depend |
| 507 | on platform- or application-specific conventions. When there are no relevant |
| 508 | conventions, the names of modifier keys should be spelled out, to improve |
| 509 | accessibility for new users and non-native speakers. For example, an |
| 510 | *xemacs* key sequence may be marked like ``:kbd:`C-x C-f```, but without |
| 511 | reference to a specific application or platform, the same sequence should be |
| 512 | marked as ``:kbd:`Control-x Control-f```. |
| 513 | |
| 514 | .. describe:: keyword |
| 515 | |
Éric Araujo | 8ab3a1d | 2011-09-01 18:45:50 +0200 | [diff] [blame] | 516 | The name of a Python keyword. Using this role will generate a link to the |
| 517 | documentation of the keyword. ``True``, ``False`` and ``None`` do not use |
| 518 | this role, but simple code markup (````True````), given that they're |
| 519 | fundamental to the language and should be known to any programmer. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 520 | |
| 521 | .. describe:: mailheader |
| 522 | |
| 523 | The name of an RFC 822-style mail header. This markup does not imply that |
| 524 | the header is being used in an email message, but can be used to refer to any |
| 525 | header of the same "style." This is also used for headers defined by the |
| 526 | various MIME specifications. The header name should be entered in the same |
| 527 | way it would normally be found in practice, with the camel-casing conventions |
| 528 | being preferred where there is more than one common usage. For example: |
| 529 | ``:mailheader:`Content-Type```. |
| 530 | |
| 531 | .. describe:: makevar |
| 532 | |
| 533 | The name of a :command:`make` variable. |
| 534 | |
| 535 | .. describe:: manpage |
| 536 | |
| 537 | A reference to a Unix manual page including the section, |
| 538 | e.g. ``:manpage:`ls(1)```. |
| 539 | |
| 540 | .. describe:: menuselection |
| 541 | |
| 542 | Menu selections should be marked using the ``menuselection`` role. This is |
| 543 | used to mark a complete sequence of menu selections, including selecting |
| 544 | submenus and choosing a specific operation, or any subsequence of such a |
| 545 | sequence. The names of individual selections should be separated by |
| 546 | ``-->``. |
| 547 | |
| 548 | For example, to mark the selection "Start > Programs", use this markup:: |
| 549 | |
| 550 | :menuselection:`Start --> Programs` |
| 551 | |
| 552 | When including a selection that includes some trailing indicator, such as the |
| 553 | ellipsis some operating systems use to indicate that the command opens a |
| 554 | dialog, the indicator should be omitted from the selection name. |
| 555 | |
| 556 | .. describe:: mimetype |
| 557 | |
| 558 | The name of a MIME type, or a component of a MIME type (the major or minor |
| 559 | portion, taken alone). |
| 560 | |
| 561 | .. describe:: newsgroup |
| 562 | |
| 563 | The name of a Usenet newsgroup. |
| 564 | |
| 565 | .. describe:: option |
| 566 | |
Georg Brandl | a5ed401 | 2010-07-19 06:57:52 +0000 | [diff] [blame] | 567 | A command-line option of Python. The leading hyphen(s) must be included. |
| 568 | If a matching ``cmdoption`` directive exists, it is linked to. For options |
| 569 | of other programs or scripts, use simple ````code```` markup. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 570 | |
| 571 | .. describe:: program |
| 572 | |
| 573 | The name of an executable program. This may differ from the file name for |
| 574 | the executable for some platforms. In particular, the ``.exe`` (or other) |
| 575 | extension should be omitted for Windows programs. |
| 576 | |
| 577 | .. describe:: regexp |
| 578 | |
| 579 | A regular expression. Quotes should not be included. |
| 580 | |
| 581 | .. describe:: samp |
| 582 | |
| 583 | A piece of literal text, such as code. Within the contents, you can use |
| 584 | curly braces to indicate a "variable" part, as in ``:file:``. |
| 585 | |
| 586 | If you don't need the "variable part" indication, use the standard |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 587 | ````code```` instead. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 588 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 589 | |
| 590 | The following roles generate external links: |
| 591 | |
| 592 | .. describe:: pep |
| 593 | |
| 594 | A reference to a Python Enhancement Proposal. This generates appropriate |
| 595 | index entries. The text "PEP *number*\ " is generated; in the HTML output, |
| 596 | this text is a hyperlink to an online copy of the specified PEP. |
| 597 | |
| 598 | .. describe:: rfc |
| 599 | |
| 600 | A reference to an Internet Request for Comments. This generates appropriate |
| 601 | index entries. The text "RFC *number*\ " is generated; in the HTML output, |
| 602 | this text is a hyperlink to an online copy of the specified RFC. |
| 603 | |
| 604 | |
| 605 | Note that there are no special roles for including hyperlinks as you can use |
| 606 | the standard reST markup for that purpose. |
| 607 | |
| 608 | |
| 609 | .. _doc-ref-role: |
| 610 | |
| 611 | Cross-linking markup |
| 612 | -------------------- |
| 613 | |
| 614 | To support cross-referencing to arbitrary sections in the documentation, the |
| 615 | standard reST labels are "abused" a bit: Every label must precede a section |
| 616 | title; and every label name must be unique throughout the entire documentation |
| 617 | source. |
| 618 | |
| 619 | You can then reference to these sections using the ``:ref:`label-name``` role. |
| 620 | |
| 621 | Example:: |
| 622 | |
| 623 | .. _my-reference-label: |
| 624 | |
| 625 | Section to cross-reference |
| 626 | -------------------------- |
| 627 | |
| 628 | This is the text of the section. |
| 629 | |
| 630 | It refers to the section itself, see :ref:`my-reference-label`. |
| 631 | |
| 632 | The ``:ref:`` invocation is replaced with the section title. |
| 633 | |
Raymond Hettinger | cfee0e8 | 2010-12-21 20:52:12 +0000 | [diff] [blame] | 634 | Alternatively, you can reference any label (not just section titles) |
Georg Brandl | 23e924f | 2011-01-15 17:05:20 +0000 | [diff] [blame] | 635 | if you provide the link text ``:ref:`link text <reference-label>```. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 636 | |
| 637 | Paragraph-level markup |
| 638 | ---------------------- |
| 639 | |
| 640 | These directives create short paragraphs and can be used inside information |
| 641 | units as well as normal text: |
| 642 | |
| 643 | .. describe:: note |
| 644 | |
| 645 | An especially important bit of information about an API that a user should be |
| 646 | aware of when using whatever bit of API the note pertains to. The content of |
| 647 | the directive should be written in complete sentences and include all |
| 648 | appropriate punctuation. |
| 649 | |
| 650 | Example:: |
| 651 | |
| 652 | .. note:: |
| 653 | |
| 654 | This function is not suitable for sending spam e-mails. |
| 655 | |
| 656 | .. describe:: warning |
| 657 | |
Georg Brandl | e720c0a | 2009-04-27 16:20:50 +0000 | [diff] [blame] | 658 | An important bit of information about an API that a user should be aware of |
| 659 | when using whatever bit of API the warning pertains to. The content of the |
| 660 | directive should be written in complete sentences and include all appropriate |
Benjamin Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame] | 661 | punctuation. In the interest of not scaring users away from pages filled |
| 662 | with warnings, this directive should only be chosen over ``note`` for |
| 663 | information regarding the possibility of crashes, data loss, or security |
| 664 | implications. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 665 | |
| 666 | .. describe:: versionadded |
| 667 | |
| 668 | This directive documents the version of Python which added the described |
| 669 | feature to the library or C API. When this applies to an entire module, it |
| 670 | should be placed at the top of the module section before any prose. |
| 671 | |
| 672 | The first argument must be given and is the version in question; you can add |
| 673 | a second argument consisting of a *brief* explanation of the change. |
| 674 | |
| 675 | Example:: |
| 676 | |
Georg Brandl | 277a150 | 2009-01-04 00:28:14 +0000 | [diff] [blame] | 677 | .. versionadded:: 3.1 |
Georg Brandl | 36ab1ef | 2009-01-03 21:17:04 +0000 | [diff] [blame] | 678 | The *spam* parameter. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 679 | |
| 680 | Note that there must be no blank line between the directive head and the |
| 681 | explanation; this is to make these blocks visually continuous in the markup. |
| 682 | |
| 683 | .. describe:: versionchanged |
| 684 | |
| 685 | Similar to ``versionadded``, but describes when and what changed in the named |
| 686 | feature in some way (new parameters, changed side effects, etc.). |
| 687 | |
| 688 | -------------- |
| 689 | |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 690 | .. describe:: impl-detail |
| 691 | |
| 692 | This directive is used to mark CPython-specific information. Use either with |
| 693 | a block content or a single sentence as an argument, i.e. either :: |
| 694 | |
| 695 | .. impl-detail:: |
| 696 | |
| 697 | This describes some implementation detail. |
| 698 | |
| 699 | More explanation. |
| 700 | |
| 701 | or :: |
| 702 | |
| 703 | .. impl-detail:: This shortly mentions an implementation detail. |
| 704 | |
| 705 | "\ **CPython implementation detail:**\ " is automatically prepended to the |
| 706 | content. |
| 707 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 708 | .. describe:: seealso |
| 709 | |
| 710 | Many sections include a list of references to module documentation or |
| 711 | external documents. These lists are created using the ``seealso`` directive. |
| 712 | |
| 713 | The ``seealso`` directive is typically placed in a section just before any |
| 714 | sub-sections. For the HTML output, it is shown boxed off from the main flow |
| 715 | of the text. |
| 716 | |
| 717 | The content of the ``seealso`` directive should be a reST definition list. |
| 718 | Example:: |
| 719 | |
| 720 | .. seealso:: |
| 721 | |
| 722 | Module :mod:`zipfile` |
| 723 | Documentation of the :mod:`zipfile` standard module. |
| 724 | |
| 725 | `GNU tar manual, Basic Tar Format <http://link>`_ |
| 726 | Documentation for tar archive files, including GNU tar extensions. |
| 727 | |
| 728 | .. describe:: rubric |
| 729 | |
| 730 | This directive creates a paragraph heading that is not used to create a |
| 731 | table of contents node. It is currently used for the "Footnotes" caption. |
| 732 | |
| 733 | .. describe:: centered |
| 734 | |
| 735 | This directive creates a centered boldfaced paragraph. Use it as follows:: |
| 736 | |
| 737 | .. centered:: |
| 738 | |
| 739 | Paragraph contents. |
| 740 | |
| 741 | |
| 742 | Table-of-contents markup |
| 743 | ------------------------ |
| 744 | |
| 745 | Since reST does not have facilities to interconnect several documents, or split |
| 746 | documents into multiple output files, Sphinx uses a custom directive to add |
| 747 | relations between the single files the documentation is made of, as well as |
| 748 | tables of contents. The ``toctree`` directive is the central element. |
| 749 | |
| 750 | .. describe:: toctree |
| 751 | |
| 752 | This directive inserts a "TOC tree" at the current location, using the |
| 753 | individual TOCs (including "sub-TOC trees") of the files given in the |
| 754 | directive body. A numeric ``maxdepth`` option may be given to indicate the |
| 755 | depth of the tree; by default, all levels are included. |
| 756 | |
| 757 | Consider this example (taken from the library reference index):: |
| 758 | |
| 759 | .. toctree:: |
| 760 | :maxdepth: 2 |
| 761 | |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 762 | intro |
| 763 | strings |
| 764 | datatypes |
| 765 | numeric |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 766 | (many more files listed here) |
| 767 | |
| 768 | This accomplishes two things: |
| 769 | |
| 770 | * Tables of contents from all those files are inserted, with a maximum depth |
| 771 | of two, that means one nested heading. ``toctree`` directives in those |
| 772 | files are also taken into account. |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 773 | * Sphinx knows that the relative order of the files ``intro``, |
| 774 | ``strings`` and so forth, and it knows that they are children of the |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 775 | shown file, the library index. From this information it generates "next |
| 776 | chapter", "previous chapter" and "parent chapter" links. |
| 777 | |
| 778 | In the end, all files included in the build process must occur in one |
| 779 | ``toctree`` directive; Sphinx will emit a warning if it finds a file that is |
| 780 | not included, because that means that this file will not be reachable through |
| 781 | standard navigation. |
| 782 | |
| 783 | The special file ``contents.rst`` at the root of the source directory is the |
| 784 | "root" of the TOC tree hierarchy; from it the "Contents" page is generated. |
| 785 | |
| 786 | |
| 787 | Index-generating markup |
| 788 | ----------------------- |
| 789 | |
| 790 | Sphinx automatically creates index entries from all information units (like |
| 791 | functions, classes or attributes) like discussed before. |
| 792 | |
| 793 | However, there is also an explicit directive available, to make the index more |
| 794 | comprehensive and enable index entries in documents where information is not |
| 795 | mainly contained in information units, such as the language reference. |
| 796 | |
| 797 | The directive is ``index`` and contains one or more index entries. Each entry |
| 798 | consists of a type and a value, separated by a colon. |
| 799 | |
| 800 | For example:: |
| 801 | |
| 802 | .. index:: |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 803 | single: execution; context |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 804 | module: __main__ |
| 805 | module: sys |
| 806 | triple: module; search; path |
| 807 | |
| 808 | This directive contains five entries, which will be converted to entries in the |
| 809 | generated index which link to the exact location of the index statement (or, in |
| 810 | case of offline media, the corresponding page number). |
| 811 | |
| 812 | The possible entry types are: |
| 813 | |
| 814 | single |
| 815 | Creates a single index entry. Can be made a subentry by separating the |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 816 | subentry text with a semicolon (this notation is also used below to describe |
| 817 | what entries are created). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 818 | pair |
| 819 | ``pair: loop; statement`` is a shortcut that creates two index entries, |
| 820 | namely ``loop; statement`` and ``statement; loop``. |
| 821 | triple |
| 822 | Likewise, ``triple: module; search; path`` is a shortcut that creates three |
| 823 | index entries, which are ``module; search path``, ``search; path, module`` and |
| 824 | ``path; module search``. |
| 825 | module, keyword, operator, object, exception, statement, builtin |
| 826 | These all create two index entries. For example, ``module: hashlib`` creates |
| 827 | the entries ``module; hashlib`` and ``hashlib; module``. |
| 828 | |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 829 | For index directives containing only "single" entries, there is a shorthand |
| 830 | notation:: |
| 831 | |
| 832 | .. index:: BNF, grammar, syntax, notation |
| 833 | |
| 834 | This creates four index entries. |
| 835 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 836 | |
| 837 | Grammar production displays |
| 838 | --------------------------- |
| 839 | |
| 840 | Special markup is available for displaying the productions of a formal grammar. |
| 841 | The markup is simple and does not attempt to model all aspects of BNF (or any |
| 842 | derived forms), but provides enough to allow context-free grammars to be |
| 843 | displayed in a way that causes uses of a symbol to be rendered as hyperlinks to |
| 844 | the definition of the symbol. There is this directive: |
| 845 | |
| 846 | .. describe:: productionlist |
| 847 | |
| 848 | This directive is used to enclose a group of productions. Each production is |
| 849 | given on a single line and consists of a name, separated by a colon from the |
| 850 | following definition. If the definition spans multiple lines, each |
| 851 | continuation line must begin with a colon placed at the same column as in the |
| 852 | first line. |
| 853 | |
| 854 | Blank lines are not allowed within ``productionlist`` directive arguments. |
| 855 | |
| 856 | The definition can contain token names which are marked as interpreted text |
Georg Brandl | 36ab1ef | 2009-01-03 21:17:04 +0000 | [diff] [blame] | 857 | (e.g. ``unaryneg ::= "-" `integer```) -- this generates cross-references |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 858 | to the productions of these tokens. |
| 859 | |
| 860 | Note that no further reST parsing is done in the production, so that you |
| 861 | don't have to escape ``*`` or ``|`` characters. |
| 862 | |
| 863 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 864 | .. XXX describe optional first parameter |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 865 | |
| 866 | The following is an example taken from the Python Reference Manual:: |
| 867 | |
| 868 | .. productionlist:: |
| 869 | try_stmt: try1_stmt | try2_stmt |
| 870 | try1_stmt: "try" ":" `suite` |
| 871 | : ("except" [`expression` ["," `target`]] ":" `suite`)+ |
| 872 | : ["else" ":" `suite`] |
| 873 | : ["finally" ":" `suite`] |
| 874 | try2_stmt: "try" ":" `suite` |
| 875 | : "finally" ":" `suite` |
| 876 | |
| 877 | |
| 878 | Substitutions |
| 879 | ------------- |
| 880 | |
| 881 | The documentation system provides three substitutions that are defined by default. |
Benjamin Peterson | f608c61 | 2008-11-16 18:33:53 +0000 | [diff] [blame] | 882 | They are set in the build configuration file :file:`conf.py`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 883 | |
| 884 | .. describe:: |release| |
| 885 | |
| 886 | Replaced by the Python release the documentation refers to. This is the full |
| 887 | version string including alpha/beta/release candidate tags, e.g. ``2.5.2b3``. |
| 888 | |
| 889 | .. describe:: |version| |
| 890 | |
| 891 | Replaced by the Python version the documentation refers to. This consists |
| 892 | only of the major and minor version parts, e.g. ``2.5``, even for version |
| 893 | 2.5.1. |
| 894 | |
| 895 | .. describe:: |today| |
| 896 | |
| 897 | Replaced by either today's date, or the date set in the build configuration |
| 898 | file. Normally has the format ``April 14, 2007``. |
| 899 | |
| 900 | |
| 901 | .. rubric:: Footnotes |
| 902 | |
| 903 | .. [1] There is a standard ``.. include`` directive, but it raises errors if the |
| 904 | file is not found. This one only emits a warning. |