Skip to main content

Convert Markdown to reStructuredText, m2rr.

Project description

PyPI PyPI version Documentation Build Status codecov

M2RR (R for Reborn) converts a markdown file including reStructuredText (rst) markups to a valid rst 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 rst markups and sphinx extensions. For example, rst’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.

Why fork

The original author seems to be no longer maintain the repository actively. Original Repo.

Features

  • Basic markdown and some extensions (see below)

    • inline/block-level raw html

    • fenced-code block

    • tables

    • footnotes ([^1])

  • Inline- and Block-level rst 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 rst files

    • option to parse relative links into ref and doc directives (m2rr_parse_relative_links)

  • Pure python implementation

    • pandoc is not required

Installation

Python 2.7 or Python 3.4+ is required.

pip install m2rr

Or,

python3 -m pip install m2rr

Usage

Command Line

m2rr command converts markdown file to rst format.

m2rr your_document.md [your_document2.md ...]

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

Programmatic Use

Import m2rr.convert function and call it with markdown text. Then it will return converted text.

from m2rr import convert
rst = convert('# Title\n\nSentence.')
print(rst)
# Title
# =====
#
# Sentence.

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

from m2rr import parse_from_file
output = parse_from_file('markdown_file.md')

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

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

Sphinx Integration

In your conf.py, add the following lines.

extensions = [
    ...,
    'm2rr',
]

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

Write index.md and run make html.

When m2rr extension is enabled on sphinx and .md file is loaded, m2rr converts to rst 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.

Restrictions

  • In the rst’s directives, markdown is not available. Please write in rst.

  • Column alignment of tables is not supported. (rst does not support this feature)

  • Heading with overline-and-underline is not supported.

    • Heading with underline is OK

  • Rst heading marks are currently hard-coded and unchangeable.

    • H1: =, H2: -, H3: ^, H4: ~, H5: ", H6: #

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

Example

See example document and its source code.

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

Demo editor

Demo editor of m2rr is available. If you are interested in m2rr, please try it.

https://github.com/miyakogi/m2rdemo

Acknowledgement

m2rr 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!

Licence

MIT

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

m2rr-0.2.3.tar.gz (16.4 kB view details)

Uploaded Source

Built Distribution

m2rr-0.2.3-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file m2rr-0.2.3.tar.gz.

File metadata

  • Download URL: m2rr-0.2.3.tar.gz
  • Upload date:
  • Size: 16.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.6

File hashes

Hashes for m2rr-0.2.3.tar.gz
Algorithm Hash digest
SHA256 0471e0aa689cccb930bef9271a9d3a01da3fdbfea2827612ecaf15237348e01e
MD5 e200ffd15d0d8fea2f8e99904e655e08
BLAKE2b-256 59915782966b17c5d0e6fb58f100424b0d9138bf1460c1e212a41f6e54f8198f

See more details on using hashes here.

File details

Details for the file m2rr-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: m2rr-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.6

File hashes

Hashes for m2rr-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 316fab11b551affec9ab1c66887ba2a7f6bf848fbb55dd70efed55a580bb81e0
MD5 8973bd6640118bc5935da7d881f266c3
BLAKE2b-256 8c80b4734d2fd3aad482c8a4a3422e92a57ebc0f54021f1014016729c4f5c0b3

See more details on using hashes here.

Supported by

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