#!/usr/bin/env python import sys import argparse import tempfile import webbrowser import time from docutils.core import publish_string import locale try: locale.setlocale(locale.LC_ALL, '') except: pass def main(argv=None): parser = argparse.ArgumentParser(description= 'Render reStructuredText straight to the browser.' ) parser.add_argument('filename', type=str) args = parser.parse_args() source_file = open(args.filename) print 'Rendering and viewing', source_file.name with tempfile.NamedTemporaryFile(suffix='.html') as f: # Render the file html = publish_string(source=source_file.read(), writer_name='html') # Write output to file f.write(html) f.flush() fileurl = 'file://' + f.name # Open in the browser webbrowser.open(fileurl, new=2) # Wait 1 second for the browser to open it time.sleep(1) if __name__ == "__main__": sys.exit(main())