<a id="rst-syntax"></a>

# reStructuredText syntax

The Sphinx Stack supports [reStructuredText](https://www.sphinx-doc.org/en/master/usage/restructuredtext/index.html) (reST).

See the following sections for syntax help and conventions.

#### NOTE
This guide assumes that you are using the [Sphinx Stack](https://github.com/canonical/sphinx-stack). Some of the mentioned syntax
requires the Sphinx extensions enabled in the Sphinx Stack.

For general style conventions, see the [Canonical Documentation Style Guide](https://docs.ubuntu.com/styleguide/en).

## Headings

| Input                                   | Description               |
|-----------------------------------------|---------------------------|
| ```none<br/>Title<br/>=====<br/>```     | Page title and H1 heading |
| ```none<br/>Heading<br/>-------<br/>``` | H2 heading                |
| ```none<br/>Heading<br/>~~~~~~~<br/>``` | H3 heading                |
| ```none<br/>Heading<br/>^^^^^^^<br/>``` | H4 heading                |
| ```none<br/>Heading<br/>.......<br/>``` | H5 heading                |

Underlines must be at least as long as the title or heading.

## Inline formatting

| Input                    | Output      |
|--------------------------|-------------|
| `:guilabel:`UI element`` | UI element  |
| ```code```               | `code`      |
| `:file:`file path``      | `file path` |
| `:command:`command``     | **command** |
| `:kbd:`Key``             | `Key`       |
| `*Italic*`               | *Italic*    |
| `**Bold**`               | **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                                                                       |
|---------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------|
| ```none<br/>.. code-block::<br/><br/>   # Demonstrate a code block<br/>   code:<br/>   - example: true<br/>```      | ```none<br/># Demonstrate a code block<br/>code:<br/>- example: true<br/>``` |
| ```none<br/>.. code-block:: yaml<br/><br/>   # Demonstrate a code block<br/>   code:<br/>   - example: true<br/>``` | ```yaml<br/># Demonstrate a code block<br/>code:<br/>- example: true<br/>``` |

### 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                                                                                                                          |
|--------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------|
| ```text<br/>.. terminal::<br/><br/>    input line 1<br/>    input line 2<br/><br/>    output line 1<br/>    output line 2<br/><br/>    output line 3<br/>``` | `user@host:~$ ``input line 1<br/>> input line 2`<br/>```text<br/>output line 1<br/>output line 2<br/><br/>output line 3<br/>``` |

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                                          |
|--------------------------------------------------------------------------------------------------|-------------------------------------------------|
| ```text<br/>.. terminal::<br/>    :output-only:<br/><br/>    This is rendered as output.<br/>``` | ```text<br/>This is rendered as output.<br/>``` |

To customize the prompt (`user@host:~$` by default), specify any of the following options:

* `:user:`
* `:host:`
* `:dir:`

| Input                                                                                                                                        | Output                                                            |
|----------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------|
| ```text<br/>.. terminal::<br/>    :user: author<br/>    :host: canonical<br/>    :dir: ~/path<br/><br/>    input<br/><br/>    output<br/>``` | `author@canonical:~/path$ ``input`<br/>```text<br/>output<br/>``` |

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](https://github.com/canonical/sphinx-terminal/blob/main/README.md).

## Links

Link markup depends on whether you need an external URL or a page in the same
documentation set.

<a id="reference-external-link-syntax"></a>

### External links

To link to documents in other Sphinx projects, use [Intersphinx](https://documentation.ubuntu.com/sphinx-stack/latest/how-to/optional-customisation/external-referencing-intersphinx/index.html.md#how-to-link-docs-intersphinx) with the `:ref:` or `:doc:` role:

| Input                                                                    | Output                                                                                                                      |
|--------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------|
| ```rst<br/>:external+ubuntu-desktop:doc:`index`<br/>```                  | [Ubuntu Desktop documentation](https://ubuntu.com/desktop/docs/en/latest/)                                                  |
| ```rst<br/>:external+ubuntu-desktop:ref:`install-ubuntu-desktop`<br/>``` | [Install Ubuntu Desktop](https://ubuntu.com/desktop/docs/en/latest/tutorial/install-ubuntu-desktop/#install-ubuntu-desktop) |

To link to other websites, use the hyperlink reference syntax:

| Input                                                         | Output                                  |
|---------------------------------------------------------------|-----------------------------------------|
| ```rst<br/>`Canonical home <https://canonical.com>`__<br/>``` | [Canonical home](https://canonical.com) |

If necessary, it’s possible to write a standalone hyperlink, which won’t contain any
link text:

| Input                                    | Output                                         |
|------------------------------------------|------------------------------------------------|
| ```rst<br/>https://canonical.com<br/>``` | [https://canonical.com](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/ | <spellexception>https://canonical.com/</spellexception> |

#### Sidebar links

You can add links to related websites or Discourse topics to the sidebar.

To add a link to a related website, add the following field at the top of the page:

```text
:relatedlinks: https://github.com/canonical/lxd-sphinx-extensions, [RTFM](https://www.google.com)
```

To override the title, use Markdown syntax. Note that spaces are ignored; if you need spaces in the title, replace them with `&#32;`, and include the value in quotes if Sphinx complains about the metadata value because it starts with `[`.
For example: `[My&#32;Title](https://...)`.

To add a link to a Discourse topic, configure the Discourse instance in the
conf.py file. Then add the following field at the top of the page:

```rst
:discourse: <topic-id>
```

#### 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`:

```python
manpages_url = "https://manpages.ubuntu.com/manpages/noble/en/man{section}/{page}.{section}.html"
```

Then within the document, use the following reST:

```rst
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                                                                                                                                                                                                                                                                                                               |
|--------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ```none<br/>.. youtube:: https://www.youtube.com/watch?v=iMLiK1fX4I0<br/>   :title: Demo<br/>``` | <br/>            <p class="youtube_link"><br/>              <a href="https://www.youtube.com/watch?v=iMLiK1fX4I0" target="_blank"><br/>                <span title="Demo" class="play_icon">▶</span><br/>                <span title="Demo">Watch on YouTube</span><br/>              </a><br/>            </p><br/> |

The video title is extracted automatically and displayed when hovering over the link.
To override the title, add the `:title:` option.

### Internal references

<a id="a-section-label"></a>

#### 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.

<a id="a-random-label"></a>

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                                                                                                                                                                      |
|----------------------------------------------|-------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `.. _label_ID:`                              |                                                                   | Adds the label `label_ID`.<br/><br/>#### NOTE<br/>When defining the label, you must prefix it with an underscore. Do not use the starting underscore when referencing the label. |
| `:ref:`a_section_label``                     | [Sections](#a-section-label)           | References a label that has a title.                                                                                                                                             |
| `:ref:`Provided link text <a_random_label>`` | [Provided link text](#a-random-label) | 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                                                            |
|-------------------------------------|-------------------------------------------------------------------|
| `:doc:`index``                      | [Reference](https://documentation.ubuntu.com/sphinx-stack/latest/reference/index.html.md)          |
| `:doc:`Provided link text <index>`` | [Provided link text](https://documentation.ubuntu.com/sphinx-stack/latest/reference/index.html.md) |

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                                                 |
|-----------------------------------------------|--------------------------------------------------------|
| `:literalref:`example <https://example.com>`` | [`example`](https://example.com)                       |
| `:literalref:`label <a_random_label>``        | [`label`](#a-random-label) |

The link text is automatically excluded from the spelling check.

## Navigation

Every documentation page must be included as a sub-page to another page in the navigation.

This is achieved with the [toctree](https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-toctree)
directive in the parent page:

```text
.. toctree::
   :hidden:

   sub-page1
   sub-page2
```

If a page should not be included in the navigation, you can suppress the resulting build
warning by putting `:orphan:` at the top of the file. Use orphan pages sparingly and
only if there is a clear reason for it.

#### TIP
Instead of hiding pages that you do not want to include in the documentation from
the navigation, you can exclude them from being built. This method will also prevent
them from being found through the search.

To exclude pages from the build, add them to the `exclude_patterns` variable in the
`conf.py` file.

## Lists

| Input                                                     | Output                                |
|-----------------------------------------------------------|---------------------------------------|
| ```none<br/>- Item 1<br/>- Item 2<br/>- Item 3<br/>```    | - Item 1<br/>- Item 2<br/>- Item 3    |
| ```none<br/>1. Step 1<br/>#. Step 2<br/>#. Step 3<br/>``` | 1. Step 1<br/>2. Step 2<br/>3. Step 3 |
| ```none<br/>a. Step 1<br/>#. Step 2<br/>#. Step 3<br/>``` | 1. Step 1<br/>2. Step 2<br/>3. Step 3 |

You can also nest lists:

### Input

```none
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
```

### Output

1. Step 1

> - Item 1
>   * Sub-item
> - Item 2
>   1. Sub-step 1
>   2. Sub-step 2
1. Step 2

> 1. Sub-step 1

> > - Item
> 1. 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                                                     |
|---------------------------------------------------------------------------|------------------------------------------------------------|
| ```none<br/>Term 1:<br/>  Definition<br/>Term 2:<br/>  Definition<br/>``` | Term 1:<br/>: Definition<br/><br/>Term 2:<br/>: Definition |

<a id="style-guide-tables"></a>

## 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]()

<a id="tables"></a>
 for all table syntax alternatives.

Both markups result in the following output:

| Header 1                                | Header 2   |
|-----------------------------------------|------------|
| Cell 1<br/><br/>Second paragraph cell 1 | Cell 2     |
| Cell 3                                  | Cell 4     |

### Grid tables

See [grid tables](https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#grid-tables) for
reference.

```none
+----------------------+------------+
| Header 1             | Header 2   |
+======================+============+
| Cell 1               | Cell 2     |
|                      |            |
| 2nd paragraph cell 1 |            |
+----------------------+------------+
| Cell 3               | Cell 4     |
+----------------------+------------+
```

### List tables

See [list tables](https://docutils.sourceforge.io/docs/ref/rst/directives.html#list-table) for
reference.

```none
.. 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:

```rst
.. 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:

```rst
.. 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](https://docutils.sourceforge.io/docs/ref/rst/directives.html#csv-table).

The Sphinx Stack can also render interactive tables, which are described in
[Add interactive data tables](https://documentation.ubuntu.com/sphinx-stack/latest/how-to/optional-customisation/interactive-tables/index.html.md#interactive-tables).

## Notes

```rst
.. admonition:: <title>
    :class: <class>
```

## Images

| Input                                                                                                                                                           | Output                                                                    |
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------|
| `.. image:: https://assets.ubuntu.com/v1/b3b72cb2-canonical-logo-166.png`                                                                                       | ![image](https://assets.ubuntu.com/v1/b3b72cb2-canonical-logo-166.png)    |
| ```none<br/>.. figure:: https://assets.ubuntu.com/v1/b3b72cb2-canonical-logo-166.png<br/>  :width: 100px<br/>  :alt: Alt text<br/><br/>  Figure caption<br/>``` | ![Alt text](https://assets.ubuntu.com/v1/b3b72cb2-canonical-logo-166.png) |

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](https://abilitynet.org.uk/resources/digital-accessibility/five-golden-rules-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.

<a id="reference-substitution-syntax"></a>

### Substitution

To reuse sentences and entire paragraphs that have little markup or special formatting,
define [substitutions](https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html#substitutions)
for them by putting the same directives in any reST file:

```rst
.. |version_number| replace:: 0.1.0

.. |rest_text| replace:: *Multi-line* text
                          that uses basic **markup**.

.. And so on
```

#### NOTE
Mind that substitutions can’t be redefined; for instance, accidentally including a
definition twice causes an error:

```none
ERROR: Duplicate substitution definition name: "rest_text".
```

The definitions from the above examples are rendered as follows:

| Input              | Output                                            |
|--------------------|---------------------------------------------------|
| `|version_number|` | 0.1.0                                             |
| `|rest_text|`      | *Multi-line* text<br/>that uses basic **markup**. |
| `|site_link|_`     | [Website link](https://example.com)               |

### 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        |
|----------------------------------------------------------------------------------------------------------------------------|---------------|
| ```none<br/>.. include:: /how-to/index.rst<br/>  :start-after: .. _how-to-guides:<br/>  :end-before: =============<br/>``` | 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](https://sphinx-design.readthedocs.io/en/latest/) extension.

| Input                                                                                                                                                                                                | Output                                                                     |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------|
| ```none<br/>.. tab-set::<br/><br/>  .. tab-item:: Tab 1<br/>      :sync: key1<br/><br/>      Content Tab 1<br/><br/>  .. tab-item:: Tab 2<br/>      :sync: key2<br/><br/>      Content Tab 2<br/>``` | ### Tab 1<br/><br/>Content Tab 1<br/><br/>### Tab 2<br/><br/>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                                                                                          |
|-----------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------|
| ```none<br/>.. glossary::<br/><br/>  an example term<br/>    Definition of an example term.<br/>``` | <a id="term-an-example-term"></a><br/><br/>an example term<br/>: Definition of an example term. |
| `:term:`an example term``                                                                           | [an example term](#term-an-example-term)                                  |

<a id="section-more-useful-markup"></a>

## More useful markup

| Input                                                                           | Output                                                     | Description                                                                                                               |
|---------------------------------------------------------------------------------|------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------|
| ```none<br/>.. versionadded:: X.Y<br/>```                                       | #### Versionadded<br/>Added in version X.Y.                | Can be used to distinguish between different versions.                                                                    |
| ```none<br/>| Line 1<br/>| Line 2<br/>| Line 3<br/>```                          | Line 1<br/><br/><br/>Line 2<br/><br/><br/>Line 3<br/><br/> | Line breaks that are not paragraphs. Use this sparingly.                                                                  |
| ```none<br/>----<br/>```                                                        | A horizontal line                                          | Can be used to visually divide sections on a page.                                                                        |
| `.. This is a comment`                                                          | <!-- This is a comment -->                                 | Not visible in the output.                                                                                                |
| `:abbr:`API (Application Programming Interface)``                               |                                                            | Hover to display the full term.                                                                                           |
| `:spellexception:`PurposelyWrong``                                              | <spellexception>PurposelyWrong</spellexception>            | Explicitly exempt a term from the spelling check.                                                                         |
| ```rst<br/>.. meta::<br/>   :description: Your description of the page.<br/>``` | Not displayed                                              | Set a custom HTML meta description with this directive at the top of the document. Don’t put newlines in the description. |
