Table Of Contents

sphinx-inlinecode

sphinx-inlinecode PyPI Version sphinx-inlinecode GitHub Repository https://static.pepy.tech/personalized-badge/sphinx-inlinecode?period=total&units=none&left_color=grey&right_color=blue&left_text=Downloads sphinx-inlinecode Documentation Status

sphinx-inlinecode is a Sphinx extension that embeds source code blocks directly into your documentation as a dropdown.

Example

sphinx_inlinecode.get_code_block(qualname, obj, highlighter)[source]View on GitHub

Parses and highlights the source code lines of the provided object

Parameters:
  • qualname (str) – the fully qualified name of the object

  • obj (Any) – the actual object to retrieve the source code lines from

  • highlighter (PythonLexer) – the Pygments lexer to highlight the code block with

Returns:

the highlighted and fully formatted HTML codeblock to insert

Return type:

BeautifulSoup

Unlike sphinx.ext.viewcode, source code blocks will also be added for property and cached_property entries

Installation

To install sphinx-inlinecode via pip:

pip install sphinx-inlinecode

Configuration

Add the extension to your conf.py

extensions = [
    "sphinx_inlinecode",
]

sphinx_inlinecode package

sphinx_inlinecode.BAD_OBJTYPES = ('attribute', 'data', 'decorator')

Object types that can’t have code blocks inserted

sphinx_inlinecode.setup(app)[source]View on GitHub
Return type:

Dict[str, Any]

sphinx_inlinecode.add_static_path(app)[source]View on GitHub

Add the path for the _static folder

sphinx_inlinecode.add_source_code(app, exception)[source]View on GitHub

Inserts source code blocks into documentation entries.

sphinx_inlinecode.parse_py_domain(app)[source]View on GitHub

Parses all Python objects in the package from the BuildEnvironment

Returns:

a dictionary mapping fully qualified object names to the actual objects

Return type:

Dict[str, Any]

sphinx_inlinecode.add_code_blocks(file, objects, highlighter)[source]View on GitHub

Inserts source code blocks into the provided HTML file and writes the output.

Parameters:
  • file (Path) – path to the HTML file.

  • objects (Dict[str, Any]) – dictionary containing the objects in the package

  • highlighter (PythonLexer) – the Pygments lexer to highlight source code blocks with

sphinx_inlinecode.get_target(viewcode_link)[source]View on GitHub

Parses the fully qualified object name from the viewcode internal reference

Parameters:

viewcode_link (Tag) – the viewcode internal reference

Returns:

the fully qualified name of the referenced object

Return type:

str

sphinx_inlinecode.get_code_block(qualname, obj, highlighter)[source]View on GitHub

Parses and highlights the source code lines of the provided object

Parameters:
  • qualname (str) – the fully qualified name of the object

  • obj (Any) – the actual object to retrieve the source code lines from

  • highlighter (PythonLexer) – the Pygments lexer to highlight the code block with

Returns:

the highlighted and fully formatted HTML codeblock to insert

Return type:

BeautifulSoup

sphinx_inlinecode.wrap_code_block(code_block)[source]View on GitHub

Wraps the given code block inside a <details> HTML element

Parameters:

code_block (str) – HTML of the code block to wrap

Returns:

the wrapped code block

Return type:

BeautifulSoup

Indices and tables