wvxvw
wvxvw

Reputation: 9465

How do I insert highlight or code-block into Sphinx-style docstrings?

For example:

def foo():
    '''
    .. highlight:: python
    import sys
    '''

Doesn't produce desired output (it prints the word "highlight" verbatim and doesn't format the following code in any special way). Same happens for code-block.

I tried different indentation etc. No matter what, generator succeeds with roughly the same, but not the desired output.

Upvotes: 21

Views: 29328

Answers (1)

Chen A.
Chen A.

Reputation: 11280

Comparing your code with the docs, you are missing indentation and an empty line between the highlight and the actual code. It should be like this:

def foo():
    '''
    .. highlight:: python
    .. code-block:: python

        import sys
        ...
    '''

Upvotes: 32

Related Questions