Movatterモバイル変換


[0]ホーム

URL:


Navigation

3. reStructuredText Primer

This section is a brief introduction to reStructuredText (reST) concepts andsyntax, intended to provide authors with enough information to author documentsproductively. Since reST was designed to be a simple, unobtrusive markuplanguage, this will not take too long.

See also

The authoritativereStructuredText UserDocumentation.

3.1. Paragraphs

The paragraph is the most basic block in a reST document. Paragraphs are simplychunks of text separated by one or more blank lines. As in Python, indentationis significant in reST, so all lines of the same paragraph must be left-alignedto the same level of indentation.

3.2. Inline markup

The standard reST inline markup is quite simple: use

  • one asterisk:*text* for emphasis (italics),
  • two asterisks:**text** for strong emphasis (boldface), and
  • backquotes:``text`` for code samples.

If asterisks or backquotes appear in running text and could be confused withinline markup delimiters, they have to be escaped with a backslash.

Be aware of some restrictions of this markup:

  • it may not be nested,
  • content may not start or end with whitespace:*text* is wrong,
  • it must be separated from surrounding text by non-word characters. Use abackslash escaped space to work around that:thisis\*one*\word.

These restrictions may be lifted in future versions of the docutils.

reST also allows for custom “interpreted text roles”’, which signify that theenclosed text should be interpreted in a specific way. Sphinx uses this toprovide semantic markup and cross-referencing of identifiers, as described inthe appropriate section. The general syntax is:rolename:`content`.

3.3. Lists and Quotes

List markup is natural: just place an asterisk at the start of a paragraph andindent properly. The same goes for numbered lists; they can also beautonumbered using a# sign:

* This is a bulleted list.* It has two items, the second  item uses two lines.1. This is a numbered list.2. It has two items too.#. This is a numbered list.#. It has two items too.

Nested lists are possible, but be aware that they must be separated from theparent list items by blank lines:

* this is* a list* with a nested list* and some subitems* and here the parent list continues

Definition lists are created as follows:

term (up to a line of text)   Definition of the term, which must be indented   and can even consist of multiple paragraphsnext term   Description.

Paragraphs are quoted by just indenting them more than the surroundingparagraphs.

3.4. Source Code

Literal code blocks are introduced by ending a paragraph with the special marker::. The literal block must be indented:

This is a normal text paragraph. The next paragraph is a code sample::   It is not processed in any way, except   that the indentation is removed.   It can span multiple lines.This is a normal text paragraph again.

The handling of the:: marker is smart:

  • If it occurs as a paragraph of its own, that paragraph is completely leftout of the document.
  • If it is preceded by whitespace, the marker is removed.
  • If it is preceded by non-whitespace, the marker is replaced by a singlecolon.

That way, the second sentence in the above example’s first paragraph would berendered as “The next paragraph is a code sample:”.

3.5. Hyperlinks

3.5.1. External links

Use`Linktext<http://target>`_ for inline web links. If the link textshould be the web address, you don’t need special markup at all, the parserfinds links and mail addresses in ordinary text.

3.5.2. Internal links

Internal linking is done via a special reST role, see the section on specificmarkup,Cross-linking markup.

3.6. Sections

Section headers are created by underlining (and optionally overlining) thesection title with a punctuation character, at least as long as the text:

=================This is a heading=================

Normally, there are no heading levels assigned to certain characters as thestructure is determined from the succession of headings. However, for thePython documentation, we use this convention:

  • # with overline, for parts
  • * with overline, for chapters
  • =, for sections
  • -, for subsections
  • ^, for subsubsections
  • ", for paragraphs

3.7. Explicit Markup

“Explicit markup” is used in reST for most constructs that need specialhandling, such as footnotes, specially-highlighted paragraphs, comments, andgeneric directives.

An explicit markup block begins with a line starting with.. followed bywhitespace and is terminated by the next paragraph at the same level ofindentation. (There needs to be a blank line between explicit markup and normalparagraphs. This may all sound a bit complicated, but it is intuitive enoughwhen you write it.)

3.8. Directives

A directive is a generic block of explicit markup. Besides roles, it is one ofthe extension mechanisms of reST, and Sphinx makes heavy use of it.

Basically, a directive consists of a name, arguments, options and content. (Keepthis terminology in mind, it is used in the next chapter describing customdirectives.) Looking at this example,

..function:: foo(x)              foo(y, z):bar:no   Return a line of text input from the user.

function is the directive name. It is given two arguments here, theremainder of the first line and the second line, as well as one optionbar(as you can see, options are given in the lines immediately following thearguments and indicated by the colons).

The directive content follows after a blank line and is indented relative to thedirective start.

3.9. Footnotes

For footnotes, use[#]_ to mark the footnote location, and add the footnotebody at the bottom of the document after a “Footnotes” rubric heading, like so:

Lorem ipsum[#]_ dolor sit amet ...[#]_..rubric:: Footnotes..[#] Text of the first footnote...[#] Text of the second footnote.

You can also explicitly number the footnotes for better context.

3.10. Comments

Every explicit markup block which isn’t a valid markup construct (like thefootnotes above) is regarded as a comment.

3.11. Source encoding

Since the easiest way to include special characters like em dashes or copyrightsigns in reST is to directly write them as Unicode characters, one has tospecify an encoding:

All Python documentation source files must be in UTF-8 encoding, and the HTMLdocuments written from them will be in that encoding as well.

3.12. Gotchas

There are some problems one commonly runs into while authoring reST documents:

  • Separation of inline markup: As said above, inline markup spans must beseparated from the surrounding text by non-word characters, you have to usean escaped space to get around that.

Table Of Contents

Previous topic

2. Style Guide

Next topic

4. Additional Markup Constructs

This Page

Quick search

Enter search terms or a module, class or function name.

Navigation

©Copyright 1990-2012, Python Software Foundation.
The Python Software Foundation is a non-profit corporation.Please donate.
Last updated on Apr 09, 2012.Found a bug?
Created usingSphinx 0.6.5.

[8]ページ先頭

©2009-2026 Movatter.jp