blob: 8a4fc3dcdf6aa4fde802dd4911ce30d2b4e980e4 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001.. highlightlang:: rest
2
3reStructuredText Primer
4=======================
5
6This section is a brief introduction to reStructuredText (reST) concepts and
7syntax, intended to provide authors with enough information to author documents
8productively. Since reST was designed to be a simple, unobtrusive markup
9language, this will not take too long.
10
11.. seealso::
12
13 The authoritative `reStructuredText User
14 Documentation <http://docutils.sourceforge.net/rst.html>`_.
15
16
17Paragraphs
18----------
19
20The paragraph is the most basic block in a reST document. Paragraphs are simply
21chunks of text separated by one or more blank lines. As in Python, indentation
22is significant in reST, so all lines of the same paragraph must be left-aligned
23to the same level of indentation.
24
25
26Inline markup
27-------------
28
29The standard reST inline markup is quite simple: use
30
31* one asterisk: ``*text*`` for emphasis (italics),
32* two asterisks: ``**text**`` for strong emphasis (boldface), and
33* backquotes: ````text```` for code samples.
34
35If asterisks or backquotes appear in running text and could be confused with
36inline markup delimiters, they have to be escaped with a backslash.
37
38Be aware of some restrictions of this markup:
39
40* it may not be nested,
41* content may not start or end with whitespace: ``* text*`` is wrong,
42* it must be separated from surrounding text by non-word characters. Use a
43 backslash escaped space to work around that: ``thisis\ *one*\ word``.
44
45These restrictions may be lifted in future versions of the docutils.
46
47reST also allows for custom "interpreted text roles"', which signify that the
48enclosed text should be interpreted in a specific way. Sphinx uses this to
49provide semantic markup and cross-referencing of identifiers, as described in
50the appropriate section. The general syntax is ``:rolename:`content```.
51
52
53Lists and Quotes
54----------------
55
56List markup is natural: just place an asterisk at the start of a paragraph and
57indent properly. The same goes for numbered lists; they can also be
58autonumbered using a ``#`` sign::
59
60 * This is a bulleted list.
61 * It has two items, the second
62 item uses two lines.
63
64 1. This is a numbered list.
65 2. It has two items too.
66
67 #. This is a numbered list.
68 #. It has two items too.
69
70Note that Sphinx disables the use of enumerated lists introduced by alphabetic
71or roman numerals, such as ::
72
73 A. First item
74 B. Second item
75
76
77Nested lists are possible, but be aware that they must be separated from the
78parent list items by blank lines::
79
80 * this is
81 * a list
82
83 * with a nested list
84 * and some subitems
85
86 * and here the parent list continues
87
88Definition lists are created as follows::
89
90 term (up to a line of text)
91 Definition of the term, which must be indented
92
93 and can even consist of multiple paragraphs
94
95 next term
96 Description.
97
98
99Paragraphs are quoted by just indenting them more than the surrounding
100paragraphs.
101
102
103Source Code
104-----------
105
106Literal code blocks are introduced by ending a paragraph with the special marker
107``::``. The literal block must be indented, to be able to include blank lines::
108
109 This is a normal text paragraph. The next paragraph is a code sample::
110
111 It is not processed in any way, except
112 that the indentation is removed.
113
114 It can span multiple lines.
115
116 This is a normal text paragraph again.
117
118The handling of the ``::`` marker is smart:
119
120* If it occurs as a paragraph of its own, that paragraph is completely left
121 out of the document.
122* If it is preceded by whitespace, the marker is removed.
123* If it is preceded by non-whitespace, the marker is replaced by a single
124 colon.
125
126That way, the second sentence in the above example's first paragraph would be
127rendered as "The next paragraph is a code sample:".
128
129
130Hyperlinks
131----------
132
133External links
134^^^^^^^^^^^^^^
135
136Use ```Link text <http://target>`_`` for inline web links. If the link text
137should be the web address, you don't need special markup at all, the parser
138finds links and mail addresses in ordinary text.
139
140Internal links
141^^^^^^^^^^^^^^
142
143Internal linking is done via a special reST role, see the section on specific
144markup, :ref:`doc-ref-role`.
145
146
147Sections
148--------
149
150Section headers are created by underlining (and optionally overlining) the
151section title with a punctuation character, at least as long as the text::
152
153 =================
154 This is a heading
155 =================
156
157Normally, there are no heading levels assigned to certain characters as the
158structure is determined from the succession of headings. However, for the
159Python documentation, we use this convention:
160
161* ``#`` with overline, for parts
162* ``*`` with overline, for chapters
163* ``=``, for sections
164* ``-``, for subsections
165* ``^``, for subsubsections
166* ``"``, for paragraphs
167
168
169Explicit Markup
170---------------
171
172"Explicit markup" is used in reST for most constructs that need special
173handling, such as footnotes, specially-highlighted paragraphs, comments, and
174generic directives.
175
176An explicit markup block begins with a line starting with ``..`` followed by
177whitespace and is terminated by the next paragraph at the same level of
178indentation. (There needs to be a blank line between explicit markup and normal
179paragraphs. This may all sound a bit complicated, but it is intuitive enough
180when you write it.)
181
182
183Directives
184----------
185
186A directive is a generic block of explicit markup. Besides roles, it is one of
187the extension mechanisms of reST, and Sphinx makes heavy use of it.
188
189Basically, a directive consists of a name, arguments, options and content. (Keep
190this terminology in mind, it is used in the next chapter describing custom
191directives.) Looking at this example, ::
192
193 .. function:: foo(x)
194 foo(y, z)
195 :bar: no
196
197 Return a line of text input from the user.
198
199``function`` is the directive name. It is given two arguments here, the
200remainder of the first line and the second line, as well as one option ``bar``
201(as you can see, options are given in the lines immediately following the
202arguments and indicated by the colons).
203
204The directive content follows after a blank line and is indented relative to the
205directive start.
206
207
208Footnotes
209---------
210
211For footnotes, use ``[#]_`` to mark the footnote location, and add the footnote
212body at the bottom of the document after a "Footnotes" rubric heading, like so::
213
214 Lorem ipsum [#]_ dolor sit amet ... [#]_
215
216 .. rubric:: Footnotes
217
218 .. [#] Text of the first footnote.
219 .. [#] Text of the second footnote.
220
221You can also explicitly number the footnotes for better context.
222
223
224Comments
225--------
226
227Every explicit markup block which isn't a valid markup construct (like the
228footnotes above) is regarded as a comment.
229
230
231Source encoding
232---------------
233
234Since the easiest way to include special characters like em dashes or copyright
235signs in reST is to directly write them as Unicode characters, one has to
236specify an encoding:
237
238All Python documentation source files must be in UTF-8 encoding, and the HTML
239documents written from them will be in that encoding as well.
240
241
242Gotchas
243-------
244
245There are some problems one commonly runs into while authoring reST documents:
246
247* **Separation of inline markup:** As said above, inline markup spans must be
248 separated from the surrounding text by non-word characters, you have to use
249 an escaped space to get around that.
250
251.. XXX more?