lib/github/commands/rest2html (view raw)
1#!/usr/bin/env python
2
3"""A small wrapper file for parsing ReST files at GitHub."""
4
5__author__ = "Jannis Leidel"
6__copyright__ = "Copyright (C) 2008 Jannis Leidel"
7__license__ = "Public Domain"
8__version__ = "0.1"
9
10try:
11 import locale
12 locale.setlocale(locale.LC_ALL, '')
13except:
14 pass
15
16import sys
17from docutils.core import publish_parts
18from docutils.writers.html4css1 import Writer
19
20SETTINGS = {
21 'cloak_email_addresses': True,
22 'file_insertion_enabled': False,
23 'raw_enabled': False,
24 'strip_comments': True,
25}
26
27def main():
28 """
29 Parses the given ReST file or the redirected string input and returns the
30 HTML body.
31
32 Usage: rest2html < README.rst
33 rest2html README.rst
34 """
35 try:
36 text = open(sys.argv[1], 'r').read()
37 except IOError: # given filename could not be found
38 return ''
39 except IndexError: # no filename given
40 text = sys.stdin.read()
41
42 parts = publish_parts(text, writer=Writer(), settings_overrides=SETTINGS)
43 if 'html_body' in parts:
44 return parts['html_body']
45 return ''
46
47if __name__ == '__main__':
48 print main()