m2r2


Namem2r2 JSON
Version 0.3.3.post2 PyPI version JSON
download
home_pagehttps://github.com/crossnox/m2r2
SummaryMarkdown and reStructuredText in a single file.
upload_time2023-01-30 03:41:43
maintainerCrossNox
docs_urlNone
authorHiroyuki Takagi
requires_python>=3.7
licenseMIT
keywords markdown restructuredtext sphinx-extension
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            M2R2
====

[![PyPI](https://img.shields.io/pypi/v/m2r2.svg)](https://pypi.python.org/pypi/m2r2)
[![PyPI version](https://img.shields.io/pypi/pyversions/m2r2.svg)](https://pypi.python.org/pypi/m2r2)
[![Documentation](https://img.shields.io/badge/docs-latest-brightgreen.svg)](https://crossnox.github.io/m2r2)
![Python package](https://github.com/CrossNox/m2r2/workflows/Python%20package/badge.svg)

--------------------------------------------------------------------------------

M2R2 converts a markdown file including reStructuredText (rst) markups to a valid
rst format.

## M2R: the original
M2R2 is a fork of [m2r](https://github.com/miyakogi/m2r) which hasn't been updated for a long time
and there's been no response from the author about a PR fixing a serious issue that broke several
pipelines using `sphinx3`. Every `m2r` config should work out of the box. I've changed some of the tooling for what I'm mostly using now. Below goes
the original readme, changing only what's needed to work with `m2r2`.

## 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.

## 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](https://recommonmark.readthedocs.io/en/latest/index.html)
* 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 (``m2r_parse_relative_links``)
    * option to render ``mermaid`` blocks as graphs with [sphinxcontrib.mermaid](https://sphinxcontrib-mermaid-demo.readthedocs.io/en/latest/index.html`) (``m2r_use_mermaid``, default: auto)
      * auto means that m2r2 will check if `sphinxcontrib.mermaid` has been added to the extensions list
* Pure python implementation
    * pandoc is not required

## Installation

Python 2.7 or Python 3.4+ is required.

```
pip install m2r2
```

Or

```
python3 -m pip install m2r2
```

or using `conda`

```
conda install -c conda-forge m2r2
```

## Usage

### Command Line

`m2r2` command converts markdown file to rst format.

```
m2r2 your_document.md [your_document2.md ...]
```

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

### Programmatic Use

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

```python
from m2r2 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.

```python
from m2r2 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.

```python
readme_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'README.md')
try:
    from m2r2 import parse_from_file
    readme = parse_from_file(readme_file)
except ImportError:
    # m2r2 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.

```python
extensions = [
    ...,
    'm2r2',
]

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

Write index.md and run `make html`.

When `m2r2` extension is enabled on sphinx and `.md` file is loaded, m2r2
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](https://github.com/crossnox/m2r2/issues).

## Example

See [example document](https://crossnox.github.io/m2r2/example.html) and [its
source code](https://github.com/crossnox/m2r2/blob/master/docs/example.md).

*Note from the original author:* I'm using m2r for writing user guide of [WDOM](https://github.com/miyakogi/wdom).
So you can see it as another example. Its [HTML is
here](http://wdom-py.readthedocs.io/en/latest/guide/index.html), and [its
source code is here](https://github.com/miyakogi/wdom/tree/dev/docs/guide).

### Demo editor

*Note:* This hasn't received any updates.

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

[https://github.com/miyakogi/m2rdemo](https://github.com/miyakogi/m2rdemo)

## Dev install
Please install the `dev` dependencies and `pre-commit` hooks with:
```bash
pip install -r requirements-dev.txt
pre-commit install
```

## Acknowledgement

m2r is written as an extension of
[mistune](http://mistune.readthedocs.io/en/latest/), which is highly extensible
pure-python markdown parser.
Without the mistune, I couldn't write this. Thank you!

## Licence

[MIT](https://github.com/crossnox/m2r2/blob/master/LICENSE)



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/crossnox/m2r2",
    "name": "m2r2",
    "maintainer": "CrossNox",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "ijmermet+m2r2@gmail.com",
    "keywords": "Markdown reStructuredText sphinx-extension",
    "author": "Hiroyuki Takagi",
    "author_email": "miyako.dev@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/ee/09/fe73f1739c727ba119cbd67c43b65316af56e55f5501ba601c79dd7ba080/m2r2-0.3.3.post2.tar.gz",
    "platform": null,
    "description": "M2R2\n====\n\n[![PyPI](https://img.shields.io/pypi/v/m2r2.svg)](https://pypi.python.org/pypi/m2r2)\n[![PyPI version](https://img.shields.io/pypi/pyversions/m2r2.svg)](https://pypi.python.org/pypi/m2r2)\n[![Documentation](https://img.shields.io/badge/docs-latest-brightgreen.svg)](https://crossnox.github.io/m2r2)\n![Python package](https://github.com/CrossNox/m2r2/workflows/Python%20package/badge.svg)\n\n--------------------------------------------------------------------------------\n\nM2R2 converts a markdown file including reStructuredText (rst) markups to a valid\nrst format.\n\n## M2R: the original\nM2R2 is a fork of [m2r](https://github.com/miyakogi/m2r) which hasn't been updated for a long time\nand there's been no response from the author about a PR fixing a serious issue that broke several\npipelines using `sphinx3`. Every `m2r` config should work out of the box. I've changed some of the tooling for what I'm mostly using now. Below goes\nthe original readme, changing only what's needed to work with `m2r2`.\n\n## Why another converter?\n\nI wanted to write sphinx document in markdown, since it's widely used now and\neasy to write code blocks and lists. However, converters using pandoc or\nrecommonmark do not support many rst markups and sphinx extensions. For\nexample, rst's reference link like ``see `ref`_`` (this is very convenient in\nlong document in which same link appears multiple times) will be converted to\na code block in HTML like `see <code>ref</code>_`, which is not expected.\n\n## Features\n\n* Basic markdown and some extensions (see below)\n    * inline/block-level raw html\n    * fenced-code block\n    * tables\n    * footnotes (``[^1]``)\n* Inline- and Block-level rst markups\n    * single- and multi-line directives (`.. directive::`)\n    * inline-roles (``:code:`print(1)` ...``)\n    * ref-link (``see `ref`_``)\n    * footnotes (``[#fn]_``)\n    * math extension inspired by [recommonmark](https://recommonmark.readthedocs.io/en/latest/index.html)\n* Sphinx extension\n    * add markdown support for sphinx\n    * ``mdinclude`` directive to include markdown from md or rst files\n    * option to parse relative links into ref and doc directives (``m2r_parse_relative_links``)\n    * option to render ``mermaid`` blocks as graphs with [sphinxcontrib.mermaid](https://sphinxcontrib-mermaid-demo.readthedocs.io/en/latest/index.html`) (``m2r_use_mermaid``, default: auto)\n      * auto means that m2r2 will check if `sphinxcontrib.mermaid` has been added to the extensions list\n* Pure python implementation\n    * pandoc is not required\n\n## Installation\n\nPython 2.7 or Python 3.4+ is required.\n\n```\npip install m2r2\n```\n\nOr\n\n```\npython3 -m pip install m2r2\n```\n\nor using `conda`\n\n```\nconda install -c conda-forge m2r2\n```\n\n## Usage\n\n### Command Line\n\n`m2r2` command converts markdown file to rst format.\n\n```\nm2r2 your_document.md [your_document2.md ...]\n```\n\nThen you will find `your_document.rst` in the same directory.\n\n### Programmatic Use\n\nImport `m2r2.convert` function and call it with markdown text.\nThen it will return converted text.\n\n```python\nfrom m2r2 import convert\nrst = convert('# Title\\n\\nSentence.')\nprint(rst)\n# Title\n# =====\n#\n# Sentence.\n```\n\nOr, use `parse_from_file` function to load markdown file and obtain converted\ntext.\n\n```python\nfrom m2r2 import parse_from_file\noutput = parse_from_file('markdown_file.md')\n```\n\nThis is an example of setup.py to write README in markdown, and publish it to\nPyPI as rst format.\n\n```python\nreadme_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'README.md')\ntry:\n    from m2r2 import parse_from_file\n    readme = parse_from_file(readme_file)\nexcept ImportError:\n    # m2r2 may not be installed in user environment\n    with open(readme_file) as f:\n        readme = f.read()\nsetup(\n    ...,\n    long_description=readme,\n    ...,\n)\n```\n\n### Sphinx Integration\n\nIn your conf.py, add the following lines.\n\n```python\nextensions = [\n    ...,\n    'm2r2',\n]\n\n# source_suffix = '.rst'\nsource_suffix = ['.rst', '.md']\n```\n\nWrite index.md and run `make html`.\n\nWhen `m2r2` extension is enabled on sphinx and `.md` file is loaded, m2r2\nconverts to rst and pass to sphinx, not making new `.rst` file.\n\n#### mdinclude directive\n\nLike `.. include:: file` directive, `.. mdinclude:: file` directive inserts\nmarkdown file at the line.\n\nNote: do not use `.. include:: file` directive to include markdown file even if\nin the markdown file, please use `.. mdinclude:: file` instead.\n\n## Restrictions\n\n* In the rst's directives, markdown is not available. Please write in rst.\n* Column alignment of tables is not supported. (rst does not support this feature)\n* Heading with overline-and-underline is not supported.\n  * Heading with underline is OK\n* Rst heading marks are currently hard-coded and unchangeable.\n  * H1: `=`, H2: `-`, H3: `^`, H4: `~`, H5: `\"`, H6: `#`\n\nIf you find any bug or unexpected behaviour, please report it to\n[Issues](https://github.com/crossnox/m2r2/issues).\n\n## Example\n\nSee [example document](https://crossnox.github.io/m2r2/example.html) and [its\nsource code](https://github.com/crossnox/m2r2/blob/master/docs/example.md).\n\n*Note from the original author:* I'm using m2r for writing user guide of [WDOM](https://github.com/miyakogi/wdom).\nSo you can see it as another example. Its [HTML is\nhere](http://wdom-py.readthedocs.io/en/latest/guide/index.html), and [its\nsource code is here](https://github.com/miyakogi/wdom/tree/dev/docs/guide).\n\n### Demo editor\n\n*Note:* This hasn't received any updates.\n\nDemo editor of m2r is available.\nIf you are interested in m2r, please try it.\n\n[https://github.com/miyakogi/m2rdemo](https://github.com/miyakogi/m2rdemo)\n\n## Dev install\nPlease install the `dev` dependencies and `pre-commit` hooks with:\n```bash\npip install -r requirements-dev.txt\npre-commit install\n```\n\n## Acknowledgement\n\nm2r is written as an extension of\n[mistune](http://mistune.readthedocs.io/en/latest/), which is highly extensible\npure-python markdown parser.\nWithout the mistune, I couldn't write this. Thank you!\n\n## Licence\n\n[MIT](https://github.com/crossnox/m2r2/blob/master/LICENSE)\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Markdown and reStructuredText in a single file.",
    "version": "0.3.3.post2",
    "split_keywords": [
        "markdown",
        "restructuredtext",
        "sphinx-extension"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5757164112275965de407680ac176f96434d02329539438324c0ab8084df0343",
                "md5": "26f61282b7f85be523f878b1fc2b7740",
                "sha256": "86157721eb6eabcd54d4eea7195890cc58fa6188b8d0abea633383cfbb5e11e3"
            },
            "downloads": -1,
            "filename": "m2r2-0.3.3.post2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "26f61282b7f85be523f878b1fc2b7740",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 11207,
            "upload_time": "2023-01-30T03:41:42",
            "upload_time_iso_8601": "2023-01-30T03:41:42.176800Z",
            "url": "https://files.pythonhosted.org/packages/57/57/164112275965de407680ac176f96434d02329539438324c0ab8084df0343/m2r2-0.3.3.post2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ee09fe73f1739c727ba119cbd67c43b65316af56e55f5501ba601c79dd7ba080",
                "md5": "eac02f083a476b7b463fbd7520fefd17",
                "sha256": "e62bcb0e74b3ce19cda0737a0556b04cf4a43b785072fcef474558f2c1482ca8"
            },
            "downloads": -1,
            "filename": "m2r2-0.3.3.post2.tar.gz",
            "has_sig": false,
            "md5_digest": "eac02f083a476b7b463fbd7520fefd17",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 17005,
            "upload_time": "2023-01-30T03:41:43",
            "upload_time_iso_8601": "2023-01-30T03:41:43.850541Z",
            "url": "https://files.pythonhosted.org/packages/ee/09/fe73f1739c727ba119cbd67c43b65316af56e55f5501ba601c79dd7ba080/m2r2-0.3.3.post2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-01-30 03:41:43",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "crossnox",
    "github_project": "m2r2",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "lcname": "m2r2"
}
        
Elapsed time: 0.69426s