all repos — markup @ b7cabb1c81ea30035dd63a508a67a949c1846f4c

The code we use to render README.your_favorite_markup

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
17import codecs
18
19from docutils.core import publish_parts
20from docutils.writers.html4css1 import Writer, HTMLTranslator
21
22SETTINGS = {
23    'cloak_email_addresses': True,
24    'file_insertion_enabled': False,
25    'raw_enabled': False,
26    'strip_comments': True,
27    'doctitle_xform': False,
28    'report_level': 5,
29    'syntax_highlight' : 'none',
30    'math_output' : 'latex'
31}
32
33class GitHubHTMLTranslator(HTMLTranslator):
34    def visit_literal_block(self, node):
35        classes = node.attributes['classes']
36        if len(classes) >= 2 and classes[0] == 'code':
37            language = classes[1]
38            del classes[:]
39            self.body.append(self.starttag(node, 'pre', lang=language))
40        else:
41            self.body.append(self.starttag(node, 'pre'))
42
43    def visit_table(self, node):
44        classes = ' '.join(['docutils', self.settings.table_style]).strip()
45        self.body.append(
46                self.starttag(node, 'table', CLASS=classes))
47
48    def depart_table(self, node):
49        self.body.append('</table>\n')
50
51def main():
52    """
53    Parses the given ReST file or the redirected string input and returns the
54    HTML body.
55
56    Usage: rest2html < README.rst
57           rest2html README.rst
58    """
59    try:
60        text = codecs.open(sys.argv[1], 'r', 'utf-8').read()
61    except IOError: # given filename could not be found
62        return ''
63    except IndexError: # no filename given
64        text = sys.stdin.read()
65
66    writer = Writer()
67    writer.translator_class = GitHubHTMLTranslator
68
69    parts = publish_parts(text, writer=writer, settings_overrides=SETTINGS)
70    if 'html_body' in parts:
71        html = parts['html_body']
72
73        # publish_parts() in python 2.x return dict values as Unicode type
74        # in py3k Unicode is unavailable and values are of str type
75        if isinstance(html, str):
76            return html
77        else:
78            return html.encode('utf-8')
79    return ''
80
81if __name__ == '__main__':
82    sys.stdout.write("%s%s" % (main(), "\n"))
83    sys.stdout.flush()