CodeMirror: reStructuredText mode

1
.. This is an excerpt from Sphinx documentation: http://sphinx.pocoo.org/_sources/rest.txt
2
 
3
.. highlightlang:: rest
4
 
5
.. _rst-primer:
6
 
7
reStructuredText Primer
8
=======================
9
 
10
This section is a brief introduction to reStructuredText (reST) concepts and
11
syntax, intended to provide authors with enough information to author documents
12
productively.  Since reST was designed to be a simple, unobtrusive markup
13
language, this will not take too long.
14
 
15
.. seealso::
16
 
17
   The authoritative `reStructuredText User Documentation
18
   <http://docutils.sourceforge.net/rst.html>`_.  The "ref" links in this
19
   document link to the description of the individual constructs in the reST
20
   reference.
21
 
22
 
23
Paragraphs
24
----------
25
 
26
The paragraph (:duref:`ref <paragraphs>`) is the most basic block in a reST
27
document.  Paragraphs are simply chunks of text separated by one or more blank
28
lines.  As in Python, indentation is significant in reST, so all lines of the
29
same paragraph must be left-aligned to the same level of indentation.
30
 
31
 
32
.. _inlinemarkup:
33
 
34
Inline markup
35
-------------
36
 
37
The standard reST inline markup is quite simple: use
38
 
39
* one asterisk: ``*text*`` for emphasis (italics),
40
* two asterisks: ``**text**`` for strong emphasis (boldface), and
41
* backquotes: ````text```` for code samples.
42
 
43
If asterisks or backquotes appear in running text and could be confused with
44
inline markup delimiters, they have to be escaped with a backslash.
45
 
46
Be aware of some restrictions of this markup:
47
 
48
* it may not be nested,
49
* content may not start or end with whitespace: ``* text*`` is wrong,
50
* it must be separated from surrounding text by non-word characters.  Use a
51
  backslash escaped space to work around that: ``thisis\ *one*\ word``.
52
 
53
These restrictions may be lifted in future versions of the docutils.
54
 
55
reST also allows for custom "interpreted text roles"', which signify that the
56
enclosed text should be interpreted in a specific way.  Sphinx uses this to
57
provide semantic markup and cross-referencing of identifiers, as described in
58
the appropriate section.  The general syntax is ``:rolename:`content```.
59
 
60
Standard reST provides the following roles:
61
 
62
* :durole:`emphasis` -- alternate spelling for ``*emphasis*``
63
* :durole:`strong` -- alternate spelling for ``**strong**``
64
* :durole:`literal` -- alternate spelling for ````literal````
65
* :durole:`subscript` -- subscript text
66
* :durole:`superscript` -- superscript text
67
* :durole:`title-reference` -- for titles of books, periodicals, and other
68
  materials
69
 
70
See :ref:`inline-markup` for roles added by Sphinx.
71
 
72
 
73
Lists and Quote-like blocks
74
---------------------------
75
 
76
List markup (:duref:`ref <bullet-lists>`) is natural: just place an asterisk at
77
the start of a paragraph and indent properly.  The same goes for numbered lists;
78
they can also be autonumbered using a ``#`` sign::
79
 
80
   * This is a bulleted list.
81
   * It has two items, the second
82
     item uses two lines.
83
 
84
   1. This is a numbered list.
85
   2. It has two items too.
86
 
87
   #. This is a numbered list.
88
   #. It has two items too.
89
 
90
 
91
Nested lists are possible, but be aware that they must be separated from the
92
parent list items by blank lines::
93
 
94
   * this is
95
   * a list
96
 
97
     * with a nested list
98
     * and some subitems
99
 
100
   * and here the parent list continues
101
 
102
Definition lists (:duref:`ref <definition-lists>`) are created as follows::
103
 
104
   term (up to a line of text)
105
      Definition of the term, which must be indented
106
 
107
      and can even consist of multiple paragraphs
108
 
109
   next term
110
      Description.
111
 
112
Note that the term cannot have more than one line of text.
113
 
114
Quoted paragraphs (:duref:`ref <block-quotes>`) are created by just indenting
115
them more than the surrounding paragraphs.
116
 
117
Line blocks (:duref:`ref <line-blocks>`) are a way of preserving line breaks::
118
 
119
   | These lines are
120
   | broken exactly like in
121
   | the source file.
122
 
123
There are also several more special blocks available:
124
 
125
* field lists (:duref:`ref <field-lists>`)
126
* option lists (:duref:`ref <option-lists>`)
127
* quoted literal blocks (:duref:`ref <quoted-literal-blocks>`)
128
* doctest blocks (:duref:`ref <doctest-blocks>`)
129
 
