Skip to content

Instantly share code, notes, and snippets.

@dokterbob
Created September 4, 2013 13:18
Show Gist options
  • Save dokterbob/6436801 to your computer and use it in GitHub Desktop.
Save dokterbob/6436801 to your computer and use it in GitHub Desktop.

Revisions

  1. dokterbob created this gist Sep 4, 2013.
    47 changes: 47 additions & 0 deletions rst2browser.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    #!/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())