-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.py
351 lines (308 loc) · 14 KB
/
writer.py
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# docutil imports
import docutils.writers.html5_polyglot
from docutils import nodes, frontend, io, utils
# local imports
from directives import explanation, hint, code
# python2-compatible code in the html-translator uses this function
def unicode(v):
return v
class Writer(docutils.writers.html5_polyglot.Writer):
'''
Standard docutils html5 writer with a custom html translator
'''
visitor_attributes = (docutils.writers.html5_polyglot.Writer.visitor_attributes[:2] +
('scripts',) +
docutils.writers.html5_polyglot.Writer.visitor_attributes[2:])
settings_spec = ('HTML-Specific Options', None,
docutils.writers.html5_polyglot.Writer.settings_spec[2] + (
('Embed the script(s) in the output HTML file.',
['--embed-script'],
{'default': 0,
'action': 'store_false',
'validator': frontend.validate_boolean}),
('Link to the scripts(s) in the output HTML file. '
'This is the default.',
['--link-script'],
{'dest': 'embed_script',
'action': 'store_true'}),
('Comma separated list of script paths. '
'Relative paths are expanded if a matching file is found '
'in the --script-dirs. With --link-script, the path is '
'rewritten relative to the output HTML file. ',
['--script-path'],
{'metavar': '<file[,file,...]>',
'validator': frontend.validate_comma_separated_list,
'default': ''}),
('Comma-separated list of directories where scripts are found. '
'Used by --script-path when expanding relative path arguments. ',
['--script-dirs'],
{'metavar': '<dir[,dir,...]>',
'validator': frontend.validate_comma_separated_list,
'default': ''}),
('Number steps within units',
['--step-unit-numbering'],
{'default': 0,
'action': 'store_true',
'validator': frontend.validate_boolean})
))
def __init__(self):
super().__init__()
self.translator_class = WebsheetHTMLTranslator
class WebsheetHTMLTranslator(docutils.writers.html5_polyglot.HTMLTranslator):
head_prefix_template = ('<html lang="%(lang)s">\n<head>\n')
content_type = ('<meta charset="%s">\n')
generator = ('<meta name="generator" content="rst2websheets '
'based on docutils %s">\n')
stylesheet_link = '<link rel="stylesheet" href="%s" type="text/css">\n'
script_link = '<script type="text/javascript" src="%s"></script>\n'
embedded_script = '<script type="text/javascript">\n%s\n</script>\n'
def __init__(self, document):
super().__init__(document)
# Retrieve list of script references from the settings object
scripts = document.settings.script_path or []
if not isinstance(scripts, list):
scripts = [path.strip() for path in scripts.split(',')]
self.scripts = [self.script_call(path) for path in scripts]
self.unit_counter = 0
self.step_counter = 0
def script_call(self, path):
"""Return code to reference or embed script file `path`"""
if self.settings.embed_script:
try:
content = io.FileInput(source_path=path, encoding='utf-8').read()
self.settings.record_dependencies.add(path)
except IOError as err:
msg = "Cannot embed script '%s': %s." % (
path, err.strerror)
self.document.reporter.error(msg)
return '<--- %s --->\n' % msg
return self.embedded_script % content
# else link to script file:
if self.settings.script_path:
# adapt path relative to output
path = utils.relative_path(self.settings._destination, path)
return self.script_link % self.encode(path)
def starttag(self, node, tagname, suffix='\n', empty=False, **attributes):
"""
Construct and return a start tag given a node (id & class attributes
are extracted), tag name, and optional attributes.
"""
tagname = tagname.lower()
prefix = []
atts = {}
ids = []
for (name, value) in attributes.items():
atts[name.lower()] = value
classes = []
languages = []
# unify class arguments and move language specification
for cls in node.get('classes', []) + atts.pop('class', '').split() :
if cls.startswith('language-'):
languages.append(cls[9:])
elif cls.strip() and cls not in classes:
classes.append(cls)
if languages:
# attribute name is 'lang' in XHTML 1.0 but 'xml:lang' in 1.1
atts[self.lang_attribute] = languages[0]
if classes:
atts['class'] = ' '.join(classes)
assert 'id' not in atts
ids.extend(node.get('ids', []))
if 'ids' in atts:
ids.extend(atts['ids'])
del atts['ids']
# Removed "if ids" statement that handles element id or id's.
# We don't want id's in the tags, we don't plan to format specific
# elements using CSS
# sorted used instead of original attlist.sort()
attlist = sorted(atts.items())
parts = [tagname]
for name, value in attlist:
# value=None is used for boolean attributes without value
if value is None:
parts.append('%s' % (name.lower()))
elif isinstance(value, list):
values = [v for v in value]
parts.append('%s="%s"' % (name.lower(),
self.attval(' '.join(values))))
else:
parts.append('%s="%s"' % (name.lower(),
self.attval(value)))
return ''.join(prefix) + '<%s>' % ' '.join(parts) + suffix
# visitor methods (for new types of nodes or overriding for existing ones)
def visit_admonition(self, node):
"""
# this doesn't work, apparently, they are all admonitions
# if isinstance(node, nodes.hint):
if 'hint' in node['classes']:
self.body.append(
self.starttag(node, 'details', ''))
close_tag = '</details>\n'
else:
# default behaviour
"""
node['classes'].insert(0, 'admonition')
self.body.append(self.starttag(node, 'div'))
# close_tag = '</div>\n'
# self.context.append(close_tag)
def depart_admonition(self, node=None):
self.body.append('</div>\n')
# self.body.append(self.context.pop())
def visit_code(self, node):
self.body.append(self.starttag(node, 'pre', '', CLASS='literal-block'))
self.body.append('<code>')
def depart_code(self, node):
self.body.append('</code>')
self.body.append('</pre>\n')
def visit_commentary(self, node):
if 'orphan' in node:
self.body.append(self.starttag(node, 'div',
Class='commentary',
Orphan=None))
else:
self.body.append(self.starttag(node, 'div',
Class='commentary'))
def depart_commentary(self, node):
self.body.append('</div>\n')
def visit_container(self, node):
# overriden because default container class is "docutils container"
self.body.append(self.starttag(node, 'div', CLASS='container'))
def depart_container(self, node):
self.body.append('</div>\n')
def visit_explanation(self, node):
if node.hasattr('open'):
self.body.append(self.starttag(node, 'details',
CLASS='explanation',
OPEN=None))
else:
self.body.append(self.starttag(node, 'details',
CLASS='explanation'))
def depart_explanation(self, node):
self.body.append('</details>\n')
def visit_group(self, node):
self.body.append(self.starttag(node, 'div', CLASS='group'))
def depart_group(self, node):
self.body.append('</div>\n')
def visit_hint(self, node):
args = {'CLASS': 'hint'}
if node.hasattr('open'): args['OPEN'] = None
if node.hasattr('solution'): args['SOLUTION'] = None
self.body.append(self.starttag(node, 'details', **args))
def depart_hint(self, node):
self.body.append('</details>\n')
# inline literal
def visit_literal(self, node):
# special case: "code" role
classes = node.get('classes', [])
if 'code' in classes:
# filter 'code' from class arguments
node['classes'] = [cls for cls in classes if cls != 'code']
self.body.append(self.starttag(node, 'code', ''))
return
self.body.append(
self.starttag(node, 'span', '', CLASS='literal'))
text = node.astext()
# remove hard line breaks (except if in a parsed-literal block)
if not isinstance(node.parent, nodes.literal_block):
text = text.replace('\n', ' ')
# Protect text like ``--an-option`` and the regular expression
# ``[+]?(\d+(\.\d*)?|\.\d+)`` from bad line wrapping
for token in self.words_and_spaces.findall(text):
if token.strip() and self.in_word_wrap_point.search(token):
self.body.append('<span class="pre">%s</span>'
% self.encode(token))
else:
self.body.append(self.encode(token))
self.body.append('</span>')
# Content already processed:
raise nodes.SkipNode
def depart_literal(self, node):
# skipped unless literal element is from "code" role:
self.body.append('</code>')
def visit_section(self, node):
self.section_level += 1
if self.section_level == 1:
self.unit_counter += 1
if self.settings.step_unit_numbering: self.step_counter = 0
self.body.append(
self.starttag(node, 'section',
CLASS='unit',
COUNTER=str(self.unit_counter)))
elif self.section_level == 2:
self.step_counter += 1
if self.settings.step_unit_numbering:
counter = str(self.unit_counter)+'.'+str(self.step_counter)
else:
counter = str(self.step_counter)
self.body.append(
self.starttag(node, 'section',
CLASS='step',
COUNTER=counter))
else: # this is where you can BAN deeper sections
self.body.append(
self.starttag(node, 'section'))
def depart_section(self, node):
self.section_level -= 1
self.body.append('</section>\n')
def visit_sidebar(self, node):
self.body.append(self.starttag(node, 'aside'))
def depart_sidebar(self, node):
self.body.append('</aside>\n')
def visit_title(self, node):
"""Only 6 section levels are supported by HTML."""
check_id = 0 # TODO: is this a bool (False) or a counter?
close_tag = '</p>\n'
if isinstance(node.parent, explanation):
self.body.append(self.starttag(node, 'summary', ''))
close_tag = '</summary>\n'
elif isinstance(node.parent, hint):
self.body.append(self.starttag(node, 'summary', ''))
close_tag = '</summary>\n'
elif isinstance(node.parent, nodes.topic):
self.body.append(
self.starttag(node, 'p', '', CLASS='topic-title'))
elif isinstance(node.parent, nodes.sidebar):
self.body.append(
self.starttag(node, 'p', '', CLASS='sidebar-title'))
elif isinstance(node.parent, nodes.Admonition):
self.body.append(
self.starttag(node, 'p', '', CLASS='admonition-title'))
elif isinstance(node.parent, nodes.table):
self.body.append(
self.starttag(node, 'caption', ''))
close_tag = '</caption>\n'
elif isinstance(node.parent, nodes.document):
self.body.append(self.starttag(node, 'h1', '', CLASS='title'))
close_tag = '</h1>\n'
self.in_document_title = len(self.body)
else:
assert isinstance(node.parent, nodes.section)
h_level = self.section_level + self.initial_header_level - 1
atts = {}
if (len(node.parent) >= 2 and
isinstance(node.parent[1], nodes.subtitle)):
atts['CLASS'] = 'with-subtitle'
self.body.append(
self.starttag(node, 'h%s' % h_level, '', **atts))
atts = {}
if node.hasattr('refid'):
atts['class'] = 'toc-backref'
atts['href'] = '#' + node['refid']
if atts:
self.body.append(self.starttag({}, 'a', '', **atts))
close_tag = '</a></h%s>\n' % (h_level)
else:
close_tag = '</h%s>\n' % (h_level)
self.context.append(close_tag)
def depart_title(self, node):
self.body.append(self.context.pop())
if self.in_document_title:
self.title = self.body[self.in_document_title:-1]
self.in_document_title = 0
self.body_pre_docinfo.extend(self.body)
self.html_title.extend(self.body)
del self.body[:]
def visit_transition(self, node):
self.body.append(self.emptytag(node, 'hr'))
def depart_transition(self, node):
pass