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]) BaseTranslator.visit_image(self, node)
def visit_image(self, node): image_src_path = os.path.join(utils.resolve_root_folder(), node['uri']) if not image_src_path.startswith("/"): image_src_path = "/" + image_src_path image = Image.__all__.get(image_src_path, None) if image is None: self.article.log.warning( "Image not found in images folder: {}".format( utils.get_better_path_rep(image_src_path) ) ) else: node['uri'] = image.url if 'alt' not in node: image_alt = os.path.splitext(os.path.basename(node['uri']))[0] image_alt = re.sub("[-_.]", " ", image_alt) node['alt'] = image_alt.capitalize() node['title'] = node['alt'] node.known_attributes += ('title', ) #import pdb; pdb.set_trace() print(node.attributes) return HTMLTranslator.visit_image(self, node)
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 node.has_key('width'): atts['width'] = node['width'] if node.has_key('height'): atts['height'] = node['height'] if node.has_key('alt'): atts['alt'] = node['alt'] if node.has_key('align'): 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 node.has_key('scale'): # 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 (node.has_key('width') and node.has_key('height')): 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 not node.has_key('width'): node['width'] = str(im.size[0]) if not node.has_key('height'): node['height'] = str(im.size[1]) try: im.fp.close() except Exception: pass BaseTranslator.visit_image(self, node)
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)
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 = {'data': node['uri'], 'type': 'image/svg+xml'} if node.has_key('width'): atts['width'] = node['width'] if node.has_key('height'): atts['height'] = node['height'] if node.has_key('align'): self.body.append('<div align="%s" class="align-%s">' % (node['align'], node['align'])) self.context.append('</div>\n') else: self.context.append('') embatts = atts.copy() embatts['src'] = embatts.pop('data') self.body.append(self.starttag(node, 'object', '', **atts)) self.body.append(self.emptytag(node, 'embed', '', **embatts)) self.body.append('</object>\n') return if node.has_key('scale'): if Image and not (node.has_key('width') and node.has_key('height')): 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 not node.has_key('width'): node['width'] = str(im.size[0]) if not node.has_key('height'): node['height'] = str(im.size[1]) del im BaseTranslator.visit_image(self, node)
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.' ), # NOQA 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)
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 = {'data': node['uri'], 'type': 'image/svg+xml'} if node.has_key('width'): atts['width'] = node['width'] if node.has_key('height'): atts['height'] = node['height'] if node.has_key('align'): self.body.append('<div align="%s" class="align-%s">' % (node['align'], node['align'])) self.context.append('</div>\n') else: self.context.append('') embatts = atts.copy() embatts['src'] = embatts.pop('data') self.body.append(self.starttag(node, 'object', '', **atts)) self.body.append(self.emptytag(node, 'embed', '', **embatts)) self.body.append('</object>\n') return if node.has_key('scale'): if Image and not (node.has_key('width') and node.has_key('height')): 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 not node.has_key('width'): node['width'] = str(im.size[0]) if not node.has_key('height'): node['height'] = str(im.size[1]) del im BaseTranslator.visit_image(self, node)
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]) del im BaseTranslator.visit_image(self, node)
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 not ('width' in node and 'height' in node): size = get_image_size(os.path.join(self.builder.srcdir, olduri)) if size is None: self.builder.env.warn_node('Could not obtain image size. ' ':scale: option is ignored.', 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)
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.has_key("scale"): if Image and not (node.has_key("width") and node.has_key("height")): 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. print olduri pass else: if not node.has_key("width"): node["width"] = str(im.size[0]) if not node.has_key("height"): node["height"] = str(im.size[1]) del im BaseTranslator.visit_image(self, node)
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.has_key('scale'): if Image and not (node.has_key('width') and node.has_key('height')): 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. print olduri pass else: if not node.has_key('width'): node['width'] = str(im.size[0]) if not node.has_key('height'): node['height'] = str(im.size[1]) del im BaseTranslator.visit_image(self, node)
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 not ("width" in node and "height" in node): size = get_image_size(os.path.join(self.builder.srcdir, olduri)) if size is None: self.builder.env.warn_node("Could not obtain image size. " ":scale: option is ignored.", 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)
def visit_image(self, node): # set an empty alt if alt is not specified # avoids that alt is taken from src node['alt'] = node.get('alt', '') return HTMLTranslator.visit_image(self, node)
def visit_image(self, node): # opengraph specs allows multiple images. # http://ogp.me/#array self.images.append(node['uri']) HTMLTranslator.visit_image(self, node)
--- sphinx/writers/html.py.orig 2016-10-01 15:14:37 UTC +++ sphinx/writers/html.py @@ -16,6 +16,7 @@ import copy import warnings from six import string_types +import docutils from docutils import nodes from docutils.writers.html4css1 import Writer, HTMLTranslator as BaseTranslator @@ -497,6 +498,16 @@ class HTMLTranslator(BaseTranslator): if 'height' not in node: node['height'] = str(size[1]) BaseTranslator.visit_image(self, node) + + # overwritten + def depart_image(self, node): + if docutils.__version__ >= "0.13": + # since docutils-0.13, HTMLWriter does not push context data on visit_image() + if node['uri'].lower().endswith(('svg', 'svgz')): + self.body.append(self.context.pop()) + else: + # docutils-0.12 or below, HTML Writer always push context data on visit_image() + self.body.append(self.context.pop()) def visit_toctree(self, node): # this only happens when formatting a toc from env.tocs -- in this