diff --git a/CHANGES b/CHANGES index e1fd96a2b..b4ca3722b 100644 --- a/CHANGES +++ b/CHANGES @@ -141,6 +141,8 @@ New features added Bugs fixed ---------- +* Fix the handling of images in different directories. + * Support option lists in the text writer. Make sure that dashes introducing long option names are not contracted to en-dashes. diff --git a/sphinx/environment.py b/sphinx/environment.py index 3b7eb91e1..5e753a1bf 100644 --- a/sphinx/environment.py +++ b/sphinx/environment.py @@ -32,7 +32,7 @@ except ImportError: from docutils import nodes from docutils.io import FileInput, NullOutput from docutils.core import Publisher -from docutils.utils import Reporter +from docutils.utils import Reporter, relative_path from docutils.readers import standalone from docutils.parsers.rst import roles from docutils.parsers.rst.languages import en as english @@ -619,19 +619,22 @@ class BuildEnvironment: node['uri'] = imgpath if imgpath.endswith(os.extsep + '*'): for filename in glob(path.join(self.srcdir, imgpath)): - dir, base = path.split(filename) - if base.lower().endswith('.pdf'): - candidates['application/pdf'] = path.join(docdir, base) - elif base.lower().endswith('.svg'): - candidates['image/svg+xml'] = path.join(docdir, base) + relname = relative_path(self.srcdir, filename) + if filename.lower().endswith('.pdf'): + candidates['application/pdf'] = path.join(docdir, relname) + elif filename.lower().endswith('.svg'): + candidates['image/svg+xml'] = path.join(docdir, relname) else: - f = open(filename, 'rb') try: - imgtype = imghdr.what(f) - finally: - f.close() + f = open(filename, 'rb') + try: + imgtype = imghdr.what(f) + finally: + f.close() + except (OSError, IOError): + self.warn(docname, 'Image file %s not readable' % filename) if imgtype: - candidates['image/' + imgtype] = path.join(docdir, base) + candidates['image/' + imgtype] = path.join(docdir, relname) else: candidates['*'] = imgpath for imgpath in candidates.itervalues(): diff --git a/tests/root/images.txt b/tests/root/images.txt index 84f89c6e0..d73375424 100644 --- a/tests/root/images.txt +++ b/tests/root/images.txt @@ -18,3 +18,6 @@ Sphinx image handling .. a non-local image URI .. image:: http://www.python.org/logo.png + +.. an image with subdir and unspecified extension +.. image:: subdir/simg.* diff --git a/tests/root/subdir/simg.png b/tests/root/subdir/simg.png new file mode 100644 index 000000000..72c12d13f Binary files /dev/null and b/tests/root/subdir/simg.png differ diff --git a/tests/test_build.py b/tests/test_build.py index 07a946964..c62c2f8cb 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -27,7 +27,7 @@ latex_warnfile = StringIO() ENV_WARNINGS = """\ WARNING: %(root)s/images.txt:9: Image file not readable: foo.png -WARNING: %(root)s/images.txt:20: Nonlocal image URI found: http://www.python.org/logo.png +WARNING: %(root)s/images.txt:21: Nonlocal image URI found: http://www.python.org/logo.png WARNING: %(root)s/includes.txt:: (WARNING/2) Encoding 'utf-8' used for reading included \ file u'wrongenc.inc' seems to be wrong, try giving an :encoding: option """ @@ -44,6 +44,7 @@ HTML_XPATH = { 'images.html': { ".//img[@src='_images/img.png']": '', ".//img[@src='_images/img1.png']": '', + ".//img[@src='_images/simg.png']": '', }, 'includes.html': { ".//pre/span[@class='s']": u'üöä', diff --git a/tests/test_env.py b/tests/test_env.py index 528edc48f..a8af1e428 100644 --- a/tests/test_env.py +++ b/tests/test_env.py @@ -54,16 +54,19 @@ def test_images(): htmlbuilder = StandaloneHTMLBuilder(app, env) htmlbuilder.post_process_images(tree) assert "no matching candidate for image URI u'foo.*'" in app._warning.content[-1] - assert set(htmlbuilder.images.keys()) == set(['subdir/img.png', 'img.png']) - assert set(htmlbuilder.images.values()) == set(['img.png', 'img1.png']) + assert set(htmlbuilder.images.keys()) == set(['subdir/img.png', 'img.png', + 'subdir/simg.png']) + assert set(htmlbuilder.images.values()) == set(['img.png', 'img1.png', + 'simg.png']) app._warning.reset() latexbuilder = LaTeXBuilder(app, env) latexbuilder.post_process_images(tree) assert "no matching candidate for image URI u'foo.*'" in app._warning.content[-1] - assert set(latexbuilder.images.keys()) == set(['subdir/img.png', + assert set(latexbuilder.images.keys()) == set(['subdir/img.png', 'subdir/simg.png', 'img.png', 'img.pdf']) - assert set(latexbuilder.images.values()) == set(['img.pdf', 'img.png', 'img1.png']) + assert set(latexbuilder.images.values()) == set(['img.pdf', 'img.png', + 'img1.png', 'simg.pdf']) def test_second_update(): # delete, add and "edit" (change saved mtime) some files and update again