reStructuredText syntax¶
The Sphinx Stack supports reStructuredText (reST).
See the following sections for syntax help and conventions.
Note
For general style conventions, see the Canonical Documentation Style Guide.
Headings¶
Input |
Description |
|---|---|
Title
=====
|
Page title and H1 heading |
Heading
-------
|
H2 heading |
Heading
~~~~~~~
|
H3 heading |
Heading
^^^^^^^
|
H4 heading |
Heading
.......
|
H5 heading |
Underlines must be at least as long as the title or heading.
Inline formatting¶
Input |
Output |
|---|---|
|
UI element |
|
|
|
|
|
command |
|
Key |
|
Italic |
|
Bold |
Code blocks¶
To start a code block, explicitly start a code block with .. code-block::. The code block must be surrounded by empty lines.
Input |
Output |
|---|---|
.. code-block::
# Demonstrate a code block
code:
- example: true
|
# Demonstrate a code block
code:
- example: true
|
.. code-block:: yaml
# Demonstrate a code block
code:
- example: true
|
# Demonstrate a code block
code:
- example: true
|
Terminal output¶
A terminal view emulates the command line experience more accurately than a code block. This is particularly useful in tutorials or guides that are terminal-heavy, or where it’s helpful to show the directory a command is run from.
To include a terminal view, use the following directive:
Input |
Output |
|---|---|
.. terminal::
input line 1
input line 2
output line 1
output line 2
output line 3
|
user@host:~$input line 1 > input line 2output line 1
output line 2
output line 3
|
By default, everything before the first blank line in the directive’s content is rendered as input, and any content that follows is rendered as output. The terminal directive can only display one input command, but that command can span multiple lines, as in the previous example.
To render only the output of a command, include the :output-only: flag in the
directive’s options:
Input |
Output |
|---|---|
.. terminal::
:output-only:
This is rendered as output.
|
This is rendered as output.
|
To customize the prompt (user@host:~$ by default), specify any of the following options:
:user::host::dir:
Input |
Output |
|---|---|
.. terminal::
:user: author
:host: canonical
:dir: ~/path
input
output
|
author@canonical:~/path$inputoutput
|
The copy button for input commands is opt-in. You must include the :copy: flag
in the directive’s options for the button to be displayed.
To make the terminal scroll horizontally instead of wrapping long lines, include the :scroll: option.
For more details, refer to the sphinx-terminal README.
Links¶
Link markup depends on whether you need an external URL or a page in the same documentation set.
External links¶
To link to documents in other Sphinx projects, use Intersphinx with the :ref: or :doc: role:
Input |
Output |
|---|---|
:external+ubuntu-desktop:doc:`index`
|
|
:external+ubuntu-desktop:ref:`install-ubuntu-desktop`
|
To link to other websites, use the hyperlink reference syntax:
Input |
Output |
|---|---|
`Canonical home <https://canonical.com>`__
|
If necessary, it’s possible to write a standalone hyperlink, which won’t contain any link text:
Input |
Output |
|---|---|
https://canonical.com
|
The documentation checks will likely flag it as a spelling error.
Plain text¶
Outside of directives, reST interprets every URL it finds as a hyperlink. If you need a link to be rendered as plain text, escape the colon in the protocol:
Input |
Output |
|---|---|
https\://canonical.com/ |
Manual pages¶
When mentioning command-line utilities, you may wish to link to the
corresponding manual page for the command. Ensure that the manpages_url
setting in your conf.py is set appropriately and use the :manpage:
role within your text to create a link.
For example, to link to man pages from the 24.04 LTS (Noble Numbat) release,
include the following in your conf.py:
manpages_url = "https://manpages.ubuntu.com/manpages/noble/en/man{section}/{page}.{section}.html"
Then within the document, use the following reST:
You can use the :manpage:`dd(1)` utility to write the disk image to your
SD card. If the image is compressed, use :manpage:`aunpack(1)` to extract
it first.
YouTube¶
To add a link to a YouTube video, use the following directive:
Input |
Output |
|---|---|
.. youtube:: https://www.youtube.com/watch?v=iMLiK1fX4I0
:title: Demo
|
The video title is extracted automatically and displayed when hovering over the link.
To override the title, add the :title: option.
Internal references¶
Sections¶
To reference a section within the documentation (either on the same page or on another page), add a label to that section and reference that label.
You can add a label anywhere in any document. When referencing a label that isn’t attached to a heading, you must add link text. If you don’t, the reference won’t work.
Input |
Output |
Description |
|---|---|---|
|
Adds the label Note |
|
|
References a label that has a title. |
|
|
References a label and specifies a title. |
Pages¶
If a documentation page does not have a label, you can still reference it by using the :doc: role with the file name and path.
Input |
Output |
|---|---|
|
|
|
Only use the :doc: role when you cannot use the :ref: role, thus only if there
is no label at the top of the file and you cannot add it. When using the :doc:
role, your reference will break when a file is renamed or moved.
Formatted link text¶
With the exception of inline code, reST doesn’t support special formatting for link text, such as emphasized and strong text.
Use the :literalref: role to format a reference’s link text as inline code:
Input |
Output |
|---|---|
|
|
|
The link text is automatically excluded from the spelling check.
Lists¶
Input |
Output |
|---|---|
- Item 1
- Item 2
- Item 3
|
|
1. Step 1
#. Step 2
#. Step 3
|
|
a. Step 1
#. Step 2
#. Step 3
|
|
You can also nest lists:
1. Step 1
- Item 1
* Sub-item
- Item 2
i. Sub-step 1
#. Sub-step 2
#. Step 2
a. Sub-step 1
- Item
#. Sub-step 2
Step 1
Item 1
Sub-item
Item 2
Sub-step 1
Sub-step 2
Step 2
Sub-step 1
Item
Sub-step 2
In numbered lists, number the first item and use #. for all subsequent items to
generate the step numbers automatically.
Use - for unordered lists. When using nested lists, you can use * for the
nested level.
Definition lists¶
Input |
Output |
|---|---|
Term 1:
Definition
Term 2:
Definition
|
|
Tables¶
reST supports different markup for tables. Grid tables are most similar to tables in Markdown, but list tables are usually much easier to use. See the Sphinx documentation for all table syntax alternatives.
Both markups result in the following output:
Header 1 |
Header 2 |
|---|---|
Cell 1 Second paragraph cell 1 |
Cell 2 |
Cell 3 |
Cell 4 |
Grid tables¶
See grid tables for reference.
+----------------------+------------+
| Header 1 | Header 2 |
+======================+============+
| Cell 1 | Cell 2 |
| | |
| 2nd paragraph cell 1 | |
+----------------------+------------+
| Cell 3 | Cell 4 |
+----------------------+------------+
List tables¶
See list tables for reference.
.. list-table::
:header-rows: 1
* - Header 1
- Header 2
* - Cell 1
2nd paragraph cell 1
- Cell 2
* - Cell 3
- Cell 4
Data tables¶
If you have a small amount of CSV data, you can include the data in the doc source.
For example:
.. csv-table::
:header: "Animal", "Number of legs", "Size"
"Worm", 0, "Small"
"Penguin", 2, "Medium"
"Horse", 4, "Large"
"Ant", 6, "Small"
"Octopus", 8, "Medium"
If you have a large amount of CSV data, or the data is generated by an automated process, you can include the data from a file.
For example:
.. csv-table::
:file: /assets/animals.csv
:header-rows: 1
Both markups result in the following output:
Animal |
Number of legs |
Size |
|---|---|---|
Worm |
0 |
Small |
Penguin |
2 |
Medium |
Horse |
4 |
Large |
Ant |
6 |
Small |
Octopus |
8 |
Medium |
Customize the column widths, character encoding, and so on, as described in the csv-table reference.
The Sphinx Stack can also render interactive tables, which are described in Add interactive data tables.
Notes¶
.. admonition:: <title>
:class: <class>
Images¶
Input |
Output |
|---|---|
|
|
.. figure:: https://assets.ubuntu.com/v1/b3b72cb2-canonical-logo-166.png
:width: 100px
:alt: Alt text
Figure caption
|
Figure caption¶ |
For local pictures, start the path with / (for example, /images/image.png).
Use PNG format for screenshots and SVG format for graphics.
If producing multiple output formats, use * as the file extension to have
Sphinx select the best image format for the output
See Five golden rules for compliant alt text for information about how to word the alt text.
Reuse¶
A big advantage of reST in comparison to plain Markdown is that it allows the reuse of content.
Substitution¶
To reuse sentences and entire paragraphs that have little markup or special formatting, define substitutions for them by putting the same directives in any reST file:
.. |version_number| replace:: 0.1.0
.. |rest_text| replace:: *Multi-line* text
that uses basic **markup**.
.. And so on
Note
The definitions from the above examples are rendered as follows:
Input |
Output |
|---|---|
|
0.1.0 |
|
Multi-line text that uses basic markup. |
|
File inclusion¶
To reuse longer sections or text with more advanced markup, you can put the content in a separate file and include the file or parts of the file in several locations.
To select parts of the text in a file, use :start-after: and :end-before: if
possible. You can combine those with :start-line: and :end-line: if
the same text occurs more than once. Using only :start-line: and :end-line: is
error-prone though.
You cannot put any labels into the content that is being reused (because references to this label would be ambiguous then). You can, however, put a label right before including the file.
By combining file inclusion and substitutions defined directly in a file, you can even replace parts of the included text.
Input |
Output |
|---|---|
.. include:: /how-to/index.rst
:start-after: .. _how-to-guides:
:end-before: =============
|
How-to guides |
Files that only contain text that is reused somewhere else should be placed in the
reuse directory and end with the extension .txt to distinguish them from
normal content files.
To make sure inclusions don’t break, consider adding comments (.. some comment) to
the source file as markers for starting and ending.
Tabs¶
The recommended way of creating tabs is with the Sphinx design extension.
Input |
Output |
|---|---|
.. tab-set::
.. tab-item:: Tab 1
:sync: key1
Content Tab 1
.. tab-item:: Tab 2
:sync: key2
Content Tab 2
|
Content Tab 1 Content Tab 2 |
Glossary¶
You can define glossary terms in any file. Ideally, all terms should be collected in one glossary so they can then be referenced from any file.
Input |
Output |
|---|---|
.. glossary::
an example term
Definition of an example term.
|
|
|
More useful markup¶
Input |
Output |
Description |
|---|---|---|
.. versionadded:: X.Y
|
Added in version X.Y. |
Can be used to distinguish between different versions. |
| Line 1
| Line 2
| Line 3
|
Line 1
Line 2
Line 3
|
Line breaks that are not paragraphs. Use this sparingly. |
----
|
A horizontal line |
Can be used to visually divide sections on a page. |
|
Not visible in the output. |
|
|
API |
Hover to display the full term. |
|
Explicitly exempt a term from the spelling check. |
|
.. meta::
:description: Your description of the page.
|
Not displayed |
Set a custom HTML meta description with this directive at the top of the document. Don’t put newlines in the description. |