Skip to content

Instantly share code, notes, and snippets.

@jlisee
Last active December 23, 2015 03:19
Show Gist options
  • Save jlisee/6572471 to your computer and use it in GitHub Desktop.
Save jlisee/6572471 to your computer and use it in GitHub Desktop.
Turn a reStructedTest into HTML using the docutils Python API.
#! /usr/bin/env python
# Author: Joseph Lisee <[email protected]>
__doc__ == """
Example of using docutils to process reStructedText into HTML.
Usage to file:
./rst2html_example.py README.rst -o README.html
To stdout:
./rst2html_example.py README.rst > README.html
"""
import argparse
import os
import sys
from docutils.core import publish_string
def main():
# Find the path to the input and output file
parser = argparse.ArgumentParser(description='reSructuredText to HTML')
parser.add_argument('input', metavar='I', type=str,
help='Input file to process')
parser.add_argument('-o','--output', default=None, help='Output file')
args = parser.parse_args()
# Load the input into a string
readme = file(args.input).read()
# Parse into HTML
html = publish_string(readme,writer_name='html')
# Output result
if args.output:
# Write to disk
with file(os.path.join(args.output), 'w') as f:
f.write(html)
else:
# Print to stdout
print(html)
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment