Skip to main content

Generate Markdown documentation from Python code

Project description

Generate Markdown documentation from Python code

This library generates Markdown documentation directly from Python code, utilizing Python type annotations.

Features at a glance

  • Each module produces a Markdown file.
  • Documentation strings are extracted from module, class, enumeration and function definitions.
  • Cross-references may be local or fully qualified, and work across modules.
  • Classes with member variable declarations produce member documentation.
  • Data-class field descriptions are validated if they have a matching member variable declaration.
  • Enumeration members are published, even if they lack a description.
  • Magic methods (e.g. __eq__) are published if they have a doc-string.
  • Multi-line code blocks in doc-strings are retained as Markdown code blocks.
  • Forward-references and type annotations as strings are automatically evaluated.

Documentation features

Cross-references with Sphinx-style syntax are supported in module, class and function doc-strings:

@dataclass
class SampleClass:
    """
    This class is extended by :class:`DerivedClass`.

    This class implements :meth:`__lt__` and :meth:`SampleClass.__gt__`.
    """

The following Sphinx-style cross-references are recognized:

  • :mod: for a module
  • :class: for a regular class
  • :exc: for an exception class
  • :deco: for a decorator function
  • :func: for a function defined at the module level
  • :meth: for a method of a class

Class member variable and data-class field descriptions are defined with :param ...::

@dataclass
class DerivedClass(SampleClass):
    """
    This data-class derives from another base class.

    :param union: A union of several types.
    :param json: A complex type with type substitution.
    :param schema: A complex type without type substitution.
    """

    union: SimpleType
    json: JsonType
    schema: Schema

Enumeration member description follows the member value assignment:

class EnumType(enum.Enum):
    enabled = "enabled"
    "Documents the enumeration member `enabled`."

    disabled = "disabled"
    "Documents the enumeration member `disabled`."

Usage

Calling the utility in Python

from markdown_doc.generator import MarkdownGenerator

MarkdownGenerator([module1, module2, module3]).generate(out_dir)

Pass an object of MarkdownOptions to configure behavior:

MarkdownGenerator(
    [module1, module2, module3],
    options=MarkdownOptions(
        anchor_style=MarkdownAnchorStyle.GITBOOK,
        partition_strategy=PartitionStrategy.SINGLE,
        include_private=False,
        stdlib_links=True,
    ),
).generate(out_dir)

Running the utility from the command line

$ python3 -m markdown_doc --help
usage: markdown_doc [-h] [-d [DIRECTORY ...]] [-m [MODULE ...]] [-r ROOT_DIR] [-o OUT_DIR] [--anchor-style {GitBook,GitHub}] [--partition {single,by_kind}]

Generates Markdown documentation from Python code

options:
  -h, --help            show this help message and exit
  -d [DIRECTORY ...], --directory [DIRECTORY ...]
                        folder(s) to recurse into when looking for modules
  -m [MODULE ...], --module [MODULE ...]
                        qualified names(s) of Python module(s) to scan
  -r ROOT_DIR, --root-dir ROOT_DIR
                        path to act as root for converting directory paths into qualified module names (default: working directory)
  -o OUT_DIR, --out-dir OUT_DIR
                        output directory (default: 'docs' in working directory)
  --anchor-style {GitBook,GitHub}
                        output format for generating anchors in headings
  --partition {single,by_kind}
                        how to split module contents across Markdown files

Related work

In order to reduce added complexity, this library does not use the Sphinx framework with autodoc.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

markdown_doc-0.1.8.tar.gz (20.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

markdown_doc-0.1.8-py3-none-any.whl (21.5 kB view details)

Uploaded Python 3

File details

Details for the file markdown_doc-0.1.8.tar.gz.

File metadata

  • Download URL: markdown_doc-0.1.8.tar.gz
  • Upload date:
  • Size: 20.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for markdown_doc-0.1.8.tar.gz
Algorithm Hash digest
SHA256 993886298dd01d8afb22496e76ee6eddc0326f790c98b5e04f0a26caa1a1cb52
MD5 220ae1962921175feb1f5cb6ff767ee8
BLAKE2b-256 c584203f72c97e6a23de92db53e035e0079134c8ee5d335cf93e2de735227bfc

See more details on using hashes here.

File details

Details for the file markdown_doc-0.1.8-py3-none-any.whl.

File metadata

  • Download URL: markdown_doc-0.1.8-py3-none-any.whl
  • Upload date:
  • Size: 21.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for markdown_doc-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 0177fc8181d8a858c4e9719737603b27e0bfe67d1b0db5daaebaf3cff7e991c3
MD5 a9ef60e2b8811b2ad620868ada6980f6
BLAKE2b-256 88ba72b47b7602f614a9ff2bd4c96d2949de35285284aec4d75a39551cabcc09

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page