Themes » Writing reST content

While the of­fi­cial re­Struc­tured­Text doc­u­ment­a­tion provides an ex­tens­ive syn­tax guide, the info is scattered across many pages. The fol­low­ing page will try to ex­plain the ba­sics and men­tion a bunch of tricks that might not be im­me­di­ately ob­vi­ous.

Ba­sic syn­tax

reST is at its core op­tim­ized for fast con­tent au­thor­ing — the text is eas­ily hu­man-read­able, with no ex­cess­ive markup, of­ten-used format­ting is kept simple and everything is ex­tens­ible to al­low in­tu­it­ive us­age in many use cases. The ba­sic rules are not far from Mark­down:

  • Head­ers are made by un­der­lin­ing a line of text with some non-al­pha­bet­ic char­ac­ter. Length of the un­der­line has to be same as length of the title. Us­ing a dif­fer­ent char­ac­ter will cre­ate a next level head­ing, there’s no rule which char­ac­ter means what (but fol­low­ing some con­sist­ent rules across all your con­tent is en­cour­aged).
  • Para­graphs are sep­ar­ated from each oth­er by a blank line. Con­sec­ut­ive lines of text are con­cat­en­ated to­geth­er us­ing spaces, ig­nor­ing the newlines. See the Ba­sic block ele­ments sec­tion be­low for more op­tions.
  • Mul­tiple con­sec­ut­ive whitespace char­ac­ters are rendered as a single space (but that’s largely due to how HTML be­haves).
  • Everything is es­caped when con­vert­ing to HTML, so in­line HTML will ap­pear as-is, un­like Mark­down.
  • As with Py­thon, in­dent­a­tion mat­ters in reST. Dif­fer­ently in­den­ted lines of text are usu­ally sep­ar­ated via a newline, but ex­cep­tions to this rule ex­ist.
  • Lines start­ing with .. are treated as com­ment to­geth­er with fol­low­ing in­den­ted lines as long as the in­dent­a­tion level is not less than in­dent­a­tion of the first line.

Ex­ample:

Heading 1
#########

First paragraph with a bunch of text. Some more text in that paragraph. You can
wrap your paragraph nicely to fit on your editor screen, the lines will be
concatenated together with a space in between.

Second paragraph of text. Each of these paragraphs is rendered as a HTML <p>
character. The main document heading is not part of the content, but rather an
implicit document title metadata.

Heading 2
=========

The above heading is using a different underline character so it will be
rendered as <h2> in the output. Also, the whole section (heading + content
until the next heading) is rendered as HTML5 <section> element.

Heading 3
---------

Another character used, another level of headings.

.. a comment, which is simply ignored when producing HTML output.
    This is still a comment.

    This as well.
        and
        this
    also

This is not a comment anymore.

Heading 2
=========

Going back to already used character, reST remembers for which heading
level it was used the first time, so it goes back to <h2>.

There’s more in­form­a­tion about ba­sic syn­tax rules in the of­fi­cial reST doc­u­ment­a­tion.

Metadata fields

Each doc­u­ment can have a bunch of metadata fields that are not rendered as part of the main doc­u­ment con­tent. Ex­ample of these is an art­icle sum­mary, date or over­rid­ing where giv­en page is saved. Metadata field starts with :name:, where name is field name. After the colon there is a space and field con­tents. Con­sec­ut­ive in­den­ted lines are treated as part of the same field.

An article
##########

:date: 2017-10-11
:author: Vladimír Vondruš
:summary: Article summary. Because article summary can be quite long, let's
    wrap it on multiple lines.

Article content starts here.

See the Pel­ic­an doc­u­ment­a­tion for de­tails about re­cog­nized fields and how vari­ous metadata can be also auto­mat­ic­ally ex­trac­ted from the filesys­tem. The m.css Pel­ic­an theme re­cog­nizes a few more fields.

Dir­ect­ives

