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