130
 
131
Source Code
132
-----------
133
 
134
Literal code blocks (:duref:`ref <literal-blocks>`) are introduced by ending a
135
paragraph with the special marker ``::``.  The literal block must be indented
136
(and, like all paragraphs, separated from the surrounding ones by blank lines)::
137
 
138
   This is a normal text paragraph. The next paragraph is a code sample::
139
 
140
      It is not processed in any way, except
141
      that the indentation is removed.
142
 
143
      It can span multiple lines.
144
 
145
   This is a normal text paragraph again.
146
 
147
The handling of the ``::`` marker is smart:
148
 
149
* If it occurs as a paragraph of its own, that paragraph is completely left
150
  out of the document.
151
* If it is preceded by whitespace, the marker is removed.
152
* If it is preceded by non-whitespace, the marker is replaced by a single
153
  colon.
154
 
155
That way, the second sentence in the above example's first paragraph would be
156
rendered as "The next paragraph is a code sample:".
157
 
158
 
159
.. _rst-tables:
160
 
161
Tables
162
------
163
 
164
Two forms of tables are supported.  For *grid tables* (:duref:`ref
165
<grid-tables>`), you have to "paint" the cell grid yourself.  They look like
166
this::
167
 
168
   +------------------------+------------+----------+----------+
169
   | Header row, column 1   | Header 2   | Header 3 | Header 4 |
170
   | (header rows optional) |            |          |          |
171
   +========================+============+==========+==========+
172
   | body row 1, column 1   | column 2   | column 3 | column 4 |
173
   +------------------------+------------+----------+----------+
174
   | body row 2             | ...        | ...      |          |
175
   +------------------------+------------+----------+----------+
176
 
177
*Simple tables* (:duref:`ref <simple-tables>`) are easier to write, but
178
limited: they must contain more than one row, and the first column cannot
179
contain multiple lines.  They look like this::
180
 
181
   =====  =====  =======
182
   A      B      A and B
183
   =====  =====  =======
184
   False  False  False
185
   True   False  False
186
   False  True   False
187
   True   True   True
188
   =====  =====  =======
189
 
190
 
191
Hyperlinks
192
----------
193
 
194
External links
195
^^^^^^^^^^^^^^
196
 
197
Use ```Link text <http://example.com/>`_`` for inline web links.  If the link
198
text should be the web address, you don't need special markup at all, the parser
199
finds links and mail addresses in ordinary text.
200
 
201
You can also separate the link and the target definition (:duref:`ref
202
<hyperlink-targets>`), like this::
203
 
204
   This is a paragraph that contains `a link`_.
205
 
206
   .. _a link: http://example.com/
207
 
208
 
209
Internal links
210
^^^^^^^^^^^^^^
211
 
212
Internal linking is done via a special reST role provided by Sphinx, see the
213
section on specific markup, :ref:`ref-role`.
214
 
215
 
216
Sections
217
--------
218
 
219
Section headers (:duref:`ref <sections>`) are created by underlining (and
220
optionally overlining) the section title with a punctuation character, at least
221
as long as the text::
222
 
223
   =================
224
   This is a heading
225
   =================
226
 
227
Normally, there are no heading levels assigned to certain characters as the
228
structure is determined from the succession of headings.  However, for the
229
Python documentation, this convention is used which you may follow:
230
 
231
* ``#`` with overline, for parts
232
* ``*`` with overline, for chapters
233
* ``=``, for sections
234
* ``-``, for subsections
235
* ``^``, for subsubsections
236
* ``"``, for paragraphs
237
 
238
Of course, you are free to use your own marker characters (see the reST
239
documentation), and use a deeper nesting level, but keep in mind that most
240
target formats (HTML, LaTeX) have a limited supported nesting depth.
241
 
242
 
243
Explicit Markup
244
---------------
245
 
246
"Explicit markup" (:duref:`ref <explicit-markup-blocks>`) is used in reST for
247
most constructs that need special handling, such as footnotes,
248
specially-highlighted paragraphs, comments, and generic directives.
249
 
250
An explicit markup block begins with a line starting with ``..`` followed by
251
whitespace and is terminated by the next paragraph at the same level of
252
indentation.  (There needs to be a blank line between explicit markup and normal
253
paragraphs.  This may all sound a bit complicated, but it is intuitive enough
254
when you write it.)
255
 
256
 
257
.. _directives:
258
 
259
Directives
260
----------
261
 
