This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

Markdown to reStructuredText converter

Project Description

M2R converts a markdown file including reST markups to a valid reST format.

Why another converter?

I wanted to write sphinx document in markdown, since it’s widely used now and easy to write code blocks and lists. However, converters using pandoc or recommonmark do not support many reST markups and sphinx extensions. For example, reST’s reference link like see `ref`_ (this is very convenient in long document in which same link appears multiple times) will be converted to a code block in HTML like see <code>ref</code>_, which is not expected.


  • Basic markdown and some extensions (see below)
    • inline/block-level raw html
    • fenced-code block
    • tables
    • footnotes ([^1])
  • Inline- and Block-level reST markups
    • single- and multi-line directives (.. directive::)
    • inline-roles (:code:`print(1)` ...)
    • ref-link (see `ref`_)
    • footnotes ([#fn]_)
    • math extension inspired by recommonmark
  • Sphinx extension
    • add markdown support for sphinx
    • mdinclude directive to include markdown from md or reST files


Python 2.7 or Python 3.4+ is required.

pip install m2r


Command Line

m2r command converts markdown file to reST format.

m2r [ ...]

Then you will find your_document.rst in the same directory.

Programmatic Use

Instantiate M2R class and call with markdown text. Then it will return converted text.

from m2r import M2R
m2r = M2R()
rest = m2r('# Title\n\nSentence.')
# Title
# =====
# Sentence.

Or, use parse_from_file function to load markdown file and obtain converted text.

from m2r import parse_from_file
output = parse_from_file('')

This is an example of to write README in markdown, and publish it to PyPI as reST format.

readme_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), '')
    from m2r import parse_from_file
    readme = parse_from_file(readme_file)
except ImportError:
    # m2r may not be installed in user environment
    with open(readme_file) as f:
        readme =

Sphinx Integration

In your, add the following lines.

extensions = [

# source_suffix = '.rst'
source_suffix = ['.rst', '.md']

Write and run make html.

When m2r extension is enabled on sphinx and .md file is loaded, m2r converts to reST and pass to sphinx, not making new .rst file.

mdinclude directive

Like .. include:: file directive, .. mdinclude:: file directive inserts markdown file at the line.

Note: do not use .. include:: file directive to include markdown file even if in the markdown file, please use .. mdinclude:: file instead.


  • In the reST’s directives, markdown is not available. Please write in reST.
  • Column alignment of tables is not supported. (reST does not support this feature)
  • Heading with overline-and-underline is not supported.
    • Heading with underline is OK

If you find any bug or unexpected behaviour, please report it to Issues.


See example document and its source code.

I’m using m2r for writing user guide of WDOM. So you can see it as another example. Its HTML is here, and its source code is here.


m2r is written as an extension of mistune, which is highly extensible pure-python markdown parser. Without the mistune, I couldn’t write this. Thank you!



Release History

Release History

This version
History Node


History Node


History Node


History Node


History Node


History Node


History Node


Download Files

Download Files

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

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
m2r-0.1.6.tar.gz (13.6 kB) Copy SHA256 Checksum SHA256 Source Jun 3, 2017

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting