1. Luke Plant
  2. sphinx

Commits

georg.brandl  committed a3a326f

Fix image handling.

  • Participants
  • Parent commits 7a30f33
  • Branches default

Comments (0)

Files changed (2)

File sphinx/builder.py

View file
         Pick the best candidate for all image URIs.
         """
         for node in doctree.traverse(nodes.image):
-            uri = node['candidates'].get('*', None)
-            if not uri:
+            if '*' not in node['candidates']:
                 for imgtype in self.supported_image_types:
-                    uri = node['candidates'].get(imgtype, None)
-                    if uri:
-                        node['uri'] = uri
+                    candidate = node['candidates'].get(imgtype, None)
+                    if candidate:
                         break
                 else:
-                    self.warn('%s:%s: %s' %
-                              (node.source, node.lineno,
-                               'No matching candidate for uri: %(uri)s' % node))
+                    self.warn('%s:%s: no matching candidate for image URI %r' %
+                              (node.source, node.lineno, node['uri']))
                     continue
-            if uri in self.env.images:
-                self.images[uri] = self.env.images[uri][1]
+                node['uri'] = candidate
+            else:
+                candidate = node['uri']
+            self.images[candidate] = self.env.images[candidate][1]
 
     # build methods
 

File sphinx/environment.py

View file
 
 # This is increased every time an environment attribute is added
 # or changed to properly invalidate pickle files.
-ENV_VERSION = 23
+ENV_VERSION = 24
 
 
 default_substitutions = set([
                 candidates['*'] = imguri
                 continue
             imgpath = path.normpath(path.join(docdir, imguri))
+            node['uri'] = imgpath
             if imgpath.endswith(os.extsep + '*'):
                 for filename in glob(imgpath):
                     basename, ext = os.path.splitext(filename)
                     elif ext == '.svg':
                         candidates['image/svg+xml'] = filename
                     else:
-                        imgtype = imghdr.what(filename)
+                        f = open(filename, 'rb')
+                        try:
+                            imgtype = imghdr.what(f)
+                        finally:
+                            f.close()
                         if imgtype:
                             candidates['image/' + imgtype] = filename
             else: