PageRenderTime 62ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/documentor/libraries/docutils-0.9.1-py3.2/docutils/writers/html4css1/__init__.py

https://github.com/tictactatic/Superdesk
Python | 1692 lines | 1631 code | 14 blank | 47 comment | 41 complexity | 6c74a736d35a0e10b687b7059aa4d42a MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-3.0, GPL-2.0
  1. # $Id: __init__.py 7328 2012-01-27 08:41:35Z milde $
  2. # Author: David Goodger
  3. # Maintainer: docutils-develop@lists.sourceforge.net
  4. # Copyright: This module has been placed in the public domain.
  5. """
  6. Simple HyperText Markup Language document tree Writer.
  7. The output conforms to the XHTML version 1.0 Transitional DTD
  8. (*almost* strict). The output contains a minimum of formatting
  9. information. The cascading style sheet "html4css1.css" is required
  10. for proper viewing with a modern graphical browser.
  11. """
  12. __docformat__ = 'reStructuredText'
  13. import sys
  14. import os
  15. import os.path
  16. import time
  17. import re
  18. import urllib.request, urllib.parse, urllib.error
  19. try: # check for the Python Imaging Library
  20. import PIL
  21. except ImportError:
  22. try: # sometimes PIL modules are put in PYTHONPATH's root
  23. import Image
  24. class PIL(object): pass # dummy wrapper
  25. PIL.Image = Image
  26. except ImportError:
  27. PIL = None
  28. import docutils
  29. from docutils import frontend, nodes, utils, writers, languages, io
  30. from docutils.error_reporting import SafeString
  31. from docutils.transforms import writer_aux
  32. from docutils.math import unichar2tex, pick_math_environment
  33. from docutils.math.latex2mathml import parse_latex_math
  34. from docutils.math.math2html import math2html
  35. class Writer(writers.Writer):
  36. supported = ('html', 'html4css1', 'xhtml')
  37. """Formats this writer supports."""
  38. default_stylesheet = 'html4css1.css'
  39. default_stylesheet_path = utils.relative_path(
  40. os.path.join(os.getcwd(), 'dummy'),
  41. os.path.join(os.path.dirname(__file__), default_stylesheet))
  42. default_template = 'template.txt'
  43. default_template_path = utils.relative_path(
  44. os.path.join(os.getcwd(), 'dummy'),
  45. os.path.join(os.path.dirname(__file__), default_template))
  46. settings_spec = (
  47. 'HTML-Specific Options',
  48. None,
  49. (('Specify the template file (UTF-8 encoded). Default is "%s".'
  50. % default_template_path,
  51. ['--template'],
  52. {'default': default_template_path, 'metavar': '<file>'}),
  53. ('Specify comma separated list of stylesheet URLs. '
  54. 'Overrides previous --stylesheet and --stylesheet-path settings.',
  55. ['--stylesheet'],
  56. {'metavar': '<URL>', 'overrides': 'stylesheet_path'}),
  57. ('Specify comma separated list of stylesheet paths. '
  58. 'With --link-stylesheet, '
  59. 'the path is rewritten relative to the output HTML file. '
  60. 'Default: "%s"' % default_stylesheet_path,
  61. ['--stylesheet-path'],
  62. {'metavar': '<file>', 'overrides': 'stylesheet',
  63. 'default': default_stylesheet_path}),
  64. ('Embed the stylesheet(s) in the output HTML file. The stylesheet '
  65. 'files must be accessible during processing. This is the default.',
  66. ['--embed-stylesheet'],
  67. {'default': 1, 'action': 'store_true',
  68. 'validator': frontend.validate_boolean}),
  69. ('Link to the stylesheet(s) in the output HTML file. '
  70. 'Default: embed stylesheets.',
  71. ['--link-stylesheet'],
  72. {'dest': 'embed_stylesheet', 'action': 'store_false'}),
  73. ('Specify the initial header level. Default is 1 for "<h1>". '
  74. 'Does not affect document title & subtitle (see --no-doc-title).',
  75. ['--initial-header-level'],
  76. {'choices': '1 2 3 4 5 6'.split(), 'default': '1',
  77. 'metavar': '<level>'}),
  78. ('Specify the maximum width (in characters) for one-column field '
  79. 'names. Longer field names will span an entire row of the table '
  80. 'used to render the field list. Default is 14 characters. '
  81. 'Use 0 for "no limit".',
  82. ['--field-name-limit'],
  83. {'default': 14, 'metavar': '<level>',
  84. 'validator': frontend.validate_nonnegative_int}),
  85. ('Specify the maximum width (in characters) for options in option '
  86. 'lists. Longer options will span an entire row of the table used '
  87. 'to render the option list. Default is 14 characters. '
  88. 'Use 0 for "no limit".',
  89. ['--option-limit'],
  90. {'default': 14, 'metavar': '<level>',
  91. 'validator': frontend.validate_nonnegative_int}),
  92. ('Format for footnote references: one of "superscript" or '
  93. '"brackets". Default is "brackets".',
  94. ['--footnote-references'],
  95. {'choices': ['superscript', 'brackets'], 'default': 'brackets',
  96. 'metavar': '<format>',
  97. 'overrides': 'trim_footnote_reference_space'}),
  98. ('Format for block quote attributions: one of "dash" (em-dash '
  99. 'prefix), "parentheses"/"parens", or "none". Default is "dash".',
  100. ['--attribution'],
  101. {'choices': ['dash', 'parentheses', 'parens', 'none'],
  102. 'default': 'dash', 'metavar': '<format>'}),
  103. ('Remove extra vertical whitespace between items of "simple" bullet '
  104. 'lists and enumerated lists. Default: enabled.',
  105. ['--compact-lists'],
  106. {'default': 1, 'action': 'store_true',
  107. 'validator': frontend.validate_boolean}),
  108. ('Disable compact simple bullet and enumerated lists.',
  109. ['--no-compact-lists'],
  110. {'dest': 'compact_lists', 'action': 'store_false'}),
  111. ('Remove extra vertical whitespace between items of simple field '
  112. 'lists. Default: enabled.',
  113. ['--compact-field-lists'],
  114. {'default': 1, 'action': 'store_true',
  115. 'validator': frontend.validate_boolean}),
  116. ('Disable compact simple field lists.',
  117. ['--no-compact-field-lists'],
  118. {'dest': 'compact_field_lists', 'action': 'store_false'}),
  119. ('Added to standard table classes. '
  120. 'Defined styles: "borderless". Default: ""',
  121. ['--table-style'],
  122. {'default': ''}),
  123. ('Math output format, one of "MathML", "HTML", "MathJax" '
  124. 'or "LaTeX". Default: "MathJax"',
  125. ['--math-output'],
  126. {'default': 'MathJax'}),
  127. ('Omit the XML declaration. Use with caution.',
  128. ['--no-xml-declaration'],
  129. {'dest': 'xml_declaration', 'default': 1, 'action': 'store_false',
  130. 'validator': frontend.validate_boolean}),
  131. ('Obfuscate email addresses to confuse harvesters while still '
  132. 'keeping email links usable with standards-compliant browsers.',
  133. ['--cloak-email-addresses'],
  134. {'action': 'store_true', 'validator': frontend.validate_boolean}),))
  135. settings_defaults = {'output_encoding_error_handler': 'xmlcharrefreplace'}
  136. relative_path_settings = ('stylesheet_path',)
  137. config_section = 'html4css1 writer'
  138. config_section_dependencies = ('writers',)
  139. visitor_attributes = (
  140. 'head_prefix', 'head', 'stylesheet', 'body_prefix',
  141. 'body_pre_docinfo', 'docinfo', 'body', 'body_suffix',
  142. 'title', 'subtitle', 'header', 'footer', 'meta', 'fragment',
  143. 'html_prolog', 'html_head', 'html_title', 'html_subtitle',
  144. 'html_body')
  145. def get_transforms(self):
  146. return writers.Writer.get_transforms(self) + [writer_aux.Admonitions]
  147. def __init__(self):
  148. writers.Writer.__init__(self)
  149. self.translator_class = HTMLTranslator
  150. def translate(self):
  151. self.visitor = visitor = self.translator_class(self.document)
  152. self.document.walkabout(visitor)
  153. for attr in self.visitor_attributes:
  154. setattr(self, attr, getattr(visitor, attr))
  155. self.output = self.apply_template()
  156. def apply_template(self):
  157. template_file = open(self.document.settings.template, 'rb')
  158. template = str(template_file.read(), 'utf-8')
  159. template_file.close()
  160. subs = self.interpolation_dict()
  161. return template % subs
  162. def interpolation_dict(self):
  163. subs = {}
  164. settings = self.document.settings
  165. for attr in self.visitor_attributes:
  166. subs[attr] = ''.join(getattr(self, attr)).rstrip('\n')
  167. subs['encoding'] = settings.output_encoding
  168. subs['version'] = docutils.__version__
  169. return subs
  170. def assemble_parts(self):
  171. writers.Writer.assemble_parts(self)
  172. for part in self.visitor_attributes:
  173. self.parts[part] = ''.join(getattr(self, part))
  174. class HTMLTranslator(nodes.NodeVisitor):
  175. """
  176. This HTML writer has been optimized to produce visually compact
  177. lists (less vertical whitespace). HTML's mixed content models
  178. allow list items to contain "<li><p>body elements</p></li>" or
  179. "<li>just text</li>" or even "<li>text<p>and body
  180. elements</p>combined</li>", each with different effects. It would
  181. be best to stick with strict body elements in list items, but they
  182. affect vertical spacing in browsers (although they really
  183. shouldn't).
  184. Here is an outline of the optimization:
  185. - Check for and omit <p> tags in "simple" lists: list items
  186. contain either a single paragraph, a nested simple list, or a
  187. paragraph followed by a nested simple list. This means that
  188. this list can be compact:
  189. - Item 1.
  190. - Item 2.
  191. But this list cannot be compact:
  192. - Item 1.
  193. This second paragraph forces space between list items.
  194. - Item 2.
  195. - In non-list contexts, omit <p> tags on a paragraph if that
  196. paragraph is the only child of its parent (footnotes & citations
  197. are allowed a label first).
  198. - Regardless of the above, in definitions, table cells, field bodies,
  199. option descriptions, and list items, mark the first child with
  200. 'class="first"' and the last child with 'class="last"'. The stylesheet
  201. sets the margins (top & bottom respectively) to 0 for these elements.
  202. The ``no_compact_lists`` setting (``--no-compact-lists`` command-line
  203. option) disables list whitespace optimization.
  204. """
  205. xml_declaration = '<?xml version="1.0" encoding="%s" ?>\n'
  206. doctype = (
  207. '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'
  208. ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n')
  209. doctype_mathml = doctype
  210. head_prefix_template = ('<html xmlns="http://www.w3.org/1999/xhtml"'
  211. ' xml:lang="%(lang)s" lang="%(lang)s">\n<head>\n')
  212. content_type = ('<meta http-equiv="Content-Type"'
  213. ' content="text/html; charset=%s" />\n')
  214. content_type_mathml = ('<meta http-equiv="Content-Type"'
  215. ' content="application/xhtml+xml; charset=%s" />\n')
  216. generator = ('<meta name="generator" content="Docutils %s: '
  217. 'http://docutils.sourceforge.net/" />\n')
  218. # Template for the MathJax script in the header:
  219. mathjax_script = '<script type="text/javascript" src="%s"></script>\n'
  220. # The latest version of MathJax from the distributed server:
  221. # avaliable to the public under the `MathJax CDN Terms of Service`__
  222. # __http://www.mathjax.org/download/mathjax-cdn-terms-of-service/
  223. mathjax_url = ('http://cdn.mathjax.org/mathjax/latest/MathJax.js?'
  224. 'config=TeX-AMS-MML_HTMLorMML')
  225. # TODO: make this configurable:
  226. #
  227. # a) as extra option or
  228. # b) appended to math-output="MathJax"?
  229. #
  230. # If b), which delimiter/delimter-set (':', ',', ' ')?
  231. stylesheet_link = '<link rel="stylesheet" href="%s" type="text/css" />\n'
  232. embedded_stylesheet = '<style type="text/css">\n\n%s\n</style>\n'
  233. words_and_spaces = re.compile(r'\S+| +|\n')
  234. sollbruchstelle = re.compile(r'.+\W\W.+|[-?].+', re.U) # wrap point inside word
  235. lang_attribute = 'lang' # name changes to 'xml:lang' in XHTML 1.1
  236. def __init__(self, document):
  237. nodes.NodeVisitor.__init__(self, document)
  238. self.settings = settings = document.settings
  239. lcode = settings.language_code
  240. self.language = languages.get_language(lcode, document.reporter)
  241. self.meta = [self.generator % docutils.__version__]
  242. self.head_prefix = []
  243. self.html_prolog = []
  244. if settings.xml_declaration:
  245. self.head_prefix.append(self.xml_declaration
  246. % settings.output_encoding)
  247. # encoding not interpolated:
  248. self.html_prolog.append(self.xml_declaration)
  249. self.head = self.meta[:]
  250. self.stylesheet = [self.stylesheet_call(path)
  251. for path in utils.get_stylesheet_list(settings)]
  252. self.body_prefix = ['</head>\n<body>\n']
  253. # document title, subtitle display
  254. self.body_pre_docinfo = []
  255. # author, date, etc.
  256. self.docinfo = []
  257. self.body = []
  258. self.fragment = []
  259. self.body_suffix = ['</body>\n</html>\n']
  260. self.section_level = 0
  261. self.initial_header_level = int(settings.initial_header_level)
  262. self.math_output = settings.math_output.lower()
  263. # A heterogenous stack used in conjunction with the tree traversal.
  264. # Make sure that the pops correspond to the pushes:
  265. self.context = []
  266. self.topic_classes = []
  267. self.colspecs = []
  268. self.compact_p = 1
  269. self.compact_simple = False
  270. self.compact_field_list = False
  271. self.in_docinfo = False
  272. self.in_sidebar = False
  273. self.title = []
  274. self.subtitle = []
  275. self.header = []
  276. self.footer = []
  277. self.html_head = [self.content_type] # charset not interpolated
  278. self.html_title = []
  279. self.html_subtitle = []
  280. self.html_body = []
  281. self.in_document_title = 0 # len(self.body) or 0
  282. self.in_mailto = False
  283. self.author_in_authors = False
  284. self.math_header = ''
  285. def astext(self):
  286. return ''.join(self.head_prefix + self.head
  287. + self.stylesheet + self.body_prefix
  288. + self.body_pre_docinfo + self.docinfo
  289. + self.body + self.body_suffix)
  290. def encode(self, text):
  291. """Encode special characters in `text` & return."""
  292. # @@@ A codec to do these and all other HTML entities would be nice.
  293. text = str(text)
  294. return text.translate({
  295. ord('&'): '&amp;',
  296. ord('<'): '&lt;',
  297. ord('"'): '&quot;',
  298. ord('>'): '&gt;',
  299. ord('@'): '&#64;', # may thwart some address harvesters
  300. # TODO: convert non-breaking space only if needed?
  301. 0xa0: '&nbsp;'}) # non-breaking space
  302. def cloak_mailto(self, uri):
  303. """Try to hide a mailto: URL from harvesters."""
  304. # Encode "@" using a URL octet reference (see RFC 1738).
  305. # Further cloaking with HTML entities will be done in the
  306. # `attval` function.
  307. return uri.replace('@', '%40')
  308. def cloak_email(self, addr):
  309. """Try to hide the link text of a email link from harversters."""
  310. # Surround at-signs and periods with <span> tags. ("@" has
  311. # already been encoded to "&#64;" by the `encode` method.)
  312. addr = addr.replace('&#64;', '<span>&#64;</span>')
  313. addr = addr.replace('.', '<span>&#46;</span>')
  314. return addr
  315. def attval(self, text,
  316. whitespace=re.compile('[\n\r\t\v\f]')):
  317. """Cleanse, HTML encode, and return attribute value text."""
  318. encoded = self.encode(whitespace.sub(' ', text))
  319. if self.in_mailto and self.settings.cloak_email_addresses:
  320. # Cloak at-signs ("%40") and periods with HTML entities.
  321. encoded = encoded.replace('%40', '&#37;&#52;&#48;')
  322. encoded = encoded.replace('.', '&#46;')
  323. return encoded
  324. def stylesheet_call(self, path):
  325. """Return code to reference or embed stylesheet file `path`"""
  326. if self.settings.embed_stylesheet:
  327. try:
  328. content = io.FileInput(source_path=path,
  329. encoding='utf-8',
  330. handle_io_errors=False).read()
  331. self.settings.record_dependencies.add(path)
  332. except IOError as err:
  333. msg = "Cannot embed stylesheet '%s': %s." % (
  334. path, SafeString(err.strerror))
  335. self.document.reporter.error(msg)
  336. return '<--- %s --->\n' % msg
  337. return self.embedded_stylesheet % content
  338. # else link to style file:
  339. if self.settings.stylesheet_path:
  340. # adapt path relative to output (cf. config.html#stylesheet-path)
  341. path = utils.relative_path(self.settings._destination, path)
  342. return self.stylesheet_link % self.encode(path)
  343. def starttag(self, node, tagname, suffix='\n', empty=False, **attributes):
  344. """
  345. Construct and return a start tag given a node (id & class attributes
  346. are extracted), tag name, and optional attributes.
  347. """
  348. tagname = tagname.lower()
  349. prefix = []
  350. atts = {}
  351. ids = []
  352. for (name, value) in list(attributes.items()):
  353. atts[name.lower()] = value
  354. classes = node.get('classes', [])
  355. if 'class' in atts:
  356. classes.append(atts.pop('class'))
  357. # move language specification to 'lang' attribute
  358. languages = [cls for cls in classes
  359. if cls.startswith('language-')]
  360. if languages:
  361. # attribute name is 'lang' in XHTML 1.0 but 'xml:lang' in 1.1
  362. atts[self.lang_attribute] = languages[0][9:]
  363. classes.pop(classes.index(languages[0]))
  364. classes = ' '.join(classes).strip()
  365. if classes:
  366. atts['class'] = classes
  367. assert 'id' not in atts
  368. ids.extend(node.get('ids', []))
  369. if 'ids' in atts:
  370. ids.extend(atts['ids'])
  371. del atts['ids']
  372. if ids:
  373. atts['id'] = ids[0]
  374. for id in ids[1:]:
  375. # Add empty "span" elements for additional IDs. Note
  376. # that we cannot use empty "a" elements because there
  377. # may be targets inside of references, but nested "a"
  378. # elements aren't allowed in XHTML (even if they do
  379. # not all have a "href" attribute).
  380. if empty:
  381. # Empty tag. Insert target right in front of element.
  382. prefix.append('<span id="%s"></span>' % id)
  383. else:
  384. # Non-empty tag. Place the auxiliary <span> tag
  385. # *inside* the element, as the first child.
  386. suffix += '<span id="%s"></span>' % id
  387. attlist = list(atts.items())
  388. attlist.sort()
  389. parts = [tagname]
  390. for name, value in attlist:
  391. # value=None was used for boolean attributes without
  392. # value, but this isn't supported by XHTML.
  393. assert value is not None
  394. if isinstance(value, list):
  395. values = [str(v) for v in value]
  396. parts.append('%s="%s"' % (name.lower(),
  397. self.attval(' '.join(values))))
  398. else:
  399. parts.append('%s="%s"' % (name.lower(),
  400. self.attval(str(value))))
  401. if empty:
  402. infix = ' /'
  403. else:
  404. infix = ''
  405. return ''.join(prefix) + '<%s%s>' % (' '.join(parts), infix) + suffix
  406. def emptytag(self, node, tagname, suffix='\n', **attributes):
  407. """Construct and return an XML-compatible empty tag."""
  408. return self.starttag(node, tagname, suffix, empty=True, **attributes)
  409. def set_class_on_child(self, node, class_, index=0):
  410. """
  411. Set class `class_` on the visible child no. index of `node`.
  412. Do nothing if node has fewer children than `index`.
  413. """
  414. children = [n for n in node if not isinstance(n, nodes.Invisible)]
  415. try:
  416. child = children[index]
  417. except IndexError:
  418. return
  419. child['classes'].append(class_)
  420. def set_first_last(self, node):
  421. self.set_class_on_child(node, 'first', 0)
  422. self.set_class_on_child(node, 'last', -1)
  423. def visit_Text(self, node):
  424. text = node.astext()
  425. encoded = self.encode(text)
  426. if self.in_mailto and self.settings.cloak_email_addresses:
  427. encoded = self.cloak_email(encoded)
  428. self.body.append(encoded)
  429. def depart_Text(self, node):
  430. pass
  431. def visit_abbreviation(self, node):
  432. # @@@ implementation incomplete ("title" attribute)
  433. self.body.append(self.starttag(node, 'abbr', ''))
  434. def depart_abbreviation(self, node):
  435. self.body.append('</abbr>')
  436. def visit_acronym(self, node):
  437. # @@@ implementation incomplete ("title" attribute)
  438. self.body.append(self.starttag(node, 'acronym', ''))
  439. def depart_acronym(self, node):
  440. self.body.append('</acronym>')
  441. def visit_address(self, node):
  442. self.visit_docinfo_item(node, 'address', meta=False)
  443. self.body.append(self.starttag(node, 'pre', CLASS='address'))
  444. def depart_address(self, node):
  445. self.body.append('\n</pre>\n')
  446. self.depart_docinfo_item()
  447. def visit_admonition(self, node):
  448. self.body.append(self.starttag(node, 'div'))
  449. self.set_first_last(node)
  450. def depart_admonition(self, node=None):
  451. self.body.append('</div>\n')
  452. attribution_formats = {'dash': ('&mdash;', ''),
  453. 'parentheses': ('(', ')'),
  454. 'parens': ('(', ')'),
  455. 'none': ('', '')}
  456. def visit_attribution(self, node):
  457. prefix, suffix = self.attribution_formats[self.settings.attribution]
  458. self.context.append(suffix)
  459. self.body.append(
  460. self.starttag(node, 'p', prefix, CLASS='attribution'))
  461. def depart_attribution(self, node):
  462. self.body.append(self.context.pop() + '</p>\n')
  463. def visit_author(self, node):
  464. if isinstance(node.parent, nodes.authors):
  465. if self.author_in_authors:
  466. self.body.append('\n<br />')
  467. else:
  468. self.visit_docinfo_item(node, 'author')
  469. def depart_author(self, node):
  470. if isinstance(node.parent, nodes.authors):
  471. self.author_in_authors = True
  472. else:
  473. self.depart_docinfo_item()
  474. def visit_authors(self, node):
  475. self.visit_docinfo_item(node, 'authors')
  476. self.author_in_authors = False # initialize
  477. def depart_authors(self, node):
  478. self.depart_docinfo_item()
  479. def visit_block_quote(self, node):
  480. self.body.append(self.starttag(node, 'blockquote'))
  481. def depart_block_quote(self, node):
  482. self.body.append('</blockquote>\n')
  483. def check_simple_list(self, node):
  484. """Check for a simple list that can be rendered compactly."""
  485. visitor = SimpleListChecker(self.document)
  486. try:
  487. node.walk(visitor)
  488. except nodes.NodeFound:
  489. return None
  490. else:
  491. return 1
  492. def is_compactable(self, node):
  493. return ('compact' in node['classes']
  494. or (self.settings.compact_lists
  495. and 'open' not in node['classes']
  496. and (self.compact_simple
  497. or self.topic_classes == ['contents']
  498. or self.check_simple_list(node))))
  499. def visit_bullet_list(self, node):
  500. atts = {}
  501. old_compact_simple = self.compact_simple
  502. self.context.append((self.compact_simple, self.compact_p))
  503. self.compact_p = None
  504. self.compact_simple = self.is_compactable(node)
  505. if self.compact_simple and not old_compact_simple:
  506. atts['class'] = 'simple'
  507. self.body.append(self.starttag(node, 'ul', **atts))
  508. def depart_bullet_list(self, node):
  509. self.compact_simple, self.compact_p = self.context.pop()
  510. self.body.append('</ul>\n')
  511. def visit_caption(self, node):
  512. self.body.append(self.starttag(node, 'p', '', CLASS='caption'))
  513. def depart_caption(self, node):
  514. self.body.append('</p>\n')
  515. def visit_citation(self, node):
  516. self.body.append(self.starttag(node, 'table',
  517. CLASS='docutils citation',
  518. frame="void", rules="none"))
  519. self.body.append('<colgroup><col class="label" /><col /></colgroup>\n'
  520. '<tbody valign="top">\n'
  521. '<tr>')
  522. self.footnote_backrefs(node)
  523. def depart_citation(self, node):
  524. self.body.append('</td></tr>\n'
  525. '</tbody>\n</table>\n')
  526. def visit_citation_reference(self, node):
  527. href = '#' + node['refid']
  528. self.body.append(self.starttag(
  529. node, 'a', '[', CLASS='citation-reference', href=href))
  530. def depart_citation_reference(self, node):
  531. self.body.append(']</a>')
  532. def visit_classifier(self, node):
  533. self.body.append(' <span class="classifier-delimiter">:</span> ')
  534. self.body.append(self.starttag(node, 'span', '', CLASS='classifier'))
  535. def depart_classifier(self, node):
  536. self.body.append('</span>')
  537. def visit_colspec(self, node):
  538. self.colspecs.append(node)
  539. # "stubs" list is an attribute of the tgroup element:
  540. node.parent.stubs.append(node.attributes.get('stub'))
  541. def depart_colspec(self, node):
  542. pass
  543. def write_colspecs(self):
  544. width = 0
  545. for node in self.colspecs:
  546. width += node['colwidth']
  547. for node in self.colspecs:
  548. colwidth = int(node['colwidth'] * 100.0 / width + 0.5)
  549. self.body.append(self.emptytag(node, 'col',
  550. width='%i%%' % colwidth))
  551. self.colspecs = []
  552. def visit_comment(self, node,
  553. sub=re.compile('-(?=-)').sub):
  554. """Escape double-dashes in comment text."""
  555. self.body.append('<!-- %s -->\n' % sub('- ', node.astext()))
  556. # Content already processed:
  557. raise nodes.SkipNode
  558. def visit_compound(self, node):
  559. self.body.append(self.starttag(node, 'div', CLASS='compound'))
  560. if len(node) > 1:
  561. node[0]['classes'].append('compound-first')
  562. node[-1]['classes'].append('compound-last')
  563. for child in node[1:-1]:
  564. child['classes'].append('compound-middle')
  565. def depart_compound(self, node):
  566. self.body.append('</div>\n')
  567. def visit_container(self, node):
  568. self.body.append(self.starttag(node, 'div', CLASS='container'))
  569. def depart_container(self, node):
  570. self.body.append('</div>\n')
  571. def visit_contact(self, node):
  572. self.visit_docinfo_item(node, 'contact', meta=False)
  573. def depart_contact(self, node):
  574. self.depart_docinfo_item()
  575. def visit_copyright(self, node):
  576. self.visit_docinfo_item(node, 'copyright')
  577. def depart_copyright(self, node):
  578. self.depart_docinfo_item()
  579. def visit_date(self, node):
  580. self.visit_docinfo_item(node, 'date')
  581. def depart_date(self, node):
  582. self.depart_docinfo_item()
  583. def visit_decoration(self, node):
  584. pass
  585. def depart_decoration(self, node):
  586. pass
  587. def visit_definition(self, node):
  588. self.body.append('</dt>\n')
  589. self.body.append(self.starttag(node, 'dd', ''))
  590. self.set_first_last(node)
  591. def depart_definition(self, node):
  592. self.body.append('</dd>\n')
  593. def visit_definition_list(self, node):
  594. self.body.append(self.starttag(node, 'dl', CLASS='docutils'))
  595. def depart_definition_list(self, node):
  596. self.body.append('</dl>\n')
  597. def visit_definition_list_item(self, node):
  598. pass
  599. def depart_definition_list_item(self, node):
  600. pass
  601. def visit_description(self, node):
  602. self.body.append(self.starttag(node, 'td', ''))
  603. self.set_first_last(node)
  604. def depart_description(self, node):
  605. self.body.append('</td>')
  606. def visit_docinfo(self, node):
  607. self.context.append(len(self.body))
  608. self.body.append(self.starttag(node, 'table',
  609. CLASS='docinfo',
  610. frame="void", rules="none"))
  611. self.body.append('<col class="docinfo-name" />\n'
  612. '<col class="docinfo-content" />\n'
  613. '<tbody valign="top">\n')
  614. self.in_docinfo = True
  615. def depart_docinfo(self, node):
  616. self.body.append('</tbody>\n</table>\n')
  617. self.in_docinfo = False
  618. start = self.context.pop()
  619. self.docinfo = self.body[start:]
  620. self.body = []
  621. def visit_docinfo_item(self, node, name, meta=True):
  622. if meta:
  623. meta_tag = '<meta name="%s" content="%s" />\n' \
  624. % (name, self.attval(node.astext()))
  625. self.add_meta(meta_tag)
  626. self.body.append(self.starttag(node, 'tr', ''))
  627. self.body.append('<th class="docinfo-name">%s:</th>\n<td>'
  628. % self.language.labels[name])
  629. if len(node):
  630. if isinstance(node[0], nodes.Element):
  631. node[0]['classes'].append('first')
  632. if isinstance(node[-1], nodes.Element):
  633. node[-1]['classes'].append('last')
  634. def depart_docinfo_item(self):
  635. self.body.append('</td></tr>\n')
  636. def visit_doctest_block(self, node):
  637. self.body.append(self.starttag(node, 'pre', CLASS='doctest-block'))
  638. def depart_doctest_block(self, node):
  639. self.body.append('\n</pre>\n')
  640. def visit_document(self, node):
  641. self.head.append('<title>%s</title>\n'
  642. % self.encode(node.get('title', '')))
  643. def depart_document(self, node):
  644. self.head_prefix.extend([self.doctype,
  645. self.head_prefix_template %
  646. {'lang': self.settings.language_code}])
  647. self.html_prolog.append(self.doctype)
  648. self.meta.insert(0, self.content_type % self.settings.output_encoding)
  649. self.head.insert(0, self.content_type % self.settings.output_encoding)
  650. if self.math_header:
  651. self.head.append(self.math_header)
  652. # skip content-type meta tag with interpolated charset value:
  653. self.html_head.extend(self.head[1:])
  654. self.body_prefix.append(self.starttag(node, 'div', CLASS='document'))
  655. self.body_suffix.insert(0, '</div>\n')
  656. self.fragment.extend(self.body) # self.fragment is the "naked" body
  657. self.html_body.extend(self.body_prefix[1:] + self.body_pre_docinfo
  658. + self.docinfo + self.body
  659. + self.body_suffix[:-1])
  660. assert not self.context, 'len(context) = %s' % len(self.context)
  661. def visit_emphasis(self, node):
  662. self.body.append(self.starttag(node, 'em', ''))
  663. def depart_emphasis(self, node):
  664. self.body.append('</em>')
  665. def visit_entry(self, node):
  666. atts = {'class': []}
  667. if isinstance(node.parent.parent, nodes.thead):
  668. atts['class'].append('head')
  669. if node.parent.parent.parent.stubs[node.parent.column]:
  670. # "stubs" list is an attribute of the tgroup element
  671. atts['class'].append('stub')
  672. if atts['class']:
  673. tagname = 'th'
  674. atts['class'] = ' '.join(atts['class'])
  675. else:
  676. tagname = 'td'
  677. del atts['class']
  678. node.parent.column += 1
  679. if 'morerows' in node:
  680. atts['rowspan'] = node['morerows'] + 1
  681. if 'morecols' in node:
  682. atts['colspan'] = node['morecols'] + 1
  683. node.parent.column += node['morecols']
  684. self.body.append(self.starttag(node, tagname, '', **atts))
  685. self.context.append('</%s>\n' % tagname.lower())
  686. if len(node) == 0: # empty cell
  687. self.body.append('&nbsp;')
  688. self.set_first_last(node)
  689. def depart_entry(self, node):
  690. self.body.append(self.context.pop())
  691. def visit_enumerated_list(self, node):
  692. """
  693. The 'start' attribute does not conform to HTML 4.01's strict.dtd, but
  694. CSS1 doesn't help. CSS2 isn't widely enough supported yet to be
  695. usable.
  696. """
  697. atts = {}
  698. if 'start' in node:
  699. atts['start'] = node['start']
  700. if 'enumtype' in node:
  701. atts['class'] = node['enumtype']
  702. # @@@ To do: prefix, suffix. How? Change prefix/suffix to a
  703. # single "format" attribute? Use CSS2?
  704. old_compact_simple = self.compact_simple
  705. self.context.append((self.compact_simple, self.compact_p))
  706. self.compact_p = None
  707. self.compact_simple = self.is_compactable(node)
  708. if self.compact_simple and not old_compact_simple:
  709. atts['class'] = (atts.get('class', '') + ' simple').strip()
  710. self.body.append(self.starttag(node, 'ol', **atts))
  711. def depart_enumerated_list(self, node):
  712. self.compact_simple, self.compact_p = self.context.pop()
  713. self.body.append('</ol>\n')
  714. def visit_field(self, node):
  715. self.body.append(self.starttag(node, 'tr', '', CLASS='field'))
  716. def depart_field(self, node):
  717. self.body.append('</tr>\n')
  718. def visit_field_body(self, node):
  719. self.body.append(self.starttag(node, 'td', '', CLASS='field-body'))
  720. self.set_class_on_child(node, 'first', 0)
  721. field = node.parent
  722. if (self.compact_field_list or
  723. isinstance(field.parent, nodes.docinfo) or
  724. field.parent.index(field) == len(field.parent) - 1):
  725. # If we are in a compact list, the docinfo, or if this is
  726. # the last field of the field list, do not add vertical
  727. # space after last element.
  728. self.set_class_on_child(node, 'last', -1)
  729. def depart_field_body(self, node):
  730. self.body.append('</td>\n')
  731. def visit_field_list(self, node):
  732. self.context.append((self.compact_field_list, self.compact_p))
  733. self.compact_p = None
  734. if 'compact' in node['classes']:
  735. self.compact_field_list = True
  736. elif (self.settings.compact_field_lists
  737. and 'open' not in node['classes']):
  738. self.compact_field_list = True
  739. if self.compact_field_list:
  740. for field in node:
  741. field_body = field[-1]
  742. assert isinstance(field_body, nodes.field_body)
  743. children = [n for n in field_body
  744. if not isinstance(n, nodes.Invisible)]
  745. if not (len(children) == 0 or
  746. len(children) == 1 and
  747. isinstance(children[0],
  748. (nodes.paragraph, nodes.line_block))):
  749. self.compact_field_list = False
  750. break
  751. self.body.append(self.starttag(node, 'table', frame='void',
  752. rules='none',
  753. CLASS='docutils field-list'))
  754. self.body.append('<col class="field-name" />\n'
  755. '<col class="field-body" />\n'
  756. '<tbody valign="top">\n')
  757. def depart_field_list(self, node):
  758. self.body.append('</tbody>\n</table>\n')
  759. self.compact_field_list, self.compact_p = self.context.pop()
  760. def visit_field_name(self, node):
  761. atts = {}
  762. if self.in_docinfo:
  763. atts['class'] = 'docinfo-name'
  764. else:
  765. atts['class'] = 'field-name'
  766. if ( self.settings.field_name_limit
  767. and len(node.astext()) > self.settings.field_name_limit):
  768. atts['colspan'] = 2
  769. self.context.append('</tr>\n'
  770. + self.starttag(node.parent, 'tr', '')
  771. + '<td>&nbsp;</td>')
  772. else:
  773. self.context.append('')
  774. self.body.append(self.starttag(node, 'th', '', **atts))
  775. def depart_field_name(self, node):
  776. self.body.append(':</th>')
  777. self.body.append(self.context.pop())
  778. def visit_figure(self, node):
  779. atts = {'class': 'figure'}
  780. if node.get('width'):
  781. atts['style'] = 'width: %s' % node['width']
  782. if node.get('align'):
  783. atts['class'] += " align-" + node['align']
  784. self.body.append(self.starttag(node, 'div', **atts))
  785. def depart_figure(self, node):
  786. self.body.append('</div>\n')
  787. def visit_footer(self, node):
  788. self.context.append(len(self.body))
  789. def depart_footer(self, node):
  790. start = self.context.pop()
  791. footer = [self.starttag(node, 'div', CLASS='footer'),
  792. '<hr class="footer" />\n']
  793. footer.extend(self.body[start:])
  794. footer.append('\n</div>\n')
  795. self.footer.extend(footer)
  796. self.body_suffix[:0] = footer
  797. del self.body[start:]
  798. def visit_footnote(self, node):
  799. self.body.append(self.starttag(node, 'table',
  800. CLASS='docutils footnote',
  801. frame="void", rules="none"))
  802. self.body.append('<colgroup><col class="label" /><col /></colgroup>\n'
  803. '<tbody valign="top">\n'
  804. '<tr>')
  805. self.footnote_backrefs(node)
  806. def footnote_backrefs(self, node):
  807. backlinks = []
  808. backrefs = node['backrefs']
  809. if self.settings.footnote_backlinks and backrefs:
  810. if len(backrefs) == 1:
  811. self.context.append('')
  812. self.context.append('</a>')
  813. self.context.append('<a class="fn-backref" href="#%s">'
  814. % backrefs[0])
  815. else:
  816. i = 1
  817. for backref in backrefs:
  818. backlinks.append('<a class="fn-backref" href="#%s">%s</a>'
  819. % (backref, i))
  820. i += 1
  821. self.context.append('<em>(%s)</em> ' % ', '.join(backlinks))
  822. self.context += ['', '']
  823. else:
  824. self.context.append('')
  825. self.context += ['', '']
  826. # If the node does not only consist of a label.
  827. if len(node) > 1:
  828. # If there are preceding backlinks, we do not set class
  829. # 'first', because we need to retain the top-margin.
  830. if not backlinks:
  831. node[1]['classes'].append('first')
  832. node[-1]['classes'].append('last')
  833. def depart_footnote(self, node):
  834. self.body.append('</td></tr>\n'
  835. '</tbody>\n</table>\n')
  836. def visit_footnote_reference(self, node):
  837. href = '#' + node['refid']
  838. format = self.settings.footnote_references
  839. if format == 'brackets':
  840. suffix = '['
  841. self.context.append(']')
  842. else:
  843. assert format == 'superscript'
  844. suffix = '<sup>'
  845. self.context.append('</sup>')
  846. self.body.append(self.starttag(node, 'a', suffix,
  847. CLASS='footnote-reference', href=href))
  848. def depart_footnote_reference(self, node):
  849. self.body.append(self.context.pop() + '</a>')
  850. def visit_generated(self, node):
  851. pass
  852. def depart_generated(self, node):
  853. pass
  854. def visit_header(self, node):
  855. self.context.append(len(self.body))
  856. def depart_header(self, node):
  857. start = self.context.pop()
  858. header = [self.starttag(node, 'div', CLASS='header')]
  859. header.extend(self.body[start:])
  860. header.append('\n<hr class="header"/>\n</div>\n')
  861. self.body_prefix.extend(header)
  862. self.header.extend(header)
  863. del self.body[start:]
  864. def visit_image(self, node):
  865. atts = {}
  866. uri = node['uri']
  867. # place SVG and SWF images in an <object> element
  868. types = {'.svg': 'image/svg+xml',
  869. '.swf': 'application/x-shockwave-flash'}
  870. ext = os.path.splitext(uri)[1].lower()
  871. if ext in ('.svg', '.swf'):
  872. atts['data'] = uri
  873. atts['type'] = types[ext]
  874. else:
  875. atts['src'] = uri
  876. atts['alt'] = node.get('alt', uri)
  877. # image size
  878. if 'width' in node:
  879. atts['width'] = node['width']
  880. if 'height' in node:
  881. atts['height'] = node['height']
  882. if 'scale' in node:
  883. if (PIL and not ('width' in node and 'height' in node)
  884. and self.settings.file_insertion_enabled):
  885. imagepath = urllib.request.url2pathname(uri)
  886. try:
  887. img = PIL.Image.open(
  888. imagepath.encode(sys.getfilesystemencoding()))
  889. except (IOError, UnicodeEncodeError):
  890. pass # TODO: warn?
  891. else:
  892. self.settings.record_dependencies.add(
  893. imagepath.replace('\\', '/'))
  894. if 'width' not in atts:
  895. atts['width'] = str(img.size[0])
  896. if 'height' not in atts:
  897. atts['height'] = str(img.size[1])
  898. del img
  899. for att_name in 'width', 'height':
  900. if att_name in atts:
  901. match = re.match(r'([0-9.]+)(\S*)$', atts[att_name])
  902. assert match
  903. atts[att_name] = '%s%s' % (
  904. float(match.group(1)) * (float(node['scale']) / 100),
  905. match.group(2))
  906. style = []
  907. for att_name in 'width', 'height':
  908. if att_name in atts:
  909. if re.match(r'^[0-9.]+$', atts[att_name]):
  910. # Interpret unitless values as pixels.
  911. atts[att_name] += 'px'
  912. style.append('%s: %s;' % (att_name, atts[att_name]))
  913. del atts[att_name]
  914. if style:
  915. atts['style'] = ' '.join(style)
  916. if (isinstance(node.parent, nodes.TextElement) or
  917. (isinstance(node.parent, nodes.reference) and
  918. not isinstance(node.parent.parent, nodes.TextElement))):
  919. # Inline context or surrounded by <a>...</a>.
  920. suffix = ''
  921. else:
  922. suffix = '\n'
  923. if 'align' in node:
  924. atts['class'] = 'align-%s' % node['align']
  925. self.context.append('')
  926. if ext in ('.svg', '.swf'): # place in an object element,
  927. # do NOT use an empty tag: incorrect rendering in browsers
  928. self.body.append(self.starttag(node, 'object', suffix, **atts) +
  929. node.get('alt', uri) + '</object>' + suffix)
  930. else:
  931. self.body.append(self.emptytag(node, 'img', suffix, **atts))
  932. def depart_image(self, node):
  933. self.body.append(self.context.pop())
  934. def visit_inline(self, node):
  935. self.body.append(self.starttag(node, 'span', ''))
  936. def depart_inline(self, node):
  937. self.body.append('</span>')
  938. def visit_label(self, node):
  939. # Context added in footnote_backrefs.
  940. self.body.append(self.starttag(node, 'td', '%s[' % self.context.pop(),
  941. CLASS='label'))
  942. def depart_label(self, node):
  943. # Context added in footnote_backrefs.
  944. self.body.append(']%s</td><td>%s' % (self.context.pop(), self.context.pop()))
  945. def visit_legend(self, node):
  946. self.body.append(self.starttag(node, 'div', CLASS='legend'))
  947. def depart_legend(self, node):
  948. self.body.append('</div>\n')
  949. def visit_line(self, node):
  950. self.body.append(self.starttag(node, 'div', suffix='', CLASS='line'))
  951. if not len(node):
  952. self.body.append('<br />')
  953. def depart_line(self, node):
  954. self.body.append('</div>\n')
  955. def visit_line_block(self, node):
  956. self.body.append(self.starttag(node, 'div', CLASS='line-block'))
  957. def depart_line_block(self, node):
  958. self.body.append('</div>\n')
  959. def visit_list_item(self, node):
  960. self.body.append(self.starttag(node, 'li', ''))
  961. if len(node):
  962. node[0]['classes'].append('first')
  963. def depart_list_item(self, node):
  964. self.body.append('</li>\n')
  965. def visit_literal(self, node):
  966. """Process text to prevent tokens from wrapping."""
  967. self.body.append(
  968. self.starttag(node, 'tt', '', CLASS='docutils literal'))
  969. text = node.astext()
  970. for token in self.words_and_spaces.findall(text):
  971. if token.strip():
  972. # Protect text like "--an-option" and the regular expression
  973. # ``[+]?(\d+(\.\d*)?|\.\d+)`` from bad line wrapping
  974. if self.sollbruchstelle.search(token):
  975. self.body.append('<span class="pre">%s</span>'
  976. % self.encode(token))
  977. else:
  978. self.body.append(self.encode(token))
  979. elif token in ('\n', ' '):
  980. # Allow breaks at whitespace:
  981. self.body.append(token)
  982. else:
  983. # Protect runs of multiple spaces; the last space can wrap:
  984. self.body.append('&nbsp;' * (len(token) - 1) + ' ')
  985. self.body.append('</tt>')
  986. # Content already processed:
  987. raise nodes.SkipNode
  988. def visit_literal_block(self, node):
  989. self.body.append(self.starttag(node, 'pre', CLASS='literal-block'))
  990. def depart_literal_block(self, node):
  991. self.body.append('\n</pre>\n')
  992. def visit_math(self, node, math_env=''):
  993. # As there is no native HTML math support, we provide alternatives:
  994. # LaTeX and MathJax math_output modes simply wrap the content,
  995. # HTML and MathML math_output modes also convert the math_code.
  996. # If the method is called from visit_math_block(), math_env != ''.
  997. #
  998. # HTML container
  999. tags = {# math_output: (block, inline, class-arguments)
  1000. 'mathml': ('div', '', ''),
  1001. 'html': ('div', 'span', 'formula'),
  1002. 'mathjax': ('div', 'span', 'math'),
  1003. 'latex': ('pre', 'tt', 'math'),
  1004. }
  1005. tag = tags[self.math_output][math_env == '']
  1006. clsarg = tags[self.math_output][2]
  1007. # LaTeX container
  1008. wrappers = {# math_mode: (inline, block)
  1009. 'mathml': (None, None),
  1010. 'html': ('$%s$', '\\begin{%s}\n%s\n\\end{%s}'),
  1011. 'mathjax': ('\(%s\)', '\\begin{%s}\n%s\n\\end{%s}'),
  1012. 'latex': (None, None),
  1013. }
  1014. wrapper = wrappers[self.math_output][math_env != '']
  1015. # get and wrap content
  1016. math_code = node.astext().translate(unichar2tex.uni2tex_table)
  1017. if wrapper and math_env:
  1018. math_code = wrapper % (math_env, math_code, math_env)
  1019. elif wrapper:
  1020. math_code = wrapper % math_code
  1021. # settings and conversion
  1022. if self.math_output in ('latex', 'mathjax'):
  1023. math_code = self.encode(math_code)
  1024. if self.math_output == 'mathjax':
  1025. self.math_header = self.mathjax_script % self.mathjax_url
  1026. elif self.math_output == 'html':
  1027. math_code = math2html(math_code)
  1028. elif self.math_output == 'mathml':
  1029. self.doctype = self.doctype_mathml
  1030. self.content_type = self.content_type_mathml
  1031. try:
  1032. mathml_tree = parse_latex_math(math_code, inline=not(math_env))
  1033. math_code = ''.join(mathml_tree.xml())
  1034. except SyntaxError as err:
  1035. err_node = self.document.reporter.error(err, base_node=node)
  1036. self.visit_system_message(err_node)
  1037. self.body.append(self.starttag(node, 'p'))
  1038. self.body.append(','.join(err.args))
  1039. self.body.append('</p>\n')
  1040. self.body.append(self.starttag(node, 'pre',
  1041. CLASS='literal-block'))
  1042. self.body.append(self.encode(math_code))
  1043. self.body.append('\n</pre>\n')
  1044. self.depart_system_message(err_node)
  1045. raise nodes.SkipNode
  1046. # append to document body
  1047. if tag:
  1048. self.body.append(self.starttag(node, tag, CLASS=clsarg))
  1049. self.body.append(math_code)
  1050. if math_env:
  1051. self.body.append('\n')
  1052. if tag:
  1053. self.body.append('</%s>\n' % tag)
  1054. # Content already processed:
  1055. raise nodes.SkipNode
  1056. def depart_math(self, node):
  1057. pass # never reached
  1058. def visit_math_block(self, node):
  1059. # print node.astext().encode('utf8')
  1060. math_env = pick_math_environment(node.astext())
  1061. self.visit_math(node, math_env=math_env)
  1062. def depart_math_block(self, node):
  1063. pass # never reached
  1064. def visit_meta(self, node):
  1065. meta = self.emptytag(node, 'meta', **node.non_default_attributes())
  1066. self.add_meta(meta)
  1067. def depart_meta(self, node):
  1068. pass
  1069. def add_meta(self, tag):
  1070. self.meta.append(tag)
  1071. self.head.append(tag)
  1072. def visit_option(self, node):
  1073. if self.context[-1]:
  1074. self.body.append(', ')
  1075. self.body.append(self.starttag(node, 'span', '', CLASS='option'))
  1076. def depart_option(self, node):
  1077. self.body.append('</span>')
  1078. self.context[-1] += 1
  1079. def visit_option_argument(self, node):
  1080. self.body.append(node.get('delimiter', ' '))
  1081. self.body.append(self.starttag(node, 'var', ''))
  1082. def depart_option_argument(self, node):
  1083. self.body.append('</var>')
  1084. def visit_option_group(self, node):
  1085. atts = {}
  1086. if ( self.settings.option_limit
  1087. and len(node.astext()) > self.settings.option_limit):
  1088. atts['colspan'] = 2
  1089. self.context.append('</tr>\n<tr><td>&nbsp;</td>')
  1090. else:
  1091. self.context.append('')
  1092. self.body.append(
  1093. self.starttag(node, 'td', CLASS='option-group', **atts))
  1094. self.body.append('<kbd>')
  1095. self.context.append(0) # count number of options
  1096. def depart_option_group(self, node):
  1097. self.context.pop()
  1098. self.body.append('</kbd></td>\n')
  1099. self.body.append(self.context.pop())
  1100. def visit_option_list(self, node):
  1101. self.body.append(
  1102. self.starttag(node, 'table', CLASS='docutils option-list',
  1103. frame="void", rules="none"))
  1104. self.body.append('<col class="option" />\n'
  1105. '<col class="description" />\n'
  1106. '<tbody valign="top">\n')
  1107. def depart_option_list(self, node):
  1108. self.body.append('</tbody>\n</table>\n')
  1109. def visit_option_list_item(self, node):
  1110. self.body.append(self.starttag(node, 'tr', ''))
  1111. def depart_option_list_item(self, node):
  1112. self.body.append('</tr>\n')
  1113. def visit_option_string(self, node):
  1114. pass
  1115. def depart_option_string(self, node):
  1116. pass
  1117. def visit_organization(self, node):
  1118. self.visit_docinfo_item(node, 'organization')
  1119. def depart_organization(self, node):
  1120. self.depart_docinfo_item()
  1121. def should_be_compact_paragraph(self, node):
  1122. """
  1123. Determine if the <p> tags around paragraph ``node`` can be omitted.
  1124. """
  1125. if (isinstance(node.parent, nodes.document) or
  1126. isinstance(node.parent, nodes.compound)):
  1127. # Never compact paragraphs in document or compound.
  1128. return 0
  1129. for key, value in node.attlist():
  1130. if (node.is_not_default(key) and
  1131. not (key == 'classes' and value in
  1132. ([], ['first'], ['last'], ['first', 'last']))):
  1133. # Attribute which needs to survive.
  1134. return 0
  1135. first = isinstance(node.parent[0], nodes.label) # skip label
  1136. for child in node.parent.children[first:]:
  1137. # only first paragraph can be compact
  1138. if isinstance(child, nodes.Invisible):
  1139. continue
  1140. if child is node:
  1141. break
  1142. return 0
  1143. parent_length = len([n for n in node.parent if not isinstance(
  1144. n, (nodes.Invisible, nodes.label))])
  1145. if ( self.compact_simple
  1146. or self.compact_field_list
  1147. or self.compact_p and parent_length == 1):
  1148. return 1
  1149. return 0
  1150. def visit_paragraph(self, node):
  1151. if self.should_be_compact_paragraph(node):
  1152. self.context.append('')
  1153. else:
  1154. self.body.append(self.starttag(node, 'p', ''))
  1155. self.context.append('</p>\n')
  1156. def depart_paragraph(self, node):
  1157. self.body.append(self.context.pop())
  1158. def visit_problematic(self, node):
  1159. if node.hasattr('refid'):
  1160. self.body.append('<a href="#%s">' % node['refid'])
  1161. self.context.append('</a>')
  1162. else:
  1163. self.context.append('')
  1164. self.body.append(self.starttag(node, 'span', '', CLASS='problematic'))
  1165. def depart_problematic(self, node):
  1166. self.body.append('</span>')
  1167. self.body.append(self.context.pop())
  1168. def visit_raw(self, node):
  1169. if 'html' in node.get('format', '').split():
  1170. t = isinstance(node.parent, nodes.TextElement) and 'span' or 'div'
  1171. if node['classes']:
  1172. self.body.append(self.starttag(node, t, suffix=''))
  1173. self.body.append(node.astext())
  1174. if node['classes']:
  1175. self.body.append('</%s>' % t)
  1176. # Keep non-HTML raw text out of output:
  1177. raise nodes.SkipNode
  1178. def visit_reference(self, node):
  1179. atts = {'class': 'reference'}
  1180. if 'refuri' in node:
  1181. atts['href'] = node['refuri']
  1182. if ( self.settings.cloak_email_addresses
  1183. and atts['href'].startswith('mailto:')):
  1184. atts['href'] = self.cloak_mailto(atts['href'])
  1185. self.in_mailto = True
  1186. atts['class'] += ' external'
  1187. else:
  1188. assert 'refid' in node, \
  1189. 'References must have "refuri" or "refid" attribute.'
  1190. atts['href'] = '#' + node['refid']
  1191. atts['class'] += ' internal'
  1192. if not isinstance(node.parent, nodes.TextElement):
  1193. assert len(node) == 1 and isinstance(node[0], nodes.image)
  1194. atts['class'] += ' image-reference'
  1195. self.body.append(self.starttag(node, 'a', '', **atts))
  1196. def depart_reference(self, node):
  1197. self.body.append('</a>')
  1198. if not isinstance(node.parent, nodes.TextElement):
  1199. self.body.append('\n')
  1200. self.in_mailto = False
  1201. def visit_revision(self, node):
  1202. self.visit_docinfo_item(node, 'revision', meta=False)
  1203. def depart_revision(self, node):
  1204. self.depart_docinfo_item()
  1205. def visit_row(self, node):
  1206. self.body.append(self.starttag(node, 'tr', ''))
  1207. node.column = 0
  1208. def depart_row(self, node):
  1209. self.body.append('</tr>\n')
  1210. def visit_rubric(self, node):
  1211. self.body.append(self.starttag(node, 'p', '', CLASS='rubric'))
  1212. def depart_rubric(self, node):
  1213. self.body.append('</p>\n')
  1214. def visit_section(self, node):
  1215. self.section_level += 1
  1216. self.body.append(
  1217. self.starttag(node, 'div', CLASS='section'))
  1218. def depart_section(self, node):
  1219. self.section_level -= 1
  1220. self.body.append('</div>\n')
  1221. def visit_sidebar(self, node):
  1222. self.body.append(
  1223. self.starttag(node, 'div', CLASS='sidebar'))
  1224. self.set_first_last(node)
  1225. self.in_sidebar = True
  1226. def depart_sidebar(self, node):
  1227. self.body.append('</div>\n')
  1228. self.in_sidebar = False
  1229. def visit_status(self, node):
  1230. self.visit_docinfo_item(node, 'status', meta=False)
  1231. def depart_status(self, node):
  1232. self.depart_docinfo_item()
  1233. def visit_strong(self, node):
  1234. self.body.append(self.starttag(node, 'strong', ''))
  1235. def depart_strong(self, node):
  1236. self.body.append('</strong>')
  1237. def visit_subscript(self, node):
  1238. self.body.append(self.starttag(node, 'sub', ''))
  1239. def depart_subscript(self, node):
  1240. self.body.append('</sub>')
  1241. def visit_substitution_definition(self, node):
  1242. """Internal only."""
  1243. raise nodes.SkipNode
  1244. def visit_substitution_reference(self, node):
  1245. self.unimplemented_visit(node)
  1246. def visit_subtitle(self, node):
  1247. if isinstance(node.parent, nodes.sidebar):
  1248. self.body.append(self.starttag(node, 'p', '',
  1249. CLASS='sidebar-subtitle'))
  1250. self.context.append('</p>\n')
  1251. elif isinstance(node.parent, nodes.document):
  1252. self.body.append(self.starttag(node, 'h2', '', CLASS='subtitle'))
  1253. self.context.append('</h2>\n')
  1254. self.in_document_title = len(self.body)
  1255. elif isinstance(node.parent, nodes.section):
  1256. tag = 'h%s' % (self.section_level + self.initial_header_level - 1)
  1257. self.body.append(
  1258. self.starttag(node, tag, '', CLASS='section-subtitle') +
  1259. self.starttag({}, 'span', '', CLASS='section-subtitle'))
  1260. self.context.append('</span></%s>\n' % tag)
  1261. def depart_subtitle(self, node):
  1262. self.body.append(self.context.pop())
  1263. if self.in_document_title:
  1264. self.subtitle = self.body[self.in_document_title:-1]
  1265. self.in_document_title = 0
  1266. self.body_pre_docinfo.extend(self.body)
  1267. self.html_subtitle.extend(self.body)
  1268. del self.body[:]
  1269. def visit_superscript(self, node):
  1270. self.body.append(self.starttag(node, 'sup', ''))
  1271. def depart_superscript(self, node):
  1272. self.body.append('</sup>')
  1273. def visit_system_message(self, node):
  1274. self.body.append(self.starttag(node, 'div', CLASS='system-message'))
  1275. self.body.append('<p class="system-message-title">')
  1276. backref_text = ''
  1277. if len(node['backrefs']):
  1278. backrefs = node['backrefs']
  1279. if len(backrefs) == 1:
  1280. backref_text = ('; <em><a href="#%s">backlink</a></em>'
  1281. % backrefs[0])
  1282. else:
  1283. i = 1
  1284. backlinks = []
  1285. for backref in backrefs:
  1286. backlinks.append('<a href="#%s">%s</a>' % (backref, i))
  1287. i += 1
  1288. backref_text = ('; <em>backlinks: %s</em>'
  1289. % ', '.join(backlinks))
  1290. if node.hasattr('line'):
  1291. line = ', line %s' % node['line']
  1292. else:
  1293. line = ''
  1294. self.body.append('System Message: %s/%s '
  1295. '(<tt class="docutils">%s</tt>%s)%s</p>\n'
  1296. % (node['type'], node['level'],
  1297. self.encode(node['source']), line, backref_text))
  1298. def depart_system_message(self, node):
  1299. self.body.append('</div>\n')
  1300. def visit_table(self, node):
  1301. classes = ' '.join(['docutils', self.settings.table_style]).strip()
  1302. self.body.append(
  1303. self.starttag(node, 'table', CLASS=classes, border="1"))
  1304. def depart_table(self, node):
  1305. self.body.append('</table>\n')
  1306. def visit_target(self, node):
  1307. if not ('refuri' in node or 'refid' in node
  1308. or 'refname' in node):
  1309. self.body.append(self.starttag(node, 'span', '', CLASS='target'))
  1310. self.context.append('</span>')
  1311. else:
  1312. self.context.append('')
  1313. def depart_target(self, node):
  1314. self.body.append(self.context.pop())
  1315. def visit_tbody(self, node):
  1316. self.write_colspecs()
  1317. self.body.append(self.context.pop()) # '</colgroup>\n' or ''
  1318. self.body.append(self.starttag(node, 'tbody', valign='top'))
  1319. def depart_tbody(self, node):
  1320. self.body.append('</tbody>\n')
  1321. def visit_term(self, node):
  1322. self.body.append(self.starttag(node, 'dt', ''))
  1323. def depart_term(self, node):
  1324. """
  1325. Leave the end tag to `self.visit_definition()`, in case there's a
  1326. classifier.
  1327. """
  1328. pass
  1329. def visit_tgroup(self, node):
  1330. # Mozilla needs <colgroup>:
  1331. self.body.append(self.starttag(node, 'colgroup'))
  1332. # Appended by thead or tbody:
  1333. self.context.append('</colgroup>\n')
  1334. node.stubs = []
  1335. def depart_tgroup(self, node):
  1336. pass
  1337. def visit_thead(self, node):
  1338. self.write_colspecs()
  1339. self.body.append(self.context.pop()) # '</colgroup>\n'
  1340. # There may or may not be a <thead>; this is for <tbody> to use:
  1341. self.context.append('')
  1342. self.body.append(self.starttag(node, 'thead', valign='bottom'))
  1343. def depart_thead(self, node):
  1344. self.body.append('</thead>\n')
  1345. def visit_title(self, node):
  1346. """Only 6 section levels are supported by HTML."""
  1347. check_id = 0 # TODO: is this a bool (False) or a counter?
  1348. close_tag = '</p>\n'
  1349. if isinstance(node.parent, nodes.topic):
  1350. self.body.append(
  1351. self.starttag(node, 'p', '', CLASS='topic-title first'))
  1352. elif isinstance(node.parent, nodes.sidebar):
  1353. self.body.append(
  1354. self.starttag(node, 'p', '', CLASS='sidebar-title'))
  1355. elif isinstance(node.parent, nodes.Admonition):
  1356. self.body.append(
  1357. self.starttag(node, 'p', '', CLASS='admonition-title'))
  1358. elif isinstance(node.parent, nodes.table):
  1359. self.body.append(
  1360. self.starttag(node, 'caption', ''))
  1361. close_tag = '</caption>\n'
  1362. elif isinstance(node.parent, nodes.document):
  1363. self.body.append(self.starttag(node, 'h1', '', CLASS='title'))
  1364. close_tag = '</h1>\n'
  1365. self.in_document_title = len(self.body)
  1366. else:
  1367. assert isinstance(node.parent, nodes.section)
  1368. h_level = self.section_level + self.initial_header_level - 1
  1369. atts = {}
  1370. if (len(node.parent) >= 2 and
  1371. isinstance(node.parent[1], nodes.subtitle)):
  1372. atts['CLASS'] = 'with-subtitle'
  1373. self.body.append(
  1374. self.starttag(node, 'h%s' % h_level, '', **atts))
  1375. atts = {}
  1376. if node.hasattr('refid'):
  1377. atts['class'] = 'toc-backref'
  1378. atts['href'] = '#' + node['refid']
  1379. if atts:
  1380. self.body.append(self.starttag({}, 'a', '', **atts))
  1381. close_tag = '</a></h%s>\n' % (h_level)
  1382. else:
  1383. close_tag = '</h%s>\n' % (h_level)
  1384. self.context.append(close_tag)
  1385. def depart_title(self, node):
  1386. self.body.append(self.context.pop())
  1387. if self.in_document_title:
  1388. self.title = self.body[self.in_document_title:-1]
  1389. self.in_document_title = 0
  1390. self.body_pre_docinfo.extend(self.body)
  1391. self.html_title.extend(self.body)
  1392. del self.body[:]
  1393. def visit_title_reference(self, node):
  1394. self.body.append(self.starttag(node, 'cite', ''))
  1395. def depart_title_reference(self, node):
  1396. self.body.append('</cite>')
  1397. def visit_topic(self, node):
  1398. self.body.append(self.starttag(node, 'div', CLASS='topic'))
  1399. self.topic_classes = node['classes']
  1400. def depart_topic(self, node):
  1401. self.body.append('</div>\n')
  1402. self.topic_classes = []
  1403. def visit_transition(self, node):
  1404. self.body.append(self.emptytag(node, 'hr', CLASS='docutils'))
  1405. def depart_transition(self, node):
  1406. pass
  1407. def visit_version(self, node):
  1408. self.visit_docinfo_item(node, 'version', meta=False)
  1409. def depart_version(self, node):
  1410. self.depart_docinfo_item()
  1411. def unimplemented_visit(self, node):
  1412. raise NotImplementedError('visiting unimplemented node type: %s'
  1413. % node.__class__.__name__)
  1414. class SimpleListChecker(nodes.GenericNodeVisitor):
  1415. """
  1416. Raise `nodes.NodeFound` if non-simple list item is encountered.
  1417. Here "simple" means a list item containing nothing other than a single
  1418. paragraph, a simple list, or a paragraph followed by a simple list.
  1419. """
  1420. def default_visit(self, node):
  1421. raise nodes.NodeFound
  1422. def visit_bullet_list(self, node):
  1423. pass
  1424. def visit_enumerated_list(self, node):
  1425. pass
  1426. def visit_list_item(self, node):
  1427. children = []
  1428. for child in node.children:
  1429. if not isinstance(child, nodes.Invisible):
  1430. children.append(child)
  1431. if (children and isinstance(children[0], nodes.paragraph)
  1432. and (isinstance(children[-1], nodes.bullet_list)
  1433. or isinstance(children[-1], nodes.enumerated_list))):
  1434. children.pop()
  1435. if len(children) <= 1:
  1436. return
  1437. else:
  1438. raise nodes.NodeFound
  1439. def visit_paragraph(self, node):
  1440. raise nodes.SkipNode
  1441. def invisible_visit(self, node):
  1442. """Invisible nodes should be ignored."""
  1443. raise nodes.SkipNode
  1444. visit_comment = invisible_visit
  1445. visit_substitution_definition = invisible_visit
  1446. visit_target = invisible_visit
  1447. visit_pending = invisible_visit