html.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:chalktalk_docs 作者: loremIpsum1771 项目源码 文件源码
def visit_image(self, node):
        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])

        if node['uri'].lower().endswith('svg') or \
           node['uri'].lower().endswith('svgz'):
            atts = {'src': node['uri']}
            if 'width' in node:
                atts['width'] = node['width']
            if 'height' in node:
                atts['height'] = node['height']
            if 'alt' in node:
                atts['alt'] = node['alt']
            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 Image and not ('width' in node and 'height' in node):
                try:
                    im = Image.open(os.path.join(self.builder.srcdir, olduri))
                except (IOError,  # Source image can't be found or opened
                        UnicodeError):  # PIL doesn't like Unicode paths.
                    pass
                else:
                    if 'width' not in node:
                        node['width'] = str(im.size[0])
                    if 'height' not in node:
                        node['height'] = str(im.size[1])
                    try:
                        im.fp.close()
                    except Exception:
                        pass
        BaseTranslator.visit_image(self, node)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号