mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Refactor: guess_mimetype()
This commit is contained in:
parent
7d1b0ec7a1
commit
208eb6e052
@ -16,7 +16,6 @@ import time
|
||||
import types
|
||||
import bisect
|
||||
import codecs
|
||||
import imghdr
|
||||
import string
|
||||
import unicodedata
|
||||
from os import path
|
||||
@ -41,6 +40,7 @@ from sphinx.util import url_re, get_matching_docs, docname_join, split_into, \
|
||||
from sphinx.util.nodes import clean_astext, make_refnode, WarningStream, is_translatable
|
||||
from sphinx.util.osutil import SEP, getcwd, fs_encoding, ensuredir
|
||||
from sphinx.util.i18n import find_catalog_files
|
||||
from sphinx.util.images import guess_mimetype
|
||||
from sphinx.util.console import bold, purple
|
||||
from sphinx.util.matching import compile_matchers
|
||||
from sphinx.util.parallel import ParallelTasks, parallel_available, make_chunks
|
||||
@ -902,24 +902,15 @@ class BuildEnvironment:
|
||||
for filename in glob(full_imgpath):
|
||||
new_imgpath = relative_path(path.join(self.srcdir, 'dummy'),
|
||||
filename)
|
||||
if filename.lower().endswith('.pdf'):
|
||||
candidates['application/pdf'] = new_imgpath
|
||||
elif filename.lower().endswith('.svg'):
|
||||
candidates['image/svg+xml'] = new_imgpath
|
||||
else:
|
||||
try:
|
||||
f = open(filename, 'rb')
|
||||
try:
|
||||
imgtype = imghdr.what(f)
|
||||
finally:
|
||||
f.close()
|
||||
except (OSError, IOError) as err:
|
||||
self.warn_node('image file %s not readable: %s' %
|
||||
(filename, err), node)
|
||||
if imgtype:
|
||||
candidates['image/' + imgtype] = new_imgpath
|
||||
try:
|
||||
mimetype = guess_mimetype(filename)
|
||||
candidates[mimetype] = new_imgpath
|
||||
except (OSError, IOError) as err:
|
||||
self.warn_node('image file %s not readable: %s' %
|
||||
(filename, err), node)
|
||||
else:
|
||||
candidates['*'] = rel_imgpath
|
||||
|
||||
# map image paths to unique image names (so that they can be put
|
||||
# into a single directory)
|
||||
for imgpath in itervalues(candidates):
|
||||
|
@ -9,7 +9,9 @@
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
|
||||
import imghdr
|
||||
import imagesize
|
||||
from os import path
|
||||
|
||||
try:
|
||||
from PIL import Image # check for the Python Imaging Library
|
||||
@ -19,6 +21,11 @@ except ImportError:
|
||||
except ImportError:
|
||||
Image = None
|
||||
|
||||
mime_suffixes = {
|
||||
'.pdf': 'application/pdf',
|
||||
'.svg': 'image/svg+xml',
|
||||
}
|
||||
|
||||
|
||||
def get_image_size(filename):
|
||||
try:
|
||||
@ -37,3 +44,16 @@ def get_image_size(filename):
|
||||
return size
|
||||
except:
|
||||
return None
|
||||
|
||||
|
||||
def guess_mimetype(filename):
|
||||
_, ext = path.splitext(filename)
|
||||
if ext in mime_suffixes:
|
||||
return mime_suffixes[ext]
|
||||
else:
|
||||
with open(filename, 'rb') as f:
|
||||
imgtype = imghdr.what(f)
|
||||
if imgtype:
|
||||
return 'image/' + imgtype
|
||||
|
||||
return None
|
||||
|
Loading…
Reference in New Issue
Block a user