# Have the same README both in Markdown and reStructuredText

Each Answer to this Q is separated by one/two green lines.

I have a project hosted on GitHub. For this I have written my README using the Markdown syntax in order to have it nicely formatted on GitHub.

As my project is in Python I also plan to upload it to PyPi. The syntax used for READMEs on PyPi is reStructuredText.

I would like to avoid having to handle two READMEs containing roughly the same content; so I searched for a markdown to RST (or the other way around) translator, but couldn’t find any.

The other solution I see is to perform a markdown/HTML and then a HTML/RST translation. I found some ressources for this here and here so I guess it should be possible.

Would you have any idea that could fit better with what I want to do?

I would recommend Pandoc, the “swiss-army knife for converting files from one markup format into another” (check out the diagram of supported conversions at the bottom of the page, it is quite impressive). Pandoc allows markdown to reStructuredText translation directly. There is also an online editor here which lets you try it out, so you could simply use the online editor to convert your README files.

As @Chris suggested, you can use Pandoc to convert Markdown to RST. This can be simply automated using pypandoc module and some magic in setup.py:

from setuptools import setup
try:
from pypandoc import convert
read_md = lambda f: convert(f, 'rst')
except ImportError:

setup(
# name, version, ...
install_requires=[]
)


This will automatically convert README.md to RST for the long description using on PyPi. When pypandoc is not available, then it just reads README.md without the conversion – to not force others to install pypandoc when they wanna just build the module, not upload to PyPi.

So you can write in Markdown as usual and don’t care about RST mess anymore. 😉

# 2019 Update

The PyPI Warehouse now supports rendering Markdown as well! You just need to update your package configuration and add the long_description_content_type="text/markdown" to it. e.g.:

setup(
name="an_example_package",
# other arguments omitted
long_description=long_description,
long_description_content_type="text/markdown"
)


Therefore, there is no need to keep the README in two formats any longer.

The Markup library used by GitHub supports reStructuredText. This means you can write a README.rst file.

They even support syntax specific color highlighting using the code and code-block directives (Example)

PyPI now supports Markdown for long descriptions!

In setup.py, set long_description to a Markdown string, add long_description_content_type="text/markdown" and make sure you’re using recent tooling (setuptools 38.6.0+, twine 1.11+).

See Dustin Ingram’s blog post for more details.

You might also be interested in the fact that it is possible to write in a common subset so that your document comes out the same way when rendered as markdown or rendered as reStructuredText: https://gist.github.com/dupuy/1855764

For my requirements I didn’t want to install Pandoc in my computer. I used docverter. Docverter is a document conversion server with an HTTP interface using Pandoc for this.

import requests
r = requests.post(url="http://c.docverter.com/convert",
data={'to':'rst','from':'markdown'},
if r.ok:
print r.content


I ran into this problem and solved it with the two following bash scripts.

Note that I have LaTeX bundled into my Markdown.

#!/usr/bin/env bash

if [ $# -lt 1 ]; then echo "$0 file.md"
exit;
fi

filename=$(basename "$1")
extension="${filename##*.}" filename="${filename%.*}"

if [ "$extension" = "md" ]; then rst=".rst" pandoc$1 -o $filename$rst
fi


Its also useful to convert to html. md2html:

#!/usr/bin/env bash

if [ $# -lt 1 ]; then echo "$0 file.md <style.css>"
exit;
fi

filename=$(basename "$1")
extension="${filename##*.}" filename="${filename%.*}"

if [ "$extension" = "md" ]; then html=".html" if [ -z$2 ]; then
# if no css
pandoc -s -S --mathjax --highlight-style pygments $1 -o$filename$html else pandoc -s -S --mathjax --highlight-style pygments -c$2 $1 -o$filename$html fi fi  I hope that helps Using the pandoc tool suggested by others I created a md2rst utility to create the rst files. Even though this solution means you have both an md and an rst it seemed to be the least invasive and would allow for whatever future markdown support is added. I prefer it over altering setup.py and maybe you would as well: #!/usr/bin/env python ''' Recursively and destructively creates a .rst file for all Markdown files in the target directory and below. Created to deal with PyPa without changing anything in setup based on the idea that getting proper Markdown support later is worth waiting for rather than forcing a pandoc dependency in sample packages and such. Vote for (https://bitbucket.org/pypa/pypi/issue/148/support-markdown-for-readmes) ''' import sys, os, re markdown_sufs = ('.md','.markdown','.mkd') markdown_regx = '\.(md|markdown|mkd)$'

target="."
if len(sys.argv) >= 2: target = sys.argv[1]

md_files = []
for root, dirnames, filenames in os.walk(target):
for name in filenames:
if name.endswith(markdown_sufs):
md_files.append(os.path.join(root, name))

for md in md_files:
bare = re.sub(markdown_regx,'',md)
cmd='pandoc --from=markdown --to=rst "{}" -o "{}.rst"'
print(cmd.format(md,bare))
os.system(cmd.format(md,bare))


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .