Fix the handling of images in different directories.

This commit is contained in:
Georg Brandl 2008-11-06 10:36:56 +01:00
parent 27ee1ed5d4
commit dbfe74d5e6
6 changed files with 28 additions and 16 deletions

View File

@ -141,6 +141,8 @@ New features added
Bugs fixed Bugs fixed
---------- ----------
* Fix the handling of images in different directories.
* Support option lists in the text writer. Make sure that dashes * Support option lists in the text writer. Make sure that dashes
introducing long option names are not contracted to en-dashes. introducing long option names are not contracted to en-dashes.

View File

@ -32,7 +32,7 @@ except ImportError:
from docutils import nodes from docutils import nodes
from docutils.io import FileInput, NullOutput from docutils.io import FileInput, NullOutput
from docutils.core import Publisher from docutils.core import Publisher
from docutils.utils import Reporter from docutils.utils import Reporter, relative_path
from docutils.readers import standalone from docutils.readers import standalone
from docutils.parsers.rst import roles from docutils.parsers.rst import roles
from docutils.parsers.rst.languages import en as english from docutils.parsers.rst.languages import en as english
@ -619,19 +619,22 @@ class BuildEnvironment:
node['uri'] = imgpath node['uri'] = imgpath
if imgpath.endswith(os.extsep + '*'): if imgpath.endswith(os.extsep + '*'):
for filename in glob(path.join(self.srcdir, imgpath)): for filename in glob(path.join(self.srcdir, imgpath)):
dir, base = path.split(filename) relname = relative_path(self.srcdir, filename)
if base.lower().endswith('.pdf'): if filename.lower().endswith('.pdf'):
candidates['application/pdf'] = path.join(docdir, base) candidates['application/pdf'] = path.join(docdir, relname)
elif base.lower().endswith('.svg'): elif filename.lower().endswith('.svg'):
candidates['image/svg+xml'] = path.join(docdir, base) candidates['image/svg+xml'] = path.join(docdir, relname)
else: else:
f = open(filename, 'rb')
try: try:
imgtype = imghdr.what(f) f = open(filename, 'rb')
finally: try:
f.close() imgtype = imghdr.what(f)
finally:
f.close()
except (OSError, IOError):
self.warn(docname, 'Image file %s not readable' % filename)
if imgtype: if imgtype:
candidates['image/' + imgtype] = path.join(docdir, base) candidates['image/' + imgtype] = path.join(docdir, relname)
else: else:
candidates['*'] = imgpath candidates['*'] = imgpath
for imgpath in candidates.itervalues(): for imgpath in candidates.itervalues():

View File

@ -18,3 +18,6 @@ Sphinx image handling
.. a non-local image URI .. a non-local image URI
.. image:: http://www.python.org/logo.png .. image:: http://www.python.org/logo.png
.. an image with subdir and unspecified extension
.. image:: subdir/simg.*

BIN
tests/root/subdir/simg.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

View File

@ -27,7 +27,7 @@ latex_warnfile = StringIO()
ENV_WARNINGS = """\ ENV_WARNINGS = """\
WARNING: %(root)s/images.txt:9: Image file not readable: foo.png 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 \ 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 file u'wrongenc.inc' seems to be wrong, try giving an :encoding: option
""" """
@ -44,6 +44,7 @@ HTML_XPATH = {
'images.html': { 'images.html': {
".//img[@src='_images/img.png']": '', ".//img[@src='_images/img.png']": '',
".//img[@src='_images/img1.png']": '', ".//img[@src='_images/img1.png']": '',
".//img[@src='_images/simg.png']": '',
}, },
'includes.html': { 'includes.html': {
".//pre/span[@class='s']": u'üöä', ".//pre/span[@class='s']": u'üöä',

View File

@ -54,16 +54,19 @@ def test_images():
htmlbuilder = StandaloneHTMLBuilder(app, env) htmlbuilder = StandaloneHTMLBuilder(app, env)
htmlbuilder.post_process_images(tree) htmlbuilder.post_process_images(tree)
assert "no matching candidate for image URI u'foo.*'" in app._warning.content[-1] 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.keys()) == set(['subdir/img.png', 'img.png',
assert set(htmlbuilder.images.values()) == set(['img.png', 'img1.png']) 'subdir/simg.png'])
assert set(htmlbuilder.images.values()) == set(['img.png', 'img1.png',
'simg.png'])
app._warning.reset() app._warning.reset()
latexbuilder = LaTeXBuilder(app, env) latexbuilder = LaTeXBuilder(app, env)
latexbuilder.post_process_images(tree) latexbuilder.post_process_images(tree)
assert "no matching candidate for image URI u'foo.*'" in app._warning.content[-1] 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']) '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(): def test_second_update():
# delete, add and "edit" (change saved mtime) some files and update again # delete, add and "edit" (change saved mtime) some files and update again