| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
""" |
| 7 |
Simple HyperText Markup Language document tree Writer. |
| 8 |
|
| 9 |
The output conforms to the XHTML version 1.0 Transitional DTD |
| 10 |
(*almost* strict). The output contains a minimum of formatting |
| 11 |
information. The cascading style sheet "html4css1.css" is required |
| 12 |
for proper viewing with a modern graphical browser. |
| 13 |
""" |
| 14 |
|
| 15 |
from __future__ import annotations |
| 16 |
|
| 17 |
__docformat__ = 'reStructuredText' |
| 18 |
|
| 19 |
import os.path |
| 20 |
import re |
| 21 |
|
| •22 |
import docutils |
| 23 |
from docutils import frontend, nodes, writers |
| 24 |
from docutils.writers import _html_base |
| 25 |
from docutils.writers._html_base import PIL |
| 26 |
|
| 27 |
|
| 28 |
class Writer(writers._html_base.Writer): |
| 29 |
|
| 30 |
supported = ('html', 'html4', 'html4css1', 'xhtml', 'xhtml10') |
| 31 |
"""Formats this writer supports.""" |
| 32 |
|
| 33 |
default_stylesheets = ['html4css1.css'] |
| 34 |
default_stylesheet_dirs = ['.', |
| •35 |
docutils._datadir(os.path.abspath(__file__)), |
| 36 |
os.path.join( |
| 37 |
docutils._datadir(os.path.dirname(os.path.abspath(__file__))), |
| 38 |
'html5_polyglot') |
| 39 |
] |
| 40 |
default_template = os.path.join( |
| •41 |
docutils._datadir(os.path.abspath(__file__)), 'template.txt') |
| 42 |
|
| 43 |
|
| 44 |
settings_spec = frontend.filter_settings_spec( |
| 45 |
writers._html_base.Writer.settings_spec, |
| 46 |
template=( |
| 47 |
'Template file. (UTF-8 encoded, default: "%s")' % default_template, |
| 48 |
['--template'], |
| 49 |
{'default': default_template, 'metavar': '<file>'}), |
| 50 |
stylesheet_path=( |
| 51 |
'Comma separated list of stylesheet paths. ' |
| 52 |
'Relative paths are expanded if a matching file is found in ' |
| 53 |
'the --stylesheet-dirs. With --link-stylesheet, ' |
| 54 |
'the path is rewritten relative to the output HTML file. ' |
| 55 |
'(default: "%s")' % ','.join(default_stylesheets), |
| 56 |
['--stylesheet-path'], |
| 57 |
{'metavar': '<file[,file,...]>', 'overrides': 'stylesheet', |
| 58 |
'validator': frontend.validate_comma_separated_list, |
| 59 |
'default': default_stylesheets}), |
| 60 |
stylesheet_dirs=( |
| 61 |
'Comma-separated list of directories where stylesheets are found. ' |
| 62 |
'Used by --stylesheet-path when expanding relative path ' |
| 63 |
'arguments. (default: "%s")' % ','.join(default_stylesheet_dirs), |
| 64 |
['--stylesheet-dirs'], |
| 65 |
{'metavar': '<dir[,dir,...]>', |
| 66 |
'validator': frontend.validate_comma_separated_list, |
| 67 |
'default': default_stylesheet_dirs}), |
| 68 |
initial_header_level=( |
| 69 |
'Specify the initial header level. Does not affect document ' |
| 70 |
'title & subtitle (see --no-doc-title). (default: 1 for "<h1>")', |
| 71 |
['--initial-header-level'], |
| 72 |
{'choices': '1 2 3 4 5 6 auto'.split(), 'default': '1', |
| 73 |
'metavar': '<level>'}), |
| 74 |
math_output=( |
| 75 |
'Math output format (one of "MathML", "HTML", "MathJax", or ' |
| 76 |
'"LaTeX") and option(s). (default: "HTML math.css")', |
| 77 |
['--math-output'], |
| 78 |
{'metavar': '<format [option(s)]>', 'default': 'HTML math.css', |
| 79 |
'validator': frontend.validate_math_output}), |
| 80 |
xml_declaration=( |
| 81 |
'Prepend an XML declaration (default). ', |
| 82 |
['--xml-declaration'], |
| 83 |
{'default': True, 'action': 'store_true', |
| 84 |
'validator': frontend.validate_boolean}), |
| 85 |
) |
| 86 |
settings_spec = settings_spec + ( |
| 87 |
'HTML4 Writer Options', |
| 88 |
'', |
| 89 |
(('Specify the maximum width (in characters) for one-column field ' |
| 90 |
'names. Longer field names will span an entire row of the table ' |
| 91 |
'used to render the field list. Default is 14 characters. ' |
| 92 |
'Use 0 for "no limit".', |
| 93 |
['--field-name-limit'], |
| 94 |
{'default': 14, 'metavar': '<level>', |
| 95 |
'validator': frontend.validate_nonnegative_int}), |
| 96 |
('Specify the maximum width (in characters) for options in option ' |
| 97 |
'lists. Longer options will span an entire row of the table used ' |
| 98 |
'to render the option list. Default is 14 characters. ' |
| 99 |
'Use 0 for "no limit".', |
| 100 |
['--option-limit'], |
| 101 |
{'default': 14, 'metavar': '<level>', |
| 102 |
'validator': frontend.validate_nonnegative_int}), |
| 103 |
) |
| 104 |
) |
| 105 |
|
| 106 |
config_section = 'html4css1 writer' |
| 107 |
|
| 108 |
def __init__(self) -> None: |
| 109 |
self.parts = {} |
| 110 |
self.translator_class = HTMLTranslator |
| 111 |
|
| 112 |
|
| 113 |
class HTMLTranslator(writers._html_base.HTMLTranslator): |
| 114 |
""" |
| 115 |
The html4css1 writer has been optimized to produce visually compact |
| 116 |
lists (less vertical whitespace). HTML's mixed content models |
| 117 |
allow list items to contain "<li><p>body elements</p></li>" or |
| 118 |
"<li>just text</li>" or even "<li>text<p>and body |
| 119 |
elements</p>combined</li>", each with different effects. It would |
| 120 |
be best to stick with strict body elements in list items, but they |
| 121 |
affect vertical spacing in older browsers (although they really |
| 122 |
shouldn't). |
| 123 |
The html5_polyglot writer solves this using CSS2. |
| 124 |
|
| 125 |
Here is an outline of the optimization: |
| 126 |
|
| 127 |
- Check for and omit <p> tags in "simple" lists: list items |
| 128 |
contain either a single paragraph, a nested simple list, or a |
| 129 |
paragraph followed by a nested simple list. This means that |
| 130 |
this list can be compact: |
| 131 |
|
| 132 |
- Item 1. |
| 133 |
- Item 2. |
| 134 |
|
| 135 |
But this list cannot be compact: |
| 136 |
|
| 137 |
- Item 1. |
| 138 |
|
| 139 |
This second paragraph forces space between list items. |
| 140 |
|
| 141 |
- Item 2. |
| 142 |
|
| 143 |
- In non-list contexts, omit <p> tags on a paragraph if that |
| 144 |
paragraph is the only child of its parent (footnotes & citations |
| 145 |
are allowed a label first). |
| 146 |
|
| 147 |
- Regardless of the above, in definitions, table cells, field bodies, |
| 148 |
option descriptions, and list items, mark the first child with |
| 149 |
'class="first"' and the last child with 'class="last"'. The stylesheet |
| 150 |
sets the margins (top & bottom respectively) to 0 for these elements. |
| 151 |
|
| 152 |
The ``no_compact_lists`` setting (``--no-compact-lists`` command-line |
| 153 |
option) disables list whitespace optimization. |
| 154 |
""" |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
doctype = ( |
| 160 |
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"' |
| 161 |
' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n') |
| 162 |
|
| 163 |
content_type = ('<meta http-equiv="Content-Type"' |
| 164 |
' content="text/html; charset=%s" />\n') |
| 165 |
content_type_mathml = ('<meta http-equiv="Content-Type"' |
| 166 |
' content="application/xhtml+xml; charset=%s" />\n') |
| 167 |
|
| 168 |
|
| 169 |
special_characters = _html_base.HTMLTranslator.special_characters.copy() |
| 170 |
special_characters[0xa0] = ' ' |
| 171 |
|
| 172 |
|
| 173 |
attribution_formats = {'dash': ('—', ''), |
| 174 |
'parentheses': ('(', ')'), |
| 175 |
'parens': ('(', ')'), |
| 176 |
'none': ('', '')} |
| 177 |
|
| 178 |
|
| 179 |
def set_first_last(self, node) -> None: |
| 180 |
self.set_class_on_child(node, 'first', 0) |
| 181 |
self.set_class_on_child(node, 'last', -1) |
| 182 |
|
| 183 |
|
| 184 |
def visit_address(self, node) -> None: |
| 185 |
self.visit_docinfo_item(node, 'address', meta=False) |
| 186 |
self.body.append(self.starttag(node, 'pre', CLASS='address')) |
| 187 |
|
| 188 |
def depart_address(self, node) -> None: |
| 189 |
self.body.append('\n</pre>\n') |
| 190 |
self.depart_docinfo_item() |
| 191 |
|
| 192 |
|
| 193 |
def visit_admonition(self, node) -> None: |
| 194 |
node['classes'].insert(0, 'admonition') |
| 195 |
self.body.append(self.starttag(node, 'div')) |
| 196 |
self.set_first_last(node) |
| 197 |
|
| 198 |
def depart_admonition(self, node=None) -> None: |
| 199 |
self.body.append('</div>\n') |
| 200 |
|
| 201 |
|
| 202 |
def visit_author(self, node) -> None: |
| 203 |
if isinstance(node.parent, nodes.authors): |
| 204 |
if self.author_in_authors: |
| 205 |
self.body.append('\n<br />') |
| 206 |
else: |
| 207 |
self.visit_docinfo_item(node, 'author') |
| 208 |
|
| 209 |
def depart_author(self, node) -> None: |
| 210 |
if isinstance(node.parent, nodes.authors): |
| 211 |
self.author_in_authors = True |
| 212 |
else: |
| 213 |
self.depart_docinfo_item() |
| 214 |
|
| 215 |
def visit_authors(self, node) -> None: |
| 216 |
self.visit_docinfo_item(node, 'authors') |
| 217 |
self.author_in_authors = False |
| 218 |
|
| 219 |
def depart_authors(self, node) -> None: |
| 220 |
self.depart_docinfo_item() |
| 221 |
|
| 222 |
|
| 223 |
def visit_colspec(self, node) -> None: |
| 224 |
self.colspecs.append(node) |
| 225 |
|
| 226 |
node.parent.stubs.append(node.attributes.get('stub')) |
| 227 |
|
| 228 |
def depart_colspec(self, node) -> None: |
| 229 |
|
| 230 |
if isinstance(node.next_node(descend=False, siblings=True), |
| 231 |
nodes.colspec): |
| 232 |
return |
| 233 |
if ('colwidths-auto' in node.parent.parent['classes'] |
| 234 |
or ('colwidths-auto' in self.settings.table_style |
| 235 |
and 'colwidths-given' not in node.parent.parent['classes'])): |
| 236 |
return |
| 237 |
total_width = sum(node.propwidth() for node in self.colspecs) |
| 238 |
self.body.append(self.starttag(node, 'colgroup')) |
| 239 |
for node in self.colspecs: |
| 240 |
colwidth = node.propwidth() * 100.0 / total_width + 0.5 |
| 241 |
self.body.append(self.emptytag(node, 'col', |
| 242 |
width='%i%%' % colwidth)) |
| 243 |
self.body.append('</colgroup>\n') |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
def is_compactable(self, node): |
| 249 |
return ('compact' in node['classes'] |
| 250 |
or (self.settings.compact_lists |
| 251 |
and 'open' not in node['classes'] |
| 252 |
and (self.compact_simple |
| 253 |
or 'contents' in node.parent['classes'] |
| 254 |
|
| 255 |
or self.check_simple_list(node)))) |
| 256 |
|
| 257 |
|
| 258 |
def visit_citation(self, node) -> None: |
| 259 |
self.body.append(self.starttag(node, 'table', |
| 260 |
CLASS='docutils citation', |
| 261 |
frame="void", rules="none")) |
| 262 |
self.body.append('<colgroup><col class="label" /><col /></colgroup>\n' |
| 263 |
'<tbody valign="top">\n' |
| 264 |
'<tr>') |
| 265 |
self.footnote_backrefs(node) |
| 266 |
|
| 267 |
def depart_citation(self, node) -> None: |
| 268 |
self.body.append('</td></tr>\n' |
| 269 |
'</tbody>\n</table>\n') |
| 270 |
|
| 271 |
def visit_citation_reference(self, node) -> None: |
| 272 |
href = '#' |
| 273 |
if 'refid' in node: |
| 274 |
href += node['refid'] |
| 275 |
elif 'refname' in node: |
| 276 |
href += self.document.nameids[node['refname']] |
| 277 |
self.body.append(self.starttag(node, 'a', suffix='[', href=href, |
| 278 |
classes=['citation-reference'])) |
| 279 |
|
| 280 |
def depart_citation_reference(self, node) -> None: |
| 281 |
self.body.append(']</a>') |
| 282 |
|
| 283 |
|
| 284 |
def visit_classifier(self, node) -> None: |
| 285 |
self.body.append(' <span class="classifier-delimiter">:</span> ') |
| 286 |
self.body.append(self.starttag(node, 'span', '', CLASS='classifier')) |
| 287 |
|
| 288 |
def depart_classifier(self, node) -> None: |
| 289 |
self.body.append('</span>') |
| 290 |
self.depart_term(node) |
| 291 |
|
| 292 |
|
| 293 |
def visit_compound(self, node) -> None: |
| 294 |
self.body.append(self.starttag(node, 'div', CLASS='compound')) |
| 295 |
if len(node) > 1: |
| 296 |
node[0]['classes'].append('compound-first') |
| 297 |
node[-1]['classes'].append('compound-last') |
| 298 |
for child in node[1:-1]: |
| 299 |
child['classes'].append('compound-middle') |
| 300 |
|
| 301 |
def depart_compound(self, node) -> None: |
| 302 |
self.body.append('</div>\n') |
| 303 |
|
| 304 |
|
| 305 |
def visit_definition(self, node) -> None: |
| 306 |
self.body.append(self.starttag(node, 'dd', '')) |
| 307 |
self.set_first_last(node) |
| 308 |
|
| 309 |
def depart_definition(self, node) -> None: |
| 310 |
self.body.append('</dd>\n') |
| 311 |
|
| 312 |
|
| 313 |
def visit_definition_list(self, node) -> None: |
| 314 |
self.body.append(self.starttag(node, 'dl', CLASS='docutils')) |
| 315 |
|
| 316 |
def depart_definition_list(self, node) -> None: |
| 317 |
self.body.append('</dl>\n') |
| 318 |
|
| 319 |
|
| 320 |
def visit_definition_list_item(self, node) -> None: |
| 321 |
pass |
| 322 |
|
| 323 |
def depart_definition_list_item(self, node) -> None: |
| 324 |
pass |
| 325 |
|
| 326 |
|
| 327 |
def visit_description(self, node) -> None: |
| 328 |
self.body.append(self.starttag(node, 'td', '')) |
| 329 |
self.set_first_last(node) |
| 330 |
|
| 331 |
def depart_description(self, node) -> None: |
| 332 |
self.body.append('</td>') |
| 333 |
|
| 334 |
|
| 335 |
def visit_docinfo(self, node) -> None: |
| 336 |
self.context.append(len(self.body)) |
| 337 |
self.body.append(self.starttag(node, 'table', |
| 338 |
CLASS='docinfo', |
| 339 |
frame="void", rules="none")) |
| 340 |
self.body.append('<col class="docinfo-name" />\n' |
| 341 |
'<col class="docinfo-content" />\n' |
| 342 |
'<tbody valign="top">\n') |
| 343 |
self.in_docinfo = True |
| 344 |
|
| 345 |
def depart_docinfo(self, node) -> None: |
| 346 |
self.body.append('</tbody>\n</table>\n') |
| 347 |
self.in_docinfo = False |
| 348 |
start = self.context.pop() |
| 349 |
self.docinfo = self.body[start:] |
| 350 |
self.body = [] |
| 351 |
|
| 352 |
def visit_docinfo_item(self, node, name, meta=True) -> None: |
| 353 |
if meta: |
| 354 |
meta_tag = '<meta name="%s" content="%s" />\n' \ |
| 355 |
% (name, self.attval(node.astext())) |
| 356 |
self.meta.append(meta_tag) |
| 357 |
self.body.append(self.starttag(node, 'tr', '')) |
| 358 |
self.body.append('<th class="docinfo-name">%s:</th>\n<td>' |
| 359 |
% self.language.labels[name]) |
| 360 |
if len(node): |
| 361 |
if isinstance(node[0], nodes.Element): |
| 362 |
node[0]['classes'].append('first') |
| 363 |
if isinstance(node[-1], nodes.Element): |
| 364 |
node[-1]['classes'].append('last') |
| 365 |
|
| 366 |
def depart_docinfo_item(self) -> None: |
| 367 |
self.body.append('</td></tr>\n') |
| 368 |
|
| 369 |
|
| 370 |
def visit_doctest_block(self, node) -> None: |
| 371 |
self.body.append(self.starttag(node, 'pre', CLASS='doctest-block')) |
| 372 |
|
| 373 |
def depart_doctest_block(self, node) -> None: |
| 374 |
self.body.append('\n</pre>\n') |
| 375 |
|
| 376 |
|
| 377 |
def visit_entry(self, node) -> None: |
| 378 |
writers._html_base.HTMLTranslator.visit_entry(self, node) |
| 379 |
if len(node) == 0: |
| 380 |
self.body.append(' ') |
| 381 |
self.set_first_last(node) |
| 382 |
|
| 383 |
def depart_entry(self, node) -> None: |
| 384 |
self.body.append(self.context.pop()) |
| 385 |
|
| 386 |
|
| 387 |
def visit_enumerated_list(self, node) -> None: |
| 388 |
""" |
| 389 |
The 'start' attribute does not conform to HTML 4.01's strict.dtd, but |
| 390 |
cannot be emulated in CSS1 (HTML 5 reincludes it). |
| 391 |
""" |
| 392 |
atts = {} |
| 393 |
if 'start' in node: |
| 394 |
atts['start'] = node['start'] |
| 395 |
if 'enumtype' in node: |
| 396 |
atts['class'] = node['enumtype'] |
| 397 |
|
| 398 |
|
| 399 |
old_compact_simple = self.compact_simple |
| 400 |
self.context.append((self.compact_simple, self.compact_p)) |
| 401 |
self.compact_p = None |
| 402 |
self.compact_simple = self.is_compactable(node) |
| 403 |
if self.compact_simple and not old_compact_simple: |
| 404 |
atts['class'] = (atts.get('class', '') + ' simple').strip() |
| 405 |
self.body.append(self.starttag(node, 'ol', **atts)) |
| 406 |
|
| 407 |
def depart_enumerated_list(self, node) -> None: |
| 408 |
self.compact_simple, self.compact_p = self.context.pop() |
| 409 |
self.body.append('</ol>\n') |
| 410 |
|
| 411 |
|
| 412 |
def visit_field(self, node) -> None: |
| 413 |
self.body.append(self.starttag(node, 'tr', '', CLASS='field')) |
| 414 |
|
| 415 |
def depart_field(self, node) -> None: |
| 416 |
self.body.append('</tr>\n') |
| 417 |
|
| 418 |
def visit_field_body(self, node) -> None: |
| 419 |
self.body.append(self.starttag(node, 'td', '', CLASS='field-body')) |
| 420 |
self.set_class_on_child(node, 'first', 0) |
| 421 |
field = node.parent |
| 422 |
if (self.compact_field_list |
| 423 |
or isinstance(field.parent, nodes.docinfo) |
| 424 |
or field.parent.index(field) == len(field.parent) - 1): |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
self.set_class_on_child(node, 'last', -1) |
| 429 |
|
| 430 |
def depart_field_body(self, node) -> None: |
| 431 |
self.body.append('</td>\n') |
| 432 |
|
| 433 |
def visit_field_list(self, node) -> None: |
| 434 |
self.context.append((self.compact_field_list, self.compact_p)) |
| 435 |
self.compact_p = None |
| 436 |
if 'compact' in node['classes']: |
| 437 |
self.compact_field_list = True |
| 438 |
elif (self.settings.compact_field_lists |
| 439 |
and 'open' not in node['classes']): |
| 440 |
self.compact_field_list = True |
| 441 |
if self.compact_field_list: |
| 442 |
for field in node: |
| 443 |
field_body = field[-1] |
| 444 |
assert isinstance(field_body, nodes.field_body) |
| 445 |
children = [n for n in field_body |
| 446 |
if not isinstance(n, nodes.Invisible)] |
| 447 |
if not (len(children) == 0 |
| 448 |
or len(children) == 1 |
| 449 |
and isinstance(children[0], |
| 450 |
(nodes.paragraph, nodes.line_block))): |
| 451 |
self.compact_field_list = False |
| 452 |
break |
| 453 |
self.body.append(self.starttag(node, 'table', frame='void', |
| 454 |
rules='none', |
| 455 |
CLASS='docutils field-list')) |
| 456 |
self.body.append('<col class="field-name" />\n' |
| 457 |
'<col class="field-body" />\n' |
| 458 |
'<tbody valign="top">\n') |
| 459 |
|
| 460 |
def depart_field_list(self, node) -> None: |
| 461 |
self.body.append('</tbody>\n</table>\n') |
| 462 |
self.compact_field_list, self.compact_p = self.context.pop() |
| 463 |
|
| 464 |
def visit_field_name(self, node) -> None: |
| 465 |
atts = {} |
| 466 |
if self.in_docinfo: |
| 467 |
atts['class'] = 'docinfo-name' |
| 468 |
else: |
| 469 |
atts['class'] = 'field-name' |
| 470 |
if (self.settings.field_name_limit |
| 471 |
and len(node.astext()) > self.settings.field_name_limit): |
| 472 |
atts['colspan'] = 2 |
| 473 |
self.context.append('</tr>\n' |
| 474 |
+ self.starttag(node.parent, 'tr', '', |
| 475 |
CLASS='field') |
| 476 |
+ '<td> </td>') |
| 477 |
else: |
| 478 |
self.context.append('') |
| 479 |
self.body.append(self.starttag(node, 'th', '', **atts)) |
| 480 |
|
| 481 |
def depart_field_name(self, node) -> None: |
| 482 |
self.body.append(':</th>') |
| 483 |
self.body.append(self.context.pop()) |
| 484 |
|
| 485 |
|
| 486 |
def visit_footnote(self, node) -> None: |
| 487 |
self.body.append(self.starttag(node, 'table', |
| 488 |
CLASS='docutils footnote', |
| 489 |
frame="void", rules="none")) |
| 490 |
self.body.append('<colgroup><col class="label" /><col /></colgroup>\n' |
| 491 |
'<tbody valign="top">\n' |
| 492 |
'<tr>') |
| 493 |
self.footnote_backrefs(node) |
| 494 |
|
| 495 |
def footnote_backrefs(self, node) -> None: |
| 496 |
backlinks = [] |
| 497 |
backrefs = node['backrefs'] |
| 498 |
if self.settings.footnote_backlinks and backrefs: |
| 499 |
if len(backrefs) == 1: |
| 500 |
self.context.append('') |
| 501 |
self.context.append('</a>') |
| 502 |
self.context.append('<a class="fn-backref" href="#%s">' |
| 503 |
% backrefs[0]) |
| 504 |
else: |
| 505 |
for (i, backref) in enumerate(backrefs, 1): |
| 506 |
backlinks.append('<a class="fn-backref" href="#%s">%s</a>' |
| 507 |
% (backref, i)) |
| 508 |
self.context.append('<em>(%s)</em> ' % ', '.join(backlinks)) |
| 509 |
self.context += ['', ''] |
| 510 |
else: |
| 511 |
self.context.append('') |
| 512 |
self.context += ['', ''] |
| 513 |
|
| 514 |
if len(node) > 1: |
| 515 |
|
| 516 |
|
| 517 |
if not backlinks: |
| 518 |
node[1]['classes'].append('first') |
| 519 |
node[-1]['classes'].append('last') |
| 520 |
|
| 521 |
def depart_footnote(self, node) -> None: |
| 522 |
self.body.append('</td></tr>\n' |
| 523 |
'</tbody>\n</table>\n') |
| 524 |
|
| 525 |
|
| 526 |
def visit_footnote_reference(self, node) -> None: |
| 527 |
href = '#' + node['refid'] |
| 528 |
format = self.settings.footnote_references |
| 529 |
if format == 'brackets': |
| 530 |
suffix = '[' |
| 531 |
self.context.append(']') |
| 532 |
else: |
| 533 |
assert format == 'superscript' |
| 534 |
suffix = '<sup>' |
| 535 |
self.context.append('</sup>') |
| 536 |
self.body.append(self.starttag(node, 'a', suffix, |
| 537 |
CLASS='footnote-reference', href=href)) |
| 538 |
|
| 539 |
def depart_footnote_reference(self, node) -> None: |
| 540 |
self.body.append(self.context.pop() + '</a>') |
| 541 |
|
| 542 |
|
| 543 |
def visit_generated(self, node) -> None: |
| 544 |
pass |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
object_image_types = {'.svg': 'image/svg+xml', |
| 553 |
'.swf': 'application/x-shockwave-flash', |
| 554 |
'.mp4': 'video/mp4', |
| 555 |
'.webm': 'video/webm', |
| 556 |
'.ogg': 'video/ogg', |
| 557 |
} |
| 558 |
|
| 559 |
def visit_image(self, node) -> None: |
| 560 |
atts = {} |
| 561 |
uri = node['uri'] |
| 562 |
ext = os.path.splitext(uri)[1].lower() |
| 563 |
if ext in self.object_image_types: |
| 564 |
atts['data'] = uri |
| 565 |
atts['type'] = self.object_image_types[ext] |
| 566 |
else: |
| 567 |
atts['src'] = uri |
| 568 |
atts['alt'] = node.get('alt', uri) |
| 569 |
|
| 570 |
if 'width' in node: |
| 571 |
atts['width'] = node['width'] |
| 572 |
if 'height' in node: |
| 573 |
atts['height'] = node['height'] |
| 574 |
if 'scale' in node: |
| 575 |
if (PIL and ('width' not in node or 'height' not in node) |
| 576 |
and self.settings.file_insertion_enabled): |
| 577 |
try: |
| 578 |
imagepath = self.uri2path(uri) |
| 579 |
with PIL.Image.open(imagepath) as img: |
| 580 |
img_size = img.size |
| 581 |
except (ValueError, OSError, UnicodeEncodeError) as e: |
| 582 |
self.document.reporter.warning( |
| 583 |
f'Problem reading image file: {e}') |
| 584 |
else: |
| 585 |
self.settings.record_dependencies.add(imagepath.as_posix()) |
| 586 |
if 'width' not in atts: |
| 587 |
atts['width'] = '%dpx' % img_size[0] |
| 588 |
if 'height' not in atts: |
| 589 |
atts['height'] = '%dpx' % img_size[1] |
| 590 |
for att_name in 'width', 'height': |
| 591 |
if att_name in atts: |
| 592 |
match = re.match(r'([0-9.]+)(\S*)$', atts[att_name]) |
| 593 |
assert match |
| 594 |
atts[att_name] = '%s%s' % ( |
| 595 |
float(match.group(1)) * (float(node['scale']) / 100), |
| 596 |
match.group(2)) |
| 597 |
style = [] |
| 598 |
for att_name in 'width', 'height': |
| 599 |
if att_name in atts: |
| 600 |
if re.match(r'^[0-9.]+$', atts[att_name]): |
| 601 |
|
| 602 |
atts[att_name] += 'px' |
| 603 |
style.append('%s: %s;' % (att_name, atts[att_name])) |
| 604 |
del atts[att_name] |
| 605 |
if style: |
| 606 |
atts['style'] = ' '.join(style) |
| 607 |
|
| 608 |
if (not isinstance(node.parent, nodes.TextElement) |
| 609 |
or isinstance(node.parent, nodes.reference) |
| 610 |
and not isinstance(node.parent.parent, nodes.TextElement)): |
| 611 |
suffix = '\n' |
| 612 |
else: |
| 613 |
suffix = '' |
| 614 |
if 'align' in node: |
| 615 |
atts['class'] = 'align-%s' % node['align'] |
| 616 |
if ext in self.object_image_types: |
| 617 |
|
| 618 |
self.body.append(self.starttag(node, 'object', '', **atts) |
| 619 |
+ node.get('alt', uri) + '</object>' + suffix) |
| 620 |
else: |
| 621 |
self.body.append(self.emptytag(node, 'img', suffix, **atts)) |
| 622 |
|
| 623 |
def depart_image(self, node) -> None: |
| 624 |
pass |
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
def visit_label(self, node) -> None: |
| 629 |
self.body.append(self.starttag(node, 'td', '%s[' % self.context.pop(), |
| 630 |
CLASS='label')) |
| 631 |
|
| 632 |
def depart_label(self, node) -> None: |
| 633 |
self.body.append(f']{self.context.pop()}</td><td>{self.context.pop()}') |
| 634 |
|
| 635 |
|
| 636 |
def visit_list_item(self, node) -> None: |
| 637 |
self.body.append(self.starttag(node, 'li', '')) |
| 638 |
if len(node): |
| 639 |
node[0]['classes'].append('first') |
| 640 |
|
| 641 |
def depart_list_item(self, node) -> None: |
| 642 |
self.body.append('</li>\n') |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
def visit_literal(self, node): |
| 647 |
|
| 648 |
classes = node['classes'] |
| 649 |
if 'code' in classes: |
| 650 |
|
| 651 |
node['classes'] = [cls for cls in classes if cls != 'code'] |
| 652 |
self.body.append(self.starttag(node, 'code', '')) |
| 653 |
return |
| 654 |
self.body.append( |
| 655 |
self.starttag(node, 'tt', '', CLASS='docutils literal')) |
| 656 |
text = node.astext() |
| 657 |
for token in self.words_and_spaces.findall(text): |
| 658 |
if token.strip(): |
| 659 |
|
| 660 |
|
| 661 |
if self.in_word_wrap_point.search(token): |
| 662 |
self.body.append('<span class="pre">%s</span>' |
| 663 |
% self.encode(token)) |
| 664 |
else: |
| 665 |
self.body.append(self.encode(token)) |
| 666 |
elif token in ('\n', ' '): |
| 667 |
|
| 668 |
self.body.append(token) |
| 669 |
else: |
| 670 |
|
| 671 |
self.body.append(' ' * (len(token) - 1) + ' ') |
| 672 |
self.body.append('</tt>') |
| 673 |
|
| 674 |
raise nodes.SkipNode |
| 675 |
|
| 676 |
def depart_literal(self, node) -> None: |
| 677 |
|
| 678 |
self.body.append('</code>') |
| 679 |
|
| 680 |
|
| 681 |
def visit_literal_block(self, node) -> None: |
| 682 |
self.body.append(self.starttag(node, 'pre', CLASS='literal-block')) |
| 683 |
|
| 684 |
def depart_literal_block(self, node) -> None: |
| 685 |
self.body.append('\n</pre>\n') |
| 686 |
|
| 687 |
|
| 688 |
def visit_option_group(self, node) -> None: |
| 689 |
atts = {} |
| 690 |
if (self.settings.option_limit |
| 691 |
and len(node.astext()) > self.settings.option_limit): |
| 692 |
atts['colspan'] = 2 |
| 693 |
self.context.append('</tr>\n<tr><td> </td>') |
| 694 |
else: |
| 695 |
self.context.append('') |
| 696 |
self.body.append( |
| 697 |
self.starttag(node, 'td', CLASS='option-group', **atts)) |
| 698 |
self.body.append('<kbd>') |
| 699 |
self.context.append(0) |
| 700 |
|
| 701 |
def depart_option_group(self, node) -> None: |
| 702 |
self.context.pop() |
| 703 |
self.body.append('</kbd></td>\n') |
| 704 |
self.body.append(self.context.pop()) |
| 705 |
|
| 706 |
def visit_option_list(self, node) -> None: |
| 707 |
self.body.append( |
| 708 |
self.starttag(node, 'table', CLASS='docutils option-list', |
| 709 |
frame="void", rules="none")) |
| 710 |
self.body.append('<col class="option" />\n' |
| 711 |
'<col class="description" />\n' |
| 712 |
'<tbody valign="top">\n') |
| 713 |
|
| 714 |
def depart_option_list(self, node) -> None: |
| 715 |
self.body.append('</tbody>\n</table>\n') |
| 716 |
|
| 717 |
def visit_option_list_item(self, node) -> None: |
| 718 |
self.body.append(self.starttag(node, 'tr', '')) |
| 719 |
|
| 720 |
def depart_option_list_item(self, node) -> None: |
| 721 |
self.body.append('</tr>\n') |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
def should_be_compact_paragraph(self, node) -> bool: |
| 726 |
""" |
| 727 |
Determine if the <p> tags around paragraph ``node`` can be omitted. |
| 728 |
""" |
| 729 |
if (isinstance(node.parent, nodes.document) |
| 730 |
or isinstance(node.parent, nodes.compound)): |
| 731 |
|
| 732 |
return False |
| 733 |
for key, value in node.attlist(): |
| 734 |
if (node.is_not_default(key) |
| 735 |
and not (key == 'classes' |
| 736 |
and value in ([], ['first'], |
| 737 |
['last'], ['first', 'last']))): |
| 738 |
|
| 739 |
return False |
| 740 |
first = isinstance(node.parent[0], nodes.label) |
| 741 |
for child in node.parent.children[first:]: |
| 742 |
|
| 743 |
if isinstance(child, nodes.Invisible): |
| 744 |
continue |
| 745 |
if child is node: |
| 746 |
break |
| 747 |
return False |
| 748 |
parent_length = len([n for n in node.parent if not isinstance( |
| 749 |
n, (nodes.Invisible, nodes.label))]) |
| 750 |
if (self.compact_simple |
| 751 |
or self.compact_field_list |
| 752 |
or self.compact_p and parent_length == 1): |
| 753 |
return True |
| 754 |
return False |
| 755 |
|
| 756 |
def visit_paragraph(self, node) -> None: |
| 757 |
if self.should_be_compact_paragraph(node): |
| 758 |
self.context.append('') |
| 759 |
else: |
| 760 |
self.body.append(self.starttag(node, 'p', '')) |
| 761 |
self.context.append('</p>\n') |
| 762 |
|
| 763 |
def depart_paragraph(self, node) -> None: |
| 764 |
self.body.append(self.context.pop()) |
| 765 |
self.report_messages(node) |
| 766 |
|
| 767 |
|
| 768 |
def visit_sidebar(self, node) -> None: |
| 769 |
self.body.append( |
| 770 |
self.starttag(node, 'div', CLASS='sidebar')) |
| 771 |
self.set_first_last(node) |
| 772 |
self.in_sidebar = True |
| 773 |
|
| 774 |
def depart_sidebar(self, node) -> None: |
| 775 |
self.body.append('</div>\n') |
| 776 |
self.in_sidebar = False |
| 777 |
|
| 778 |
|
| 779 |
def visit_subscript(self, node) -> None: |
| 780 |
if isinstance(node.parent, nodes.literal_block): |
| 781 |
self.body.append(self.starttag(node, 'span', '', |
| 782 |
CLASS='subscript')) |
| 783 |
else: |
| 784 |
self.body.append(self.starttag(node, 'sub', '')) |
| 785 |
|
| 786 |
def depart_subscript(self, node) -> None: |
| 787 |
if isinstance(node.parent, nodes.literal_block): |
| 788 |
self.body.append('</span>') |
| 789 |
else: |
| 790 |
self.body.append('</sub>') |
| 791 |
|
| 792 |
|
| 793 |
def visit_subtitle(self, node) -> None: |
| 794 |
if isinstance(node.parent, nodes.sidebar): |
| 795 |
self.body.append(self.starttag(node, 'p', '', |
| 796 |
CLASS='sidebar-subtitle')) |
| 797 |
self.context.append('</p>\n') |
| 798 |
elif isinstance(node.parent, nodes.document): |
| 799 |
self.body.append(self.starttag(node, 'h2', '', CLASS='subtitle')) |
| 800 |
self.context.append('</h2>\n') |
| 801 |
self.in_document_title = len(self.body) |
| 802 |
elif isinstance(node.parent, nodes.section): |
| 803 |
tag = 'h%s' % (self.section_level + self.initial_header_level - 1) |
| 804 |
self.body.append( |
| 805 |
self.starttag(node, tag, '', CLASS='section-subtitle') |
| 806 |
+ self.starttag({}, 'span', '', CLASS='section-subtitle')) |
| 807 |
self.context.append('</span></%s>\n' % tag) |
| 808 |
|
| 809 |
def depart_subtitle(self, node) -> None: |
| 810 |
self.body.append(self.context.pop()) |
| 811 |
if self.in_document_title: |
| 812 |
self.subtitle = self.body[self.in_document_title:-1] |
| 813 |
self.in_document_title = 0 |
| 814 |
self.body_pre_docinfo.extend(self.body) |
| 815 |
self.html_subtitle.extend(self.body) |
| 816 |
del self.body[:] |
| 817 |
|
| 818 |
|
| 819 |
def visit_superscript(self, node) -> None: |
| 820 |
if isinstance(node.parent, nodes.literal_block): |
| 821 |
self.body.append(self.starttag(node, 'span', '', |
| 822 |
CLASS='superscript')) |
| 823 |
else: |
| 824 |
self.body.append(self.starttag(node, 'sup', '')) |
| 825 |
|
| 826 |
def depart_superscript(self, node) -> None: |
| 827 |
if isinstance(node.parent, nodes.literal_block): |
| 828 |
self.body.append('</span>') |
| 829 |
else: |
| 830 |
self.body.append('</sup>') |
| 831 |
|
| 832 |
|
| 833 |
def visit_system_message(self, node) -> None: |
| 834 |
self.body.append(self.starttag(node, 'div', CLASS='system-message')) |
| 835 |
self.body.append('<p class="system-message-title">') |
| 836 |
backref_text = '' |
| 837 |
if len(node['backrefs']): |
| 838 |
backrefs = node['backrefs'] |
| 839 |
if len(backrefs) == 1: |
| 840 |
backref_text = ('; <em><a href="#%s">backlink</a></em>' |
| 841 |
% backrefs[0]) |
| 842 |
else: |
| 843 |
i = 1 |
| 844 |
backlinks = [] |
| 845 |
for backref in backrefs: |
| 846 |
backlinks.append('<a href="#%s">%s</a>' % (backref, i)) |
| 847 |
i += 1 |
| 848 |
backref_text = ('; <em>backlinks: %s</em>' |
| 849 |
% ', '.join(backlinks)) |
| 850 |
if node.hasattr('line'): |
| 851 |
line = ', line %s' % node['line'] |
| 852 |
else: |
| 853 |
line = '' |
| 854 |
self.body.append('System Message: %s/%s ' |
| 855 |
'(<tt class="docutils">%s</tt>%s)%s</p>\n' |
| 856 |
% (node['type'], node['level'], |
| 857 |
self.encode(node['source']), line, backref_text)) |
| 858 |
|
| 859 |
def depart_system_message(self, node) -> None: |
| 860 |
self.body.append('</div>\n') |
| 861 |
|
| 862 |
|
| 863 |
def visit_table(self, node) -> None: |
| 864 |
self.context.append(self.compact_p) |
| 865 |
self.compact_p = True |
| 866 |
atts = {'border': 1} |
| 867 |
classes = ['docutils', self.settings.table_style] |
| 868 |
if 'align' in node: |
| 869 |
classes.append('align-%s' % node['align']) |
| 870 |
if 'width' in node: |
| 871 |
width = node['width'] |
| 872 |
if width[-1:] in '0123456789.': |
| 873 |
width += 'px' |
| 874 |
atts['style'] = f'width: {width}' |
| 875 |
self.body.append( |
| 876 |
self.starttag(node, 'table', CLASS=' '.join(classes), **atts)) |
| 877 |
|
| 878 |
def depart_table(self, node) -> None: |
| 879 |
self.compact_p = self.context.pop() |
| 880 |
self.body.append('</table>\n') |
| 881 |
|
| 882 |
|
| 883 |
def visit_tbody(self, node) -> None: |
| 884 |
self.body.append(self.starttag(node, 'tbody', valign='top')) |
| 885 |
|
| 886 |
def depart_tbody(self, node) -> None: |
| 887 |
self.body.append('</tbody>\n') |
| 888 |
|
| 889 |
|
| 890 |
def visit_term(self, node) -> None: |
| 891 |
self.body.append(self.starttag(node, 'dt', '', |
| 892 |
classes=node.parent['classes'], |
| 893 |
ids=node.parent['ids'])) |
| 894 |
|
| 895 |
def depart_term(self, node) -> None: |
| 896 |
|
| 897 |
if node.next_node(nodes.classifier, descend=False, siblings=True): |
| 898 |
return |
| 899 |
self.body.append('</dt>\n') |
| 900 |
|
| 901 |
|
| 902 |
def visit_thead(self, node) -> None: |
| 903 |
self.body.append(self.starttag(node, 'thead', valign='bottom')) |
| 904 |
|
| 905 |
def depart_thead(self, node) -> None: |
| 906 |
self.body.append('</thead>\n') |
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
def section_title_tags(self, node): |
| 911 |
classes = [] |
| 912 |
h_level = self.section_level + self.initial_header_level - 1 |
| 913 |
if (len(node.parent) >= 2 |
| 914 |
and isinstance(node.parent[1], nodes.subtitle)): |
| 915 |
classes.append('with-subtitle') |
| 916 |
if h_level > 6: |
| 917 |
classes.append('h%i' % h_level) |
| 918 |
tagname = 'h%i' % min(h_level, 6) |
| 919 |
start_tag = self.starttag(node, tagname, '', classes=classes) |
| 920 |
if node.hasattr('refid'): |
| 921 |
atts = {} |
| 922 |
atts['class'] = 'toc-backref' |
| 923 |
atts['href'] = '#' + node['refid'] |
| 924 |
start_tag += self.starttag({}, 'a', '', **atts) |
| 925 |
close_tag = '</a></%s>\n' % tagname |
| 926 |
else: |
| 927 |
close_tag = '</%s>\n' % tagname |
| 928 |
return start_tag, close_tag |
| 929 |
|
| 930 |
|
| 931 |
class SimpleListChecker(writers._html_base.SimpleListChecker): |
| 932 |
|
| 933 |
""" |
| 934 |
Raise `nodes.NodeFound` if non-simple list item is encountered. |
| 935 |
|
| 936 |
Here "simple" means a list item containing nothing other than a single |
| 937 |
paragraph, a simple list, or a paragraph followed by a simple list. |
| 938 |
""" |
| 939 |
|
| 940 |
def visit_list_item(self, node): |
| 941 |
children = [child for child in node.children |
| 942 |
if not isinstance(child, nodes.Invisible)] |
| 943 |
if (children and isinstance(children[0], nodes.paragraph) |
| 944 |
and (isinstance(children[-1], nodes.bullet_list) |
| 945 |
or isinstance(children[-1], nodes.enumerated_list))): |
| 946 |
children.pop() |
| 947 |
if len(children) <= 1: |
| 948 |
return |
| 949 |
else: |
| 950 |
raise nodes.NodeFound |
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
def visit_paragraph(self, node): |
| 959 |
raise nodes.SkipNode |
| 960 |
|
| 961 |
def visit_definition_list(self, node): |
| 962 |
raise nodes.NodeFound |
| 963 |
|
| 964 |
def visit_docinfo(self, node): |
| 965 |
raise nodes.NodeFound |