Spe­cial block ele­ments (for ex­ample to in­clude an im­age) are called dir­ect­ives, are in­tro­duced by a line start­ing with .. name::, where name is dir­ect­ive name, after the :: there can be op­tion­al po­s­i­tion­al ar­gu­ments. After that there can be an op­tion­al set of named dir­ect­ive op­tions (in­den­ted lines start­ing with :name: where name is op­tion name) and after that op­tion­al dir­ect­ive con­tent, also in­den­ted. Un­indent­ing will es­cape the dir­ect­ive. Dir­ect­ives can be nes­ted and it’s pos­sible to provide user-defined dir­ect­ives via plu­gins.

Ex­ample and cor­res­pond­ing out­put, note the in­dent­a­tion:

Paragraph with text.

.. block-info:: Info block title

    Info block content.

    .. figure:: ship.jpg
        :alt: Image alt text

        Figure title

        Figure content.

        .. math::

            A = \pi r^2

        This is again figure content.

    This is again info block content.

And this is another paragraph of text, outside of the info block.

Para­graph with text.

And this is an­oth­er para­graph of text, out­side of the info block.

In­ter­preted text roles

While dir­ect­ives are for block ele­ments, in­ter­preted text roles are for in­line ele­ments. They are part of a para­graph and are in form of :name:\`contents\`, where name is role name and con­tents are role con­tents. The role has to be sep­ar­ated with non-al­pha­nu­mer­ic char­ac­ter from the sur­round­ings; if you need to avoid the space, es­cape it with \; sim­il­arly with the ` char­ac­ter, if you need to use it in­side. Un­like dir­ect­ives, roles can’t be nes­ted. Roles are also ex­tens­ible via plu­gins.

Roles, like dir­ect­ives, also have op­tions, but the only way to use them is to define a new role based off the ori­gin­al one with the op­tions you need. Use the .. role:: dir­ect­ive like in the fol­low­ing snip­pet, where ori­gin­al is op­tion­al name of the ori­gin­al role to de­rive from, new is the name of new role and fol­lows a list of op­tions:

.. role:: new(original)
    :option1: value1
    :option2: value2

Ex­ample and a cor­res­pond­ing out­put:

.. role:: red
    :class: m-text m-danger

A text paragraph with :emphasis:`emphasised text`, :strong:`strong text`
and :literal:`typewriter`\ y text. :red:`This text is red.`

A text para­graph with em­phas­ised text, strong text and typewritery text. This text is red.

Ba­sic block ele­ments

Be­sides head­ings and simple para­graphs, there are more block ele­ments like quotes, lit­er­al blocks, tables etc. with im­pli­cit syn­tax. Some­times you might want to sep­ar­ate two in­den­ted blocks, use a blank line con­tain­ing just .. to achieve that. Like dir­ect­ives, block ele­ments are also nestable, so you can have lists in­side quotes and the like.

| Line block
| will
| preserve the newlines

    A quote is simply an indented block.

..

    A different quote.