262
A directive (:duref:`ref <directives>`) is a generic block of explicit markup.
263
Besides roles, it is one of the extension mechanisms of reST, and Sphinx makes
264
heavy use of it.
265
 
266
Docutils supports the following directives:
267
 
268
* Admonitions: :dudir:`attention`, :dudir:`caution`, :dudir:`danger`,
269
  :dudir:`error`, :dudir:`hint`, :dudir:`important`, :dudir:`note`,
270
  :dudir:`tip`, :dudir:`warning` and the generic :dudir:`admonition`.
271
  (Most themes style only "note" and "warning" specially.)
272
 
273
* Images:
274
 
275
  - :dudir:`image` (see also Images_ below)
276
  - :dudir:`figure` (an image with caption and optional legend)
277
 
278
* Additional body elements:
279
 
280
  - :dudir:`contents` (a local, i.e. for the current file only, table of
281
    contents)
282
  - :dudir:`container` (a container with a custom class, useful to generate an
283
    outer ``<div>`` in HTML)
284
  - :dudir:`rubric` (a heading without relation to the document sectioning)
285
  - :dudir:`topic`, :dudir:`sidebar` (special highlighted body elements)
286
  - :dudir:`parsed-literal` (literal block that supports inline markup)
287
  - :dudir:`epigraph` (a block quote with optional attribution line)
288
  - :dudir:`highlights`, :dudir:`pull-quote` (block quotes with their own
289
    class attribute)
290
  - :dudir:`compound` (a compound paragraph)
291
 
292
* Special tables:
293
 
294
  - :dudir:`table` (a table with title)
295
  - :dudir:`csv-table` (a table generated from comma-separated values)
296
  - :dudir:`list-table` (a table generated from a list of lists)
297
 
298
* Special directives:
299
 
300
  - :dudir:`raw` (include raw target-format markup)
301
  - :dudir:`include` (include reStructuredText from another file)
302
    -- in Sphinx, when given an absolute include file path, this directive takes
303
    it as relative to the source directory
304
  - :dudir:`class` (assign a class attribute to the next element) [1]_
305
 
306
* HTML specifics:
307
 
308
  - :dudir:`meta` (generation of HTML ``<meta>`` tags)
309
  - :dudir:`title` (override document title)
310
 
311
* Influencing markup:
312
 
313
  - :dudir:`default-role` (set a new default role)
314
  - :dudir:`role` (create a new role)
315
 
316
  Since these are only per-file, better use Sphinx' facilities for setting the
317
  :confval:`default_role`.
318
 
319
Do *not* use the directives :dudir:`sectnum`, :dudir:`header` and
320
:dudir:`footer`.
321
 
322
Directives added by Sphinx are described in :ref:`sphinxmarkup`.
323
 
324
Basically, a directive consists of a name, arguments, options and content. (Keep
325
this terminology in mind, it is used in the next chapter describing custom
326
directives.)  Looking at this example, ::
327
 
328
   .. function:: foo(x)
329
                 foo(y, z)
330
      :module: some.module.name
331
 
332
      Return a line of text input from the user.
333
 
334
``function`` is the directive name.  It is given two arguments here, the
335
remainder of the first line and the second line, as well as one option
336
``module`` (as you can see, options are given in the lines immediately following
337
the arguments and indicated by the colons).  Options must be indented to the
338
same level as the directive content.
339
 
340
The directive content follows after a blank line and is indented relative to the
341
directive start.
342
 
343
 
344
Images
345
------
346
 
347
reST supports an image directive (:dudir:`ref <image>`), used like so::
348
 
349
   .. image:: gnu.png
350
      (options)
351
 
352
When used within Sphinx, the file name given (here ``gnu.png``) must either be
353
relative to the source file, or absolute which means that they are relative to
354
the top source directory.  For example, the file ``sketch/spam.rst`` could refer
355
to the image ``images/spam.png`` as ``../images/spam.png`` or
356
``/images/spam.png``.
357
 
358
Sphinx will automatically copy image files over to a subdirectory of the output
359
directory on building (e.g. the ``_static`` directory for HTML output.)
360
 
361
Interpretation of image size options (``width`` and ``height``) is as follows:
362
if the size has no unit or the unit is pixels, the given size will only be
363
respected for output channels that support pixels (i.e. not in LaTeX output).
364
Other units (like ``pt`` for points) will be used for HTML and LaTeX output.
365
 
366
Sphinx extends the standard docutils behavior by allowing an asterisk for the
367
extension::
368
 
369
   .. image:: gnu.*
370
 
371
Sphinx then searches for all images matching the provided pattern and determines
372
their type.  Each builder then chooses the best image out of these candidates.
373
For instance, if the file name ``gnu.*`` was given and two files :file:`gnu.pdf`
374
and :file:`gnu.png` existed in the source tree, the LaTeX builder would choose
375
the former, while the HTML builder would prefer the latter.
376
 
377
.. versionchanged:: 0.4
378
   Added the support for file names ending in an asterisk.
379
 
380
.. versionchanged:: 0.6
381
   Image paths can now be absolute.
382
 
383
 
384
Footnotes
385
---------
386
 
387
For footnotes (:duref:`ref <footnotes>`), use ``[#name]_`` to mark the footnote
388
location, and add the footnote body at the bottom of the document after a
389
"Footnotes" rubric heading, like so::
390
 
391
   Lorem ipsum [#f1]_ dolor sit amet ... [#f2]_
392
 
393
   .. rubric:: Footnotes
394
 
395
   .. [#f1] Text of the first footnote.
396
   .. [#f2] Text of the second footnote.
397
 
398
You can also explicitly number the footnotes (``[1]_``) or use auto-numbered
399
footnotes without names (``[#]_``).
400
 
401
 
402
Citations
403
---------
404
 
405
Standard reST citations (:duref:`ref <citations>`) are supported, with the
406
additional feature that they are "global", i.e. all citations can be referenced
407
from all files.  Use them like so::
408
 
409
   Lorem ipsum [Ref]_ dolor sit amet.
410
 
411
   .. [Ref] Book or article reference, URL or whatever.
412
 
413
Citation usage is similar to footnote usage, but with a label that is not
414
numeric or begins with ``#``.
415
 
416
 
417
Substitutions
418
-------------
419
 
420
reST supports "substitutions" (:duref:`ref <substitution-definitions>`), which
421
are pieces of text and/or markup referred to in the text by ``|name|``.  They
422
are defined like footnotes with explicit markup blocks, like this::
423
 
424
   .. |name| replace:: replacement *text*
425
 
426
or this::
427
 
428
   .. |caution| image:: warning.png
429
                :alt: Warning!
430
 
431
See the :duref:`reST reference for substitutions <substitution-definitions>`
432
for details.
433
 
434
If you want to use some substitutions for all documents, put them into
435
:confval:`rst_prolog` or put them into a separate file and include it into all
436
documents you want to use them in, using the :rst:dir:`include` directive.  (Be
437
sure to give the include file a file name extension differing from that of other
438
source files, to avoid Sphinx finding it as a standalone document.)
439
 
440
Sphinx defines some default substitutions, see :ref:`default-substitutions`.
441
 
442
 
443
Comments
444
--------
445
 
446
Every explicit markup block which isn't a valid markup construct (like the
447
footnotes above) is regarded as a comment (:duref:`ref <comments>`).  For
448
example::
449
 
450
   .. This is a comment.
451
 
452
You can indent text after a comment start to form multiline comments::
453
 
454
   ..
455
      This whole indented block
456
      is a comment.
457
 
458
      Still in the comment.
459
 
460
 
461
Source encoding
462
---------------
463
 
464
Since the easiest way to include special characters like em dashes or copyright
465
signs in reST is to directly write them as Unicode characters, one has to
466
specify an encoding.  Sphinx assumes source files to be encoded in UTF-8 by
467
default; you can change this with the :confval:`source_encoding` config value.
468
 
469
 
470
Gotchas
471
-------
472
 
473
There are some problems one commonly runs into while authoring reST documents:
474
 
475
* **Separation of inline markup:** As said above, inline markup spans must be
476
  separated from the surrounding text by non-word characters, you have to use a
477
  backslash-escaped space to get around that.  See `the reference
478
  <http://docutils.sf.net/docs/ref/rst/restructuredtext.html#inline-markup>`_
479
  for the details.
480
 
481
* **No nested inline markup:** Something like ``*see :func:`foo`*`` is not
482
  possible.
483
 
484
 
485
.. rubric:: Footnotes
486
 
487
.. [1] When the default domain contains a :rst:dir:`class` directive, this directive
488
       will be shadowed.  Therefore, Sphinx re-exports it as :rst:dir:`rst-class`.
489
 
 
 

The reStructuredText mode supports one configuration parameter:

verbatim (string)
A name or MIME type of a mode that will be used for highlighting verbatim blocks. By default, reStructuredText mode uses uniform color for whole block of verbatim text if no mode is given.

If python mode is available, it will be used for highlighting blocks containing Python/IPython terminal sessions (blocks starting with >>> (for Python) or In [num]: (for IPython).

MIME types defined: text/x-rst.