Georg Brandl avatar Georg Brandl committed 1877718

Fix #60: "smart" image inclusion in subdirs.

Comments (0)

Files changed (5)

 Release 0.5.1 (in development)
 ==============================
 
+* Fix a bug when including images in sources in subdirectories.
+
 * Fix the JavaScript search when html_copy_source is off.
 
 * Fix a bug in autodoc when documenting classes with the option.

sphinx/environment.py

                 self.warn(docname, 'Nonlocal image URI found: %s' % imguri, node.line)
                 candidates['?'] = imguri
                 continue
+            # imgpath is the image path *from srcdir*
             imgpath = path.normpath(path.join(docdir, imguri))
+            # set imgpath as default URI
             node['uri'] = imgpath
             if imgpath.endswith(os.extsep + '*'):
                 for filename in glob(path.join(self.srcdir, imgpath)):
-                    relname = relative_path(self.srcdir, filename)
+                    new_imgpath = relative_path(self.srcdir, filename)
                     if filename.lower().endswith('.pdf'):
-                        candidates['application/pdf'] = path.join(docdir, relname)
+                        candidates['application/pdf'] = new_imgpath
                     elif filename.lower().endswith('.svg'):
-                        candidates['image/svg+xml'] = path.join(docdir, relname)
+                        candidates['image/svg+xml'] = new_imgpath
                     else:
                         try:
                             f = open(filename, 'rb')
                         except (OSError, IOError):
                             self.warn(docname, 'Image file %s not readable' % filename)
                         if imgtype:
-                            candidates['image/' + imgtype] = path.join(docdir, relname)
+                            candidates['image/' + imgtype] = new_imgpath
             else:
                 candidates['*'] = imgpath
+            # map image paths to unique image names (so that they can be put
+            # into a single directory)
             for imgpath in candidates.itervalues():
                 self.dependencies.setdefault(docname, set()).add(imgpath)
                 if not os.access(path.join(self.srcdir, imgpath), os.R_OK):

tests/root/contents.txt

    :maxdepth: 2
 
    images
+   subdir/images
    includes
    markup
    desc

tests/root/subdir/images.txt

+Image including source in subdir
+================================
+
+.. image:: img.*

tests/test_build.py

         ".//img[@src='_images/img1.png']": '',
         ".//img[@src='_images/simg.png']": '',
     },
+    'subdir/images.html': {
+        ".//img[@src='../_images/img1.png']": '',
+    },
     'includes.html': {
         ".//pre/span[@class='s']": u'üöä',
         ".//pre": u'Max Strauß',
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.