Select an image by similarity if multiple images are globbed by `.. image:: filename.*`

Until this point, it is used the last image returned from glob.glob().
The order of glob.glob() is determined by filesystem.
After this, Sphinx uses filename similarity to determine the image.
This commit is contained in:
Takeshi KOMIYA
2016-01-28 00:39:04 +09:00
parent 208eb6e052
commit 46138ca605
4 changed files with 5 additions and 1 deletions

View File

@@ -105,6 +105,7 @@ Features added
* #1853: support custom text splitter on html search with ``language='ja'``.
* #2320: classifier of glossary terms can be used for index entries grouping key.
The classifier also be used for translation. See also :ref:`glossary-directive`.
* Select an image by similarity if multiple images are globbed by ``.. image:: filename.*``
Bugs fixed
----------

View File

@@ -899,15 +899,18 @@ class BuildEnvironment:
# set imgpath as default URI
node['uri'] = rel_imgpath
if rel_imgpath.endswith(os.extsep + '*'):
globbed = {}
for filename in glob(full_imgpath):
new_imgpath = relative_path(path.join(self.srcdir, 'dummy'),
filename)
try:
mimetype = guess_mimetype(filename)
candidates[mimetype] = new_imgpath
globbed.setdefault(mimetype, []).append(new_imgpath)
except (OSError, IOError) as err:
self.warn_node('image file %s not readable: %s' %
(filename, err), node)
for key, files in iteritems(globbed):
candidates[key] = sorted(files, key=len)[0] # select by similarity
else:
candidates['*'] = rel_imgpath

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB