File: code_and_apis.md

package info (click to toggle)
myst-parser 4.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,348 kB
  • sloc: python: 7,402; xml: 1,383; makefile: 33; sh: 25
file content (251 lines) | stat: -rw-r--r-- 6,715 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
(syntax/code-blocks)=
# Source code and APIs

## Basic block syntax highlighting

Code blocks contain a language identifier, which is used to determine the language of the code.
This language is used to determine the syntax highlighting, using an available [pygments lexer](https://pygments.org/docs/lexers/).

:::{myst-example}
```python
from a import b
c = "string"
```
:::

::::{admonition} Adding a language lexer
:class: tip dropdown
You can create and register your own lexer, using the [`pygments.lexers` entry point](https://pygments.org/docs/plugins/#register-plugins),
or within a sphinx extension, with the [`app.add_lexer` method](inv:sphinx#*.Sphinx.add_lexer).
::::

::::{admonition} Show backticks inside raw markdown blocks
:class: tip dropdown

If you'd like to show backticks inside of your markdown,
you can do so by nesting them in backticks of a greater length.
Markdown will treat the outer-most backticks as the edges of the "raw" block and everything inside will show up.
For example:

:::{myst-example}
`` `hi` ``

````
```
hi
```
````
:::

::::

## Inline syntax highlighting

The [attrs_inline](#syntax/attributes/inline) extension can be used to apply syntax highlighting to inline code:

:::{myst-example}
Inline Python code `a = "b"`{l=python}
:::

## Numbering and highlighting lines

To set a global default for line numbering, per lexer name,
the `myst_number_code_blocks` [configuration option](#sphinx/config-options) can be used.
For example, using:

```python
myst_number_code_blocks = ["typescript"]
```

Will number all code blocks with the `typescript` lexer by default.

:::{myst-example}
```typescript
type MyBool = true | false;

interface User {
  name: string;
  id: number;
}
```
:::

To apply numbering and highlighting to a specific code block,
the [attrs_block](#syntax/attributes/block) extension can be used:

:::{myst-example}
{lineno-start=1 emphasize-lines="2,3"}
```python
a = 1
b = 2
c = 3
```
:::

## Adding a caption

With the `code-block` {{directive}},
a caption can be added to a code blocks, as well as other options:

:::{myst-example}
```{code-block} python
:caption: This is a caption
:emphasize-lines: 2,3
:lineno-start: 1

a = 1
b = 2
c = 3
```
:::

The following options are recognized:

:::{admonition} Code block options
:class: hint dropdown

`linenos` : flag
: Enable to generate line numbers for the code block

`lineno-start` : integer
: The starting line number for the code block (`linenos` is automatically enabled)

`emphasize-lines` : comma-separated list of integers
: Highlight the specified lines

`caption` : string
: The caption for the code block

`force` : flag
: Allow minor errors on highlighting to be ignored

`name` : string
: The name of the code block, which can be referenced elsewhere in the document

`class` : string
: The class to apply to the code block

```{seealso}
The [Sphinx documentation](inv:sphinx#code-block)
```

:::

(syntax/literalinclude)=
## Including code from files

Longer pieces of code can be included from files using the `literalinclude` {{directive}}:

:::{myst-example}
```{literalinclude} examples/example.py
```
:::

The file name is usually relative to the current file’s path. However, if it is absolute (starting with `/`), it is relative to the top source directory.

To select only a sub-section of the file, the `lines`, `pyobject` or `start-after` and `end-before` options can be used:

:::{myst-example}
```{literalinclude} examples/example.py
:start-after: start example
:end-before: end example
```
:::

```{seealso}
The [Sphinx documentation](inv:sphinx#literalinclude)
```

(syntax/apis)=
## Documenting whole APIs

Sphinx and MyST provide means to analyse source code and automatically generate documentation and referenceable links for APIs.

`sphinx.ext.autodoc` can be used ([see below](#syntax/apis/sphinx-autodoc)), however, it is not inherently compatible with MyST Markdown, and so the `sphinx-autodoc2` extension is recommended.

(syntax/apis/sphinx-autodoc2)=
### `sphinx-autodoc2`

[`sphinx-autodoc2`](https://sphinx-autodoc2.readthedocs.io) is an extension for Sphinx that provides an integrated means to document Python APIs.

As opposed to `sphinx.ext.autodoc`, `sphinx-autodoc2` performs static (rather than dynamic) analysis of the source code, integrates full package documenting, and also allows for docstrings to be written in both RestructureText and MyST.

The `auto_mode` will automatically generate the full API documentation, as shown <project:/apidocs/index.rst>.

Alternatively, the `autodoc2-object` directive can be used to generate documentation for a single object.
To embed in a MyST document the MyST `render_plugin` should be specified, for example:

````{myst-example}
```{autodoc2-object} myst_parser.sphinx_ext.main.setup_sphinx
render_plugin = "myst"
no_index = true
```
````

This can be referenced elsewhere in the document using the `:py:obj:` role, or a `#` link (see [cross-referencing](#syntax/referencing)).

````{myst-example}
- {py:obj}`myst_parser.sphinx_ext.main.setup_sphinx`
- [](#myst_parser.sphinx_ext.main.setup_sphinx)
````

Additionally, summaries of multiple objects can be generated using the `autodoc2-summary` directive:

````{myst-example}
```{autodoc2-summary}
:renderer: myst

~myst_parser.sphinx_ext.main.setup_sphinx
~myst_parser.sphinx_ext.main.create_myst_config
```
````

#### Using MyST docstrings

`sphinx-autodoc2` can be configured to use MyST docstrings (rather than RestructureText), for the entire project or select objects, by setting the `autodoc2_docstring_parser_regexes` configuration option:

```python
autodoc2_docstring_parser_regexes = [
    # this will render all docstrings as Markdown
    (r".*", "myst"),
    # this will render select docstrings as Markdown
    (r"mypackage\.mymodule\..*", "myst"),
]
```

For example:

````{myst-example}
```{autodoc2-object} myst_parser.setup
render_plugin = "myst"
no_index = true
docstring_parser_regexes = [
    ["myst_parser\\.setup", "myst"],
]
```
````

(syntax/apis/sphinx-autodoc)=
### `sphinx.ext.autodoc`

[Sphinx extension `autodoc`](inv:sphinx#sphinx.ext.autodoc) also can generate documentation for Python objects.
However, because it is hard-coded to generate RestructureText, the special [`eval-rst` directive](#syntax/directives/parsing) needs to be used:

````{myst-example}
```{eval-rst}
.. autofunction:: myst_parser.sphinx_ext.main.setup_sphinx
    :noindex:
```
````

Summaries can also be generated with [`autosummary`](inv:sphinx#sphinx.ext.autosummary):

````{myst-example}
```{eval-rst}
.. autosummary::
    :nosignatures:

    myst_parser.sphinx_ext.main.setup_sphinx
    myst_parser.sphinx_ext.main.create_myst_config
```
````