m2r2: Markdown and reStructuredText Converter

0.3.4 · active · verified Thu Apr 16

M2R2 is a Python library designed to convert Markdown files, including embedded reStructuredText (RST) markups, into a valid RST format. It is a actively maintained fork of the original `m2r` library, specifically addressing compatibility issues with Sphinx 3+ and newer Python versions. M2R2 also functions as a Sphinx extension, enabling developers to write documentation in Markdown directly within Sphinx projects. The current version is 0.3.4, with a focused release cadence primarily for maintenance and compatibility updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically convert a Markdown string to reStructuredText using the `convert` function. It also highlights the command-line utility and how to integrate `m2r2` as a Sphinx extension by adding 'm2r2' to your `conf.py` extensions list, allowing Sphinx to process `.md` files directly.

from m2r2 import convert
import os

# Programmatic conversion of a Markdown string
markdown_text = """# My Document\n\nThis is **Markdown** with `inline code` and also `reST :role:` content.\n\n.. admonition:: Note\n   This is reStructuredText inside Markdown.\n"""
rst_output = convert(markdown_text)
print("--- Converted RST ---")
print(rst_output)

# Command-line usage (assuming 'my_document.md' exists)
# You can also run 'm2r2 my_document.md' from your terminal
# It will create 'my_document.rst'

# For Sphinx integration, add 'm2r2' to extensions in conf.py:
# extensions = [
#     'sphinx.ext.autodoc',
#     'm2r2',
# ]
# Then Sphinx will process .md files.

view raw JSON →