::

    Literal block is itroduced with ::, which can be even part of previous
    paragraph (in which case it's reduced to a single colon).

.. class:: m-table

========= ============
Heading 1 Heading 2
========= ============
Cell 1    Table cell 2
Row 2     Row 2 cell 2
Row 3     Row 3 cell 3
========= ============

-   An unordered list
-   Another item

    1.  Sub-list, ordered
    2.  Another item
    3.  Note that the sub-list is separated by blank lines

-   Third item of the top-level list

Term 1
    Description
Term 2
    Description of term 2

Line block
will
pre­serve the newlines

A quote is simply an in­den­ted block.
A dif­fer­ent quote.
Literal block is itroduced with ::, which can be even the ending of
previous paragraph (in which case it's reduced to a single colon).
Head­ing 1 Head­ing 2
Cell 1 Cell 2
Row 2 Row 2 cell 2
Row 3 Cell 3
  • An un­ordered list
  • An­oth­er item
    1. Sub-list, ordered
    2. An­oth­er item
    3. Note that the sub-list is sep­ar­ated by blank lines
  • Third item of the top-level list
Term 1
De­scrip­tion
Term 2
De­scrip­tion of term 2

The of­fi­cial reST doc­u­ment­a­tion has more de­tailed info about block ele­ments.

Ba­sic in­line ele­ments

The :emphasis: role can be writ­ten short­er by wrap­ping the con­tent in *, :strong: us­ing ** and :literal: with ``. A single back­tick \` is re­served and you can re­define to whatever you need in giv­en scope us­ing the de­fault-role dir­ect­ive.

Ex­tern­al links are writ­ten us­ing \`title <URL>\`_, where title is link text and URL is the link des­tin­a­tion. The link title can be then used again to link to the same URL as simply \`title\`_. Note that spe­cify­ing two links with the same title and dif­fer­ent URLs is an er­ror — if you need that, use an­onym­ous links that are in a form of \`title <URL>\`__ (two un­der­scores after).

In­tern­al links: every head­ing can be linked to us­ing \`heading\`_, where head­ing is the head­ing text, moreover the head­ing it­self can be wrapped in back­ticks and an un­der­score to make it a click­able link point­ing to it­self. Ar­bit­rary an­chors in­side the text flow that can be linked to later can be cre­ated us­ing the .. _anchor: syn­tax.

.. _an anchor:

An *emphasised text*, **strong text** and a ``literal``. Link to
`Google <https://google.com>`_, `the heading below <#a-heading>`_ or just an
URL as-is: https://mcss.mosra.cz/.

`A heading`_
============

Repeated link to `Google`_. Anonymous links that share the same titles
`here <http://blog.mosra.cz>`__ and `here <https://magnum.graphics/>`__.
Link to `an anchor`_ above.

An em­phas­ised text, strong text and a literal. Link to Google, the head­ing be­low or just an URL as-is: https://mcss.mosra.cz/.

A heading

Re­peated link to Google. An­onym­ous links that share the same titles here and here. Link to an an­chor above.

There are some spe­cial fea­tures in Pel­ic­an for easi­er link­ing to in­tern­al con­tent, be sure to check out the doc­u­ment­a­tion. The reST doc­u­ment­a­tion also of­fers more de­tailed info about in­line markup.

Es­sen­tial dir­ect­ives

  • A con­tain­er dir­ect­ive will just put a <div> around its con­tents and its ar­gu­ments will be put as CSS classes to that ele­ment.
  • A class dir­ect­ive will put its ar­gu­ment as CSS class to the im­me­di­ately fol­low­ing ele­ment. Be­sides that, most of the dir­ect­ives also ac­cept a :class: op­tion that does the same.
  • Some­times you need to put raw HTML code onto your page (for ex­ample to em­bed a third-party wid­get). Use the raw dir­ect­ive to achieve that.
  • For in­clud­ing lar­ger por­tions of raw HTML code or big­ger code snip­pets there’s the in­clude dir­ect­ive, you can also spe­cify just a por­tion of the file either by line num­bers or text match.
  • The con­tents dir­ect­ive will auto­mat­ic­ally make a Table of Con­tents list out of head­ings in your doc­u­ment. Very use­ful for nav­ig­a­tion in large pages and art­icles.

For stuff like im­ages, fig­ures, code blocks, math list­ing etc., m.css provides vari­ous plu­gins that do it bet­ter than the built­in way. Head over to the of­fi­cial reST doc­u­ment­a­tion for more info about built­in dir­ect­ives.

Es­sen­tial in­ter­preted text roles

  • Be­sides the already men­tioned roles, there’s also a sup and sub role for su­per­script and sub­script text.
  • It’s also pos­sible to put raw HTML code in­line by de­riv­ing from the raw role.

Again, m.css provides vari­ous plu­gins that al­low you to have in­line code, math, Git­Hub and Doxy­gen links and much more. Head over to the of­fi­cial reST doc­u­ment­a­tion for more info about built­in roles.