Docstring to Markdown Converter

0.17 · active · verified Sat Apr 11

The `docstring-to-markdown` library provides on-the-fly conversion of Python docstrings into Markdown format. It currently supports reStructuredText and has initial support for Google-formatted docstrings since version 0.13. The current version is 0.17, and it is actively maintained with a regular release cadence as evidenced by its release history on PyPI.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `docstring_to_markdown.convert` to transform both reStructuredText and Google-style Python docstrings into Markdown. It also shows how to handle `UnknownFormatError` for unrecognised input.

import docstring_to_markdown

def my_function_rst(param1, param2):
    """
    My function in reStructuredText format.

    :param param1: The first parameter.
    :type param1: str
    :param param2: The second parameter.
    :type param2: int
    :returns: A concatenated string.
    :rtype: str

    .. code-block:: python

        print('Example usage')

    """
    return f"{param1}-{param2}"

def my_function_google(param1, param2):
    """
    My function in Google style format.

    Args:
        param1 (str): The first parameter.
        param2 (int): The second parameter.

    Returns:
        str: A concatenated string.

    Example:
        >>> my_function_google('hello', 123)
        'hello-123'
    """
    return f"{param1}-{param2}"


# Convert a reStructuredText docstring
python_code = my_function_rst.__doc__
markdown_output = docstring_to_markdown.convert(python_code)
print(f"ReStructuredText to Markdown:\n{markdown_output}\n")

# Convert a Google-style docstring
python_code_google = my_function_google.__doc__
markdown_output_google = docstring_to_markdown.convert(python_code_google)
print(f"Google style to Markdown:\n{markdown_output_google}")

# Example of an unrecognised format raising an error
try:
    docstring_to_markdown.convert('This is a plain docstring without a recognized format.')
except docstring_to_markdown.UnknownFormatError:
    print("Successfully caught UnknownFormatError for unrecognized format.")

view raw JSON →