<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>3. reStructuredText Primer — Python v3.2.2 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '3.2.2', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python v3.2.2 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python v3.2.2 documentation" href="../index.html" /> <link rel="up" title="Documenting Python" href="index.html" /> <link rel="next" title="4. Additional Markup Constructs" href="markup.html" /> <link rel="prev" title="2. Style guide" href="style.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="markup.html" title="4. Additional Markup Constructs" accesskey="N">next</a> |</li> <li class="right" > <a href="style.html" title="2. Style guide" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="../index.html">Python v3.2.2 documentation</a> »</li> <li><a href="index.html" accesskey="U">Documenting Python</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="restructuredtext-primer"> <h1>3. reStructuredText Primer<a class="headerlink" href="#restructuredtext-primer" title="Permalink to this headline">¶</a></h1> <p>This section is a brief introduction to reStructuredText (reST) concepts and syntax, intended to provide authors with enough information to author documents productively. Since reST was designed to be a simple, unobtrusive markup language, this will not take too long.</p> <div class="admonition-see-also admonition seealso"> <p class="first admonition-title">See also</p> <p class="last">The authoritative <a class="reference external" href="http://docutils.sourceforge.net/rst.html">reStructuredText User Documentation</a>.</p> </div> <div class="section" id="paragraphs"> <h2>3.1. Paragraphs<a class="headerlink" href="#paragraphs" title="Permalink to this headline">¶</a></h2> <p>The paragraph is the most basic block in a reST document. Paragraphs are simply chunks of text separated by one or more blank lines. As in Python, indentation is significant in reST, so all lines of the same paragraph must be left-aligned to the same level of indentation.</p> </div> <div class="section" id="inline-markup"> <h2>3.2. Inline markup<a class="headerlink" href="#inline-markup" title="Permalink to this headline">¶</a></h2> <p>The standard reST inline markup is quite simple: use</p> <ul class="simple"> <li>one asterisk: <tt class="docutils literal"><span class="pre">*text*</span></tt> for emphasis (italics),</li> <li>two asterisks: <tt class="docutils literal"><span class="pre">**text**</span></tt> for strong emphasis (boldface), and</li> <li>backquotes: <tt class="docutils literal"><span class="pre">``text``</span></tt> for code samples.</li> </ul> <p>If asterisks or backquotes appear in running text and could be confused with inline markup delimiters, they have to be escaped with a backslash.</p> <p>Be aware of some restrictions of this markup:</p> <ul class="simple"> <li>it may not be nested,</li> <li>content may not start or end with whitespace: <tt class="docutils literal"><span class="pre">*</span> <span class="pre">text*</span></tt> is wrong,</li> <li>it must be separated from surrounding text by non-word characters. Use a backslash escaped space to work around that: <tt class="docutils literal"><span class="pre">thisis\</span> <span class="pre">*one*\</span> <span class="pre">word</span></tt>.</li> </ul> <p>These restrictions may be lifted in future versions of the docutils.</p> <p>reST also allows for custom “interpreted text roles”’, which signify that the enclosed text should be interpreted in a specific way. Sphinx uses this to provide semantic markup and cross-referencing of identifiers, as described in the appropriate section. The general syntax is <tt class="docutils literal"><span class="pre">:rolename:`content`</span></tt>.</p> </div> <div class="section" id="lists-and-quotes"> <h2>3.3. Lists and Quotes<a class="headerlink" href="#lists-and-quotes" title="Permalink to this headline">¶</a></h2> <p>List markup is natural: just place an asterisk at the start of a paragraph and indent properly. The same goes for numbered lists; they can also be autonumbered using a <tt class="docutils literal"><span class="pre">#</span></tt> sign:</p> <div class="highlight-rest"><div class="highlight"><pre><span class="m">*</span> This is a bulleted list. <span class="m">*</span> It has two items, the second item uses two lines. <span class="m">1.</span> This is a numbered list. <span class="m">2.</span> It has two items too. <span class="m">#.</span> This is a numbered list. <span class="m">#.</span> It has two items too. </pre></div> </div> <p>Nested lists are possible, but be aware that they must be separated from the parent list items by blank lines:</p> <div class="highlight-rest"><div class="highlight"><pre><span class="m">*</span> this is <span class="m">*</span> a list <span class="m">*</span> with a nested list <span class="m">*</span> and some subitems <span class="m">*</span> and here the parent list continues </pre></div> </div> <p>Definition lists are created as follows:</p> <div class="highlight-rest"><div class="highlight"><pre>term (up to a line of text) Definition of the term, which must be indented and can even consist of multiple paragraphs next term Description. </pre></div> </div> <p>Paragraphs are quoted by just indenting them more than the surrounding paragraphs.</p> </div> <div class="section" id="source-code"> <h2>3.4. Source Code<a class="headerlink" href="#source-code" title="Permalink to this headline">¶</a></h2> <p>Literal code blocks are introduced by ending a paragraph with the special marker <tt class="docutils literal"><span class="pre">::</span></tt>. The literal block must be indented:</p> <div class="highlight-rest"><div class="highlight"><pre>This is a normal text paragraph. The next paragraph is a code sample<span class="se">::</span> <span class="s"> It is not processed in any way, except</span> <span class="s"> that the indentation is removed.</span> <span class="s"> It can span multiple lines.</span> This is a normal text paragraph again. </pre></div> </div> <p>The handling of the <tt class="docutils literal"><span class="pre">::</span></tt> marker is smart:</p> <ul class="simple"> <li>If it occurs as a paragraph of its own, that paragraph is completely left out of the document.</li> <li>If it is preceded by whitespace, the marker is removed.</li> <li>If it is preceded by non-whitespace, the marker is replaced by a single colon.</li> </ul> <p>That way, the second sentence in the above example’s first paragraph would be rendered as “The next paragraph is a code sample:”.</p> </div> <div class="section" id="hyperlinks"> <h2>3.5. Hyperlinks<a class="headerlink" href="#hyperlinks" title="Permalink to this headline">¶</a></h2> <div class="section" id="external-links"> <h3>3.5.1. External links<a class="headerlink" href="#external-links" title="Permalink to this headline">¶</a></h3> <p>Use <tt class="docutils literal"><span class="pre">`Link</span> <span class="pre">text</span> <span class="pre"><http://target>`_</span></tt> for inline web links. If the link text should be the web address, you don’t need special markup at all, the parser finds links and mail addresses in ordinary text.</p> </div> <div class="section" id="internal-links"> <h3>3.5.2. Internal links<a class="headerlink" href="#internal-links" title="Permalink to this headline">¶</a></h3> <p>Internal linking is done via a special reST role, see the section on specific markup, <a class="reference internal" href="markup.html#doc-ref-role"><em>Cross-linking markup</em></a>.</p> </div> </div> <div class="section" id="sections"> <h2>3.6. Sections<a class="headerlink" href="#sections" title="Permalink to this headline">¶</a></h2> <p>Section headers are created by underlining (and optionally overlining) the section title with a punctuation character, at least as long as the text:</p> <div class="highlight-rest"><div class="highlight"><pre><span class="gh">=================</span> <span class="gh">This is a heading</span> <span class="gh">=================</span> </pre></div> </div> <p>Normally, there are no heading levels assigned to certain characters as the structure is determined from the succession of headings. However, for the Python documentation, we use this convention:</p> <ul class="simple"> <li><tt class="docutils literal"><span class="pre">#</span></tt> with overline, for parts</li> <li><tt class="docutils literal"><span class="pre">*</span></tt> with overline, for chapters</li> <li><tt class="docutils literal"><span class="pre">=</span></tt>, for sections</li> <li><tt class="docutils literal"><span class="pre">-</span></tt>, for subsections</li> <li><tt class="docutils literal"><span class="pre">^</span></tt>, for subsubsections</li> <li><tt class="docutils literal"><span class="pre">"</span></tt>, for paragraphs</li> </ul> </div> <div class="section" id="explicit-markup"> <h2>3.7. Explicit Markup<a class="headerlink" href="#explicit-markup" title="Permalink to this headline">¶</a></h2> <p>“Explicit markup” is used in reST for most constructs that need special handling, such as footnotes, specially-highlighted paragraphs, comments, and generic directives.</p> <p>An explicit markup block begins with a line starting with <tt class="docutils literal"><span class="pre">..</span></tt> followed by whitespace and is terminated by the next paragraph at the same level of indentation. (There needs to be a blank line between explicit markup and normal paragraphs. This may all sound a bit complicated, but it is intuitive enough when you write it.)</p> </div> <div class="section" id="directives"> <h2>3.8. Directives<a class="headerlink" href="#directives" title="Permalink to this headline">¶</a></h2> <p>A directive is a generic block of explicit markup. Besides roles, it is one of the extension mechanisms of reST, and Sphinx makes heavy use of it.</p> <p>Basically, a directive consists of a name, arguments, options and content. (Keep this terminology in mind, it is used in the next chapter describing custom directives.) Looking at this example,</p> <div class="highlight-rest"><div class="highlight"><pre><span class="p">..</span> <span class="ow">function</span><span class="p">::</span> foo(x) foo(y, z) <span class="nc">:bar:</span> <span class="nf">no</span> Return a line of text input from the user. </pre></div> </div> <p><tt class="docutils literal"><span class="pre">function</span></tt> is the directive name. It is given two arguments here, the remainder of the first line and the second line, as well as one option <tt class="docutils literal"><span class="pre">bar</span></tt> (as you can see, options are given in the lines immediately following the arguments and indicated by the colons).</p> <p>The directive content follows after a blank line and is indented relative to the directive start.</p> </div> <div class="section" id="footnotes"> <h2>3.9. Footnotes<a class="headerlink" href="#footnotes" title="Permalink to this headline">¶</a></h2> <p>For footnotes, use <tt class="docutils literal"><span class="pre">[#]_</span></tt> to mark the footnote location, and add the footnote body at the bottom of the document after a “Footnotes” rubric heading, like so:</p> <div class="highlight-rest"><div class="highlight"><pre>Lorem ipsum <span class="s">[#]_</span> dolor sit amet ... <span class="s">[#]_</span> <span class="p">..</span> <span class="ow">rubric</span><span class="p">::</span> Footnotes <span class="p">..</span> <span class="nt">[#]</span> Text of the first footnote. <span class="p">..</span> <span class="nt">[#]</span> Text of the second footnote. </pre></div> </div> <p>You can also explicitly number the footnotes for better context.</p> </div> <div class="section" id="comments"> <h2>3.10. Comments<a class="headerlink" href="#comments" title="Permalink to this headline">¶</a></h2> <p>Every explicit markup block which isn’t a valid markup construct (like the footnotes above) is regarded as a comment.</p> </div> <div class="section" id="source-encoding"> <h2>3.11. Source encoding<a class="headerlink" href="#source-encoding" title="Permalink to this headline">¶</a></h2> <p>Since the easiest way to include special characters like em dashes or copyright signs in reST is to directly write them as Unicode characters, one has to specify an encoding:</p> <p>All Python documentation source files must be in UTF-8 encoding, and the HTML documents written from them will be in that encoding as well.</p> </div> <div class="section" id="gotchas"> <h2>3.12. Gotchas<a class="headerlink" href="#gotchas" title="Permalink to this headline">¶</a></h2> <p>There are some problems one commonly runs into while authoring reST documents:</p> <ul class="simple"> <li><strong>Separation of inline markup:</strong> As said above, inline markup spans must be separated from the surrounding text by non-word characters, you have to use an escaped space to get around that.</li> </ul> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">3. reStructuredText Primer</a><ul> <li><a class="reference internal" href="#paragraphs">3.1. Paragraphs</a></li> <li><a class="reference internal" href="#inline-markup">3.2. Inline markup</a></li> <li><a class="reference internal" href="#lists-and-quotes">3.3. Lists and Quotes</a></li> <li><a class="reference internal" href="#source-code">3.4. Source Code</a></li> <li><a class="reference internal" href="#hyperlinks">3.5. Hyperlinks</a><ul> <li><a class="reference internal" href="#external-links">3.5.1. External links</a></li> <li><a class="reference internal" href="#internal-links">3.5.2. Internal links</a></li> </ul> </li> <li><a class="reference internal" href="#sections">3.6. Sections</a></li> <li><a class="reference internal" href="#explicit-markup">3.7. Explicit Markup</a></li> <li><a class="reference internal" href="#directives">3.8. Directives</a></li> <li><a class="reference internal" href="#footnotes">3.9. Footnotes</a></li> <li><a class="reference internal" href="#comments">3.10. Comments</a></li> <li><a class="reference internal" href="#source-encoding">3.11. Source encoding</a></li> <li><a class="reference internal" href="#gotchas">3.12. Gotchas</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="style.html" title="previous chapter">2. Style guide</a></p> <h4>Next topic</h4> <p class="topless"><a href="markup.html" title="next chapter">4. Additional Markup Constructs</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li><a href="../_sources/documenting/rest.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" size="18" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="markup.html" title="4. Additional Markup Constructs" >next</a> |</li> <li class="right" > <a href="style.html" title="2. Style guide" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="../index.html">Python v3.2.2 documentation</a> »</li> <li><a href="index.html" >Documenting Python</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2011, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Sep 04, 2011. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7. </div> </body> </html>