Skip to content

Instantly share code, notes, and snippets.

@bebraw
Created November 12, 2011 11:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bebraw/1360404 to your computer and use it in GitHub Desktop.
Save bebraw/1360404 to your computer and use it in GitHub Desktop.
import subprocess
import sys
def convert(source, from_format, to_format):
# original version: http://osiux.com/html-to-restructured-text-in-python-using-pandoc
# supported formats at http://johnmacfarlane.net/pandoc/
# raises OSError if pandoc is not found!
p = subprocess.Popen(['pandoc', '--from=' + from_format, '--to=' + to_format],
stdin=subprocess.PIPE, stdout=subprocess.PIPE
)
if sys.version_info[0] == 3:
return p.communicate(bytes(source, 'UTF-8'))[0]
return p.communicate(source)[0]
# example
readme = open('README.md').read() # might want to use "with" to make sure it gets closed
output = convert(readme, 'markdown', 'rst')
@fred6
Copy link

fred6 commented Jul 24, 2012

This doesn't quite work in python 3.2 for me. I had to cast 'source' to bytes:

return p.communicate(bytes(source, 'UTF-8'))[0]

@bebraw
Copy link
Author

bebraw commented Jul 26, 2012

@fred6: I updated the Gist. Thanks! :)

@ssbarnea
Copy link

I think we need to reopen this. It doesn't seem to install properly, see https://travis-ci.org/pycontribs/tendo/jobs/47505164

@mw3i
Copy link

mw3i commented Jan 5, 2022

thanks for this!! this is perfect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment