Esempio n. 1
0
 def visit_title(self, node):
     # type: (nodes.Node) -> None
     BaseTranslator.visit_title(self, node)
     self.add_secnumber(node)
     self.add_fignumber(node.parent)
     if isinstance(node.parent, nodes.table):
         self.body.append('<span class="caption-text">')
Esempio n. 2
0
 def visit_caption(self, node):
     # type: (nodes.Node) -> None
     if isinstance(node.parent, nodes.container) and node.parent.get('literal_block'):
         self.body.append('<div class="code-block-caption">')
     else:
         BaseTranslator.visit_caption(self, node)
     self.add_fignumber(node.parent)
     self.body.append(self.starttag(node, 'span', '', CLASS='caption-text'))
Esempio n. 3
0
    def visit_literal_block(self, node):
        # type: (nodes.Node) -> None
        if node.rawsource != node.astext():
            # most probably a parsed-literal block -- don't highlight
            return BaseTranslator.visit_literal_block(self, node)
        lang = self.highlightlang
        linenos = node.rawsource.count('\n') >= \
            self.highlightlinenothreshold - 1
        highlight_args = node.get('highlight_args', {})
        if 'language' in node:
            # code-block directives
            lang = node['language']
            highlight_args['force'] = True
        if 'linenos' in node:
            linenos = node['linenos']
        if lang is self.highlightlang_base:
            # only pass highlighter options for original language
            opts = self.highlightopts
        else:
            opts = {}

        highlighted = self.highlighter.highlight_block(
            node.rawsource, lang, opts=opts, linenos=linenos,
            location=(self.builder.current_docname, node.line), **highlight_args
        )
        starttag = self.starttag(node, 'div', suffix='',
                                 CLASS='highlight-%s' % lang)
        self.body.append(starttag + highlighted + '</div>\n')
        raise nodes.SkipNode
Esempio n. 4
0
    def depart_caption(self, node):
        # type: (nodes.Node) -> None
        self.body.append('</span>')

        # append permalink if available
        if isinstance(node.parent, nodes.container) and node.parent.get('literal_block'):
            self.add_permalink_ref(node.parent, _('Permalink to this code'))
        elif isinstance(node.parent, nodes.figure):
            image_nodes = node.parent.traverse(nodes.image)
            target_node = image_nodes and image_nodes[0] or node.parent
            self.add_permalink_ref(target_node, _('Permalink to this image'))
        elif node.parent.get('toctree'):
            self.add_permalink_ref(node.parent.parent, _('Permalink to this toctree'))

        if isinstance(node.parent, nodes.container) and node.parent.get('literal_block'):
            self.body.append('</div>\n')
        else:
            BaseTranslator.depart_caption(self, node)
Esempio n. 5
0
 def __init__(self, builder, *args, **kwds):
     # type: (StandaloneHTMLBuilder, Any, Any) -> None
     BaseTranslator.__init__(self, *args, **kwds)
     self.highlighter = builder.highlighter
     self.builder = builder
     self.docnames = [builder.current_docname]  # for singlehtml builder
     self.manpages_url = builder.config.manpages_url
     self.protect_literal_text = 0
     self.permalink_text = builder.config.html_add_permalinks
     # support backwards-compatible setting to a bool
     if not isinstance(self.permalink_text, string_types):
         self.permalink_text = self.permalink_text and u'\u00B6' or ''
     self.permalink_text = self.encode(self.permalink_text)
     self.secnumber_suffix = builder.config.html_secnumber_suffix
     self.param_separator = ''
     self.optional_param_level = 0
     self._table_row_index = 0
     self.required_params_left = 0
Esempio n. 6
0
 def should_be_compact_paragraph(self, node):
     # type: (nodes.Node) -> bool
     """Determine if the <p> tags around paragraph can be omitted."""
     if isinstance(node.parent, addnodes.desc_content):
         # Never compact desc_content items.
         return False
     if isinstance(node.parent, addnodes.versionmodified):
         # Never compact versionmodified nodes.
         return False
     return BaseTranslator.should_be_compact_paragraph(self, node)
Esempio n. 7
0
    def depart_title(self, node):
        # type: (nodes.Node) -> None
        close_tag = self.context[-1]
        if (self.permalink_text and self.builder.add_permalinks and
           node.parent.hasattr('ids') and node.parent['ids']):
            # add permalink anchor
            if close_tag.startswith('</h'):
                self.add_permalink_ref(node.parent, _('Permalink to this headline'))
            elif close_tag.startswith('</a></h'):
                self.body.append(u'</a><a class="headerlink" href="#%s" ' %
                                 node.parent['ids'][0] +
                                 u'title="%s">%s' % (
                                     _('Permalink to this headline'),
                                     self.permalink_text))
            elif isinstance(node.parent, nodes.table):
                self.body.append('</span>')
                self.add_permalink_ref(node.parent, _('Permalink to this table'))
        elif isinstance(node.parent, nodes.table):
            self.body.append('</span>')

        BaseTranslator.depart_title(self, node)
Esempio n. 8
0
    def visit_image(self, node):
        # type: (nodes.Node) -> None
        olduri = node['uri']
        # rewrite the URI if the environment knows about it
        if olduri in self.builder.images:
            node['uri'] = posixpath.join(self.builder.imgpath,
                                         self.builder.images[olduri])

        uri = node['uri']
        if uri.lower().endswith(('svg', 'svgz')):
            atts = {'src': uri}
            if 'width' in node:
                atts['width'] = node['width']
            if 'height' in node:
                atts['height'] = node['height']
            atts['alt'] = node.get('alt', uri)
            if 'align' in node:
                self.body.append('<div align="%s" class="align-%s">' %
                                 (node['align'], node['align']))
                self.context.append('</div>\n')
            else:
                self.context.append('')
            self.body.append(self.emptytag(node, 'img', '', **atts))
            return

        if 'scale' in node:
            # Try to figure out image height and width.  Docutils does that too,
            # but it tries the final file name, which does not necessarily exist
            # yet at the time the HTML file is written.
            if not ('width' in node and 'height' in node):
                size = get_image_size(os.path.join(self.builder.srcdir, olduri))
                if size is None:
                    logger.warning('Could not obtain image size. :scale: option is ignored.',
                                   location=node)
                else:
                    if 'width' not in node:
                        node['width'] = str(size[0])
                    if 'height' not in node:
                        node['height'] = str(size[1])
        BaseTranslator.visit_image(self, node)
Esempio n. 9
0
    def visit_literal_block(self, node):
        # type: (nodes.Node) -> None
        if node.rawsource != node.astext():
            # most probably a parsed-literal block -- don't highlight
            return BaseTranslator.visit_literal_block(self, node)

        lang = node.get('language', 'default')
        linenos = node.get('linenos', False)
        highlight_args = node.get('highlight_args', {})
        highlight_args['force'] = node.get('force_highlighting', False)
        if lang is self.builder.config.highlight_language:
            # only pass highlighter options for original language
            opts = self.builder.config.highlight_options
        else:
            opts = {}

        highlighted = self.highlighter.highlight_block(
            node.rawsource, lang, opts=opts, linenos=linenos,
            location=(self.builder.current_docname, node.line), **highlight_args
        )
        starttag = self.starttag(node, 'div', suffix='',
                                 CLASS='highlight-%s notranslate' % lang)
        self.body.append(starttag + highlighted + '</div>\n')
        raise nodes.SkipNode
Esempio n. 10
0
 def visit_field_list(self, node):
     # type: (nodes.Node) -> None
     self._fieldlist_row_index = 0
     return BaseTranslator.visit_field_list(self, node)
Esempio n. 11
0
 def depart_image(self, node):
     # type: (nodes.Node) -> None
     if node['uri'].lower().endswith(('svg', 'svgz')):
         self.body.append(self.context.pop())
     else:
         BaseTranslator.depart_image(self, node)
Esempio n. 12
0
 def visit_bullet_list(self, node):
     # type: (nodes.Node) -> None
     if len(node) == 1 and node[0].tagname == 'toctree':
         # avoid emitting empty <ul></ul>
         raise nodes.SkipNode
     BaseTranslator.visit_bullet_list(self, node)
Esempio n. 13
0
 def visit_field_list(self, node):
     # type: (nodes.Node) -> None
     self._fieldlist_row_index = 0
     return BaseTranslator.visit_field_list(self, node)
Esempio n. 14
0
 def depart_image(self, node):
     # type: (nodes.Node) -> None
     if node['uri'].lower().endswith(('svg', 'svgz')):
         self.body.append(self.context.pop())
     else:
         BaseTranslator.depart_image(self, node)
Esempio n. 15
0
 def visit_bullet_list(self, node):
     # type: (nodes.Node) -> None
     if len(node) == 1 and node[0].tagname == 'toctree':
         # avoid emitting empty <ul></ul>
         raise nodes.SkipNode
     BaseTranslator.visit_bullet_list(self, node)
Esempio n. 16
0
 def __init__(self, doc):
     BaseTranslator.__init__(self, doc)