forked from github/markup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest2html
executable file
·124 lines (101 loc) · 3.68 KB
/
rest2html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python2 -S
"""
rest2html - A small wrapper file for parsing ReST files at GitHub.
Written in 2008 by Jannis Leidel <[email protected]>
Brandon Keepers <[email protected]>
Bryan Veloso <[email protected]>
Chris Wanstrath <[email protected]>
Dave Abrahams <[email protected]>
Garen Torikian <[email protected]>
Gasper Zejn <[email protected]>
Michael Jones <[email protected]>
Sam Whited <[email protected]>
Tyler Chung <[email protected]>
Vicent Marti <[email protected]>
To the extent possible under law, the author(s) have dedicated all copyright
and related and neighboring rights to this software to the public domain
worldwide. This software is distributed without any warranty.
You should have received a copy of the CC0 Public Domain Dedication along with
this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
"""
__author__ = "Jannis Leidel"
__license__ = "CC0"
__version__ = "0.1"
import sys
#fix docutils failing with unicode parameters to CSV-Tabl
#TODO: remove -S switch and the following 2 lines after switching system to python 3.
sys.setdefaultencoding('utf-8')
import site
try:
import locale
locale.setlocale(locale.LC_ALL, '')
except:
pass
import codecs
from docutils.core import publish_parts
from docutils.writers.html4css1 import Writer, HTMLTranslator
SETTINGS = {
'cloak_email_addresses': True,
'file_insertion_enabled': False,
'raw_enabled': False,
'strip_comments': True,
'doctitle_xform': False,
'report_level': 5,
'syntax_highlight' : 'none',
'math_output' : 'latex'
}
class GitHubHTMLTranslator(HTMLTranslator):
# removes the <div class="document"> tag wrapped around docs
# see also: http://bit.ly/1exfq2h (warning! sourceforge link.)
def depart_document(self, node):
HTMLTranslator.depart_document(self, node)
self.html_body.pop(0)
self.html_body.pop()
# technique for visiting sections, without generating additional divs
# see also: http://bit.ly/NHtyRx
def visit_section(self, node):
self.section_level += 1
def depart_section(self, node):
self.section_level -= 1
def visit_literal_block(self, node):
classes = node.attributes['classes']
if len(classes) >= 2 and classes[0] == 'code':
language = classes[1]
del classes[:]
self.body.append(self.starttag(node, 'pre', lang=language))
else:
self.body.append(self.starttag(node, 'pre'))
def visit_table(self, node):
classes = ' '.join(['docutils', self.settings.table_style]).strip()
self.body.append(
self.starttag(node, 'table', CLASS=classes))
def depart_table(self, node):
self.body.append('</table>\n')
def main():
"""
Parses the given ReST file or the redirected string input and returns the
HTML body.
Usage: rest2html < README.rst
rest2html README.rst
"""
try:
text = codecs.open(sys.argv[1], 'r', 'utf-8').read()
except IOError: # given filename could not be found
return ''
except IndexError: # no filename given
text = sys.stdin.read()
writer = Writer()
writer.translator_class = GitHubHTMLTranslator
parts = publish_parts(text, writer=writer, settings_overrides=SETTINGS)
if 'html_body' in parts:
html = parts['html_body']
# publish_parts() in python 2.x return dict values as Unicode type
# in py3k Unicode is unavailable and values are of str type
if isinstance(html, str):
return html
else:
return html.encode('utf-8')
return ''
if __name__ == '__main__':
sys.stdout.write("%s%s" % (main(), "\n"))
sys.stdout.flush()