-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Copy pathrest2html
executable file
·320 lines (266 loc) · 11.2 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/env python
"""
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
import os
# This fixes docutils failing with unicode parameters to CSV-Table. The -S
# switch and the following 3 lines can be removed after upgrading to python 3.
if sys.version_info[0] < 3:
reload(sys)
sys.setdefaultencoding('utf-8')
import site
try:
import locale
locale.setlocale(locale.LC_ALL, '')
except:
pass
import codecs
import io
from docutils import nodes, utils
from docutils.parsers.rst import directives, roles
from docutils.parsers.rst.directives.body import CodeBlock, Directive
from docutils.core import publish_parts
from docutils.writers.html4css1 import Writer, HTMLTranslator
from docutils.parsers.rst.states import Body
from docutils import nodes
# By default, docutils provides two choices for unknown directives:
# - Show errors if the reporting level is high enough
# - Silently do not display them otherwise
# Comments are not displayed, either.
# This code monkey-patches docutils to show preformatted lines
# in both these cases.
# Recommended practice for repositories with rst files:
# - If github is the preferred viewer for the rst files (e.g. README.rst),
# then design the files to properly display for github. E.g., do not
# include unknown directives or restructuredText comments.
# - If github is NOT the preferred viewer for the rst files (e.g.
# the files are designed to be used with sphinx extensions at readthedocs)
# then include a restructuredText comment at the top of the file
# explaining that the document will display much more nicely with its
# preferred viewer, e.g. at readthedocs.
original_behavior = False # Documents original docutils behavior
github_display = True
def extract_extension_options(field_list, option_spec):
"""
Overrides `utils.extract_extension_options` and inlines
`utils.assemble_option_dict` to make it ignore unknown options passed to
directives (i.e. ``:caption:`` for ``.. code-block:``).
"""
dropped = set()
options = {}
for name, value in utils.extract_options(field_list):
convertor = option_spec.get(name)
if name in options or name in dropped:
raise utils.DuplicateOptionError('duplicate option "%s"' % name)
# silently drop unknown options as long as they are not duplicates
if convertor is None:
dropped.add(name)
continue
# continue as before
try:
options[name] = convertor(value)
except (ValueError, TypeError) as detail:
raise detail.__class__('(option: "%s"; value: %r)\n%s'
% (name, value, ' '.join(detail.args)))
return options
utils.extract_extension_options = extract_extension_options
def unknown_directive(self, type_name):
lineno = self.state_machine.abs_line_number()
indented, indent, offset, blank_finish = \
self.state_machine.get_first_known_indented(0, strip_indent=False)
text = '\n'.join(indented)
if original_behavior:
error = self.reporter.error(
'Unknown directive type "%s".' % type_name,
nodes.literal_block(text, text), line=lineno)
return [error], blank_finish
elif github_display:
cls = ['unknown_directive']
result = [nodes.literal_block(text, text, classes=cls)]
return result, blank_finish
else:
return [nodes.comment(text, text)], blank_finish
def comment(self, match):
if not match.string[match.end():].strip() \
and self.state_machine.is_next_line_blank(): # an empty comment?
return [nodes.comment()], 1 # "A tiny but practical wart."
indented, indent, offset, blank_finish = \
self.state_machine.get_first_known_indented(match.end())
while indented and not indented[-1].strip():
indented.trim_end()
if not original_behavior:
firstline = ''.join(indented[:1]).split()
if ' '.join(firstline[:2]) == 'github display':
if len(firstline) == 3 and firstline[2] in ('on', 'off'):
global github_display
github_display = firstline[2] == 'on'
if len(indented) == 1:
return [nodes.comment()], 1
text = '\n'.join(indented[1:])
cls = ['github_comment']
result = [nodes.literal_block(text, text, classes=cls)]
return result, blank_finish
text = '\n'.join(indented)
return [nodes.comment(text, text)], blank_finish
Body.comment = comment
Body.unknown_directive = unknown_directive
SETTINGS = {
'cloak_email_addresses': False,
'file_insertion_enabled': False,
'raw_enabled': True,
'strip_comments': True,
'doctitle_xform': True,
'initial_header_level': 2,
'report_level': 5,
'syntax_highlight': 'none',
'math_output': 'latex',
'field_name_limit': 50,
}
default_highlight_language = None
class HighlightDirective(Directive):
required_arguments = 1
optional_arguments = 1
option_spec = {}
def run(self):
"""Track the default syntax highlighting language
"""
global default_highlight_language
default_highlight_language = self.arguments[0]
return []
class DoctestDirective(CodeBlock):
"""Render Sphinx 'doctest:: [group]' blocks as 'code:: python'
"""
def run(self):
"""Discard any doctest group argument, render contents as python code
"""
self.arguments = ['python']
return super(DoctestDirective, self).run()
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) # pop the starting <div> off
self.html_body.pop() # pop the ending </div> off
# technique for visiting sections, without generating additional divs
# see also: http://bit.ly/NHtyRx
# the a is to support ::contents with ::sectnums: http://git.io/N1yC
def visit_section(self, node):
for id_attribute in node.attributes['ids']:
self.body.append('<a name="%s"></a>\n' % id_attribute)
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))
elif default_highlight_language is not None:
self.body.append(self.starttag(node, 'pre', lang=default_highlight_language))
else:
self.body.append(self.starttag(node, 'pre'))
def visit_doctest_block(self, node):
self.body.append(self.starttag(node, 'pre', lang='pycon'))
# always wrap two-backtick rst inline literals in <code>, not <tt>
# this also avoids the generation of superfluous <span> tags
def visit_literal(self, node):
self.body.append(self.starttag(node, 'code', suffix=''))
def depart_literal(self, node):
self.body.append('</code>')
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 depart_image(self, node):
uri = node['uri']
ext = os.path.splitext(uri)[1].lower()
# we need to swap RST's use of `object` with `img` tags
# see http://git.io/5me3dA
if ext == ".svg":
# preserve essential attributes
atts = {}
for attribute, value in node.attributes.items():
# we have no time for empty values
if value:
if attribute == "uri":
atts['src'] = value
else:
atts[attribute] = value
# toss off `object` tag
self.body.pop()
# add on `img` with attributes
self.body.append(self.starttag(node, 'img', **atts))
HTMLTranslator.depart_image(self, node)
# treat images held in a reference on the base document as inlined
# images; this is to help avoid rendering a reference's decorative
# line for the spacing after an image
if (isinstance(node.parent, nodes.reference)
and isinstance(node.parent.parent, nodes.document)):
self.body.append(self.body.pop().rstrip('\n'))
def kbd(name, rawtext, text, lineno, inliner, options={}, content=[]):
return [nodes.raw('', '<kbd>%s</kbd>' % text, format='html')], []
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
if sys.version_info[0] < 3: # python 2.x
text = sys.stdin.read()
else: # python 3
input_stream = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8')
text = input_stream.read()
writer = Writer()
writer.translator_class = GitHubHTMLTranslator
roles.register_canonical_role('kbd', kbd)
# Render source code in Sphinx doctest blocks
directives.register_directive('doctest', DoctestDirective)
# Set default highlight language
directives.register_directive('highlight', HighlightDirective)
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__':
if sys.version_info[0] < 3: # python 2.x
sys.stdout.write("%s%s" % (main(), "\n"))
else: # python 3
output_stream = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
output_stream.write("%s%s" % (main(), "\n"))
sys.stdout.flush()