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