4 Code Blocks#

https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#showing-code-examples

4.1 Literal Blocks#

https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html#literal-blocks

https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#literal-blocks

This is a code sample:

print('Hello, world!')

4.2 Quoted Literal Blocks#

https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#quoted-literal-blocks

Supported initial characters: ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~:

! every line
! must
! start with the same character

4.3 code-block Directive#

https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-code-block

print('Hello,')
print('wooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooorld!')
# another code block

No language:

┌─┬┐  ╔═╦╗  ╓─╥╖  ╒═╤╕
│ ││  ║ ║║  ║ ║║  │ ││
├─┼┤  ╠═╬╣  ╟─╫╢  ╞═╪╡
└─┴┘  ╚═╩╝  ╙─╨╜  ╘═╧╛
┌───────────────────┐
│  ╔═══╗ Some Text  │▒
│  ╚═╦═╝ in the box │▒
╞═╤══╩══╤═══════════╡▒
│ ├──┬──┤           │▒
│ └──┴──┘           │▒
└───────────────────┘▒
 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒

:linenos: option:

1print('Hello,')
2print('wooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooorld!')
1# another code block

:linenos: with :lineno-start::

12345print('Hello,')
12346print('world!')

:emphasize-lines:

def some_function():
    interesting = False
    print('This line is highlighted.')
    print('This one is not...')
    print('...but this one is.')

:emphasize-lines: and :linenos::

1def some_function():
2    interesting = False
3    print('This line is highlighted.')
4    print('This one is not...')
5    print('...but this one is.')

:caption::

Listing 4.1 This is a :caption:#
print('Hello, world!')

:caption: and :linenos::

Listing 4.2 This is another :caption:#
1print('Hello,')
2print('world!')

See also [source] link in insipid_sphinx_theme.

4.4 parsed-literal Directive#

https://docutils.sourceforge.io/docs/ref/rst/directives.html#parsed-literal

Code block with inline markup, e.g. literal text[*].
Code block with nothing special

Warning

Syntax highlighting (including providing a background color) is sometimes broken, see https://github.com/sphinx-doc/sphinx/issues/2167.

4.5 Doctest Blocks#

https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#doctest-blocks

>>> print('this is a Doctest block')
this is a Doctest block

4.6 Nesting#

Note

'code in note'

Some text with inline code.

'some more code'

Warning

1def some_function():
2    interesting = False
3    print('This line is highlighted.')
4    print('This one is not...')
5    print('...but this one is.')
'code in quote'

Warning

definition term
'code in definition in admonition'
Code After Sidebar
==================

.

.

.

.

.

.

.

A long long long long long long long long long long long long long line.