Added epub_fix_images configuration option

This commit is contained in:
Roland Meister 2011-02-27 22:02:30 +01:00
parent 3b42bf8c27
commit fe07254b9c
5 changed files with 67 additions and 1 deletions

View File

@ -38,6 +38,7 @@ epub_pre_files = [('index.html', 'Welcome')]
epub_exclude_files = ['_static/opensearch.xml', '_static/doctools.js', epub_exclude_files = ['_static/opensearch.xml', '_static/doctools.js',
'_static/jquery.js', '_static/searchtools.js', '_static/underscore.js', '_static/jquery.js', '_static/searchtools.js', '_static/underscore.js',
'_static/basic.css', 'search.html'] '_static/basic.css', 'search.html']
epub_fix_images = False
latex_documents = [('contents', 'sphinx.tex', 'Sphinx Documentation', latex_documents = [('contents', 'sphinx.tex', 'Sphinx Documentation',
'Georg Brandl', 'manual', 1)] 'Georg Brandl', 'manual', 1)]

View File

@ -804,6 +804,14 @@ the `Dublin Core metadata <http://dublincore.org/>`_.
a chapter, but can be confusing because it mixes entries of differnet a chapter, but can be confusing because it mixes entries of differnet
depth in one list. The default value is ``True``. depth in one list. The default value is ``True``.
.. confval:: epub_fix_images
This flag determines if sphinx should try to fix image formats that are not
supported by some epub readers. At the moment palette images with a small
color table are upgraded. You need the Python Image Library (PIL) installed
to use this option. The default value is ``False`` because the automatic
conversion may loose information.
.. _latex-options: .. _latex-options:
Options for LaTeX output Options for LaTeX output

View File

@ -17,12 +17,21 @@ import time
import zipfile import zipfile
from os import path from os import path
try:
from PIL import Image
except ImportError:
try:
import Image
except ImportError:
Image = None
from docutils import nodes from docutils import nodes
from sphinx import addnodes from sphinx import addnodes
from sphinx.builders.html import StandaloneHTMLBuilder from sphinx.builders.html import StandaloneHTMLBuilder
from sphinx.util.osutil import EEXIST from sphinx.util.osutil import ensuredir, EEXIST
from sphinx.util.smartypants import sphinx_smarty_pants as ssp from sphinx.util.smartypants import sphinx_smarty_pants as ssp
from sphinx.util.console import brown
# (Fragment) templates from which the metainfo files content.opf, toc.ncx, # (Fragment) templates from which the metainfo files content.opf, toc.ncx,
@ -299,6 +308,50 @@ class EpubBuilder(StandaloneHTMLBuilder):
subentrylinks[i] = \ subentrylinks[i] = \
self.fix_fragment(m.group(1), m.group(2)) self.fix_fragment(m.group(1), m.group(2))
def copy_image_files_pil(self):
"""Copy images using the PIL.
The method tries to read and write the files with the PIL,
converting the format if necessary/possible.
"""
ensuredir(path.join(self.outdir, '_images'))
for src in self.status_iterator(self.images, 'copying images... ',
brown, len(self.images)):
dest = self.images[src]
try:
img = Image.open(path.join(self.srcdir, src))
except IOError:
self.warn('cannot read image file %r: copying it instead' %
(path.join(self.srcdir, src), ))
try:
copyfile(path.join(self.srcdir, src),
path.join(self.outdir, '_images', dest))
except Exception, err:
self.warn('cannot copy image file %r: %s' %
(path.join(self.srcdir, src), err))
continue
if img.mode in ('P',):
# See PIL documentation for Image.convert()
img = img.convert()
try:
img.save(path.join(self.outdir, '_images', dest))
except IOError, err:
self.warn('cannot write image file %r: %s' %
(path.join(self.srcdir, src), err))
def copy_image_files(self):
"""Copy image files to destination directory.
This overwritten method can use the PIL to convert image files.
"""
if self.images:
if self.config.epub_fix_images:
if not Image:
self.warn('PIL not found - copying image files')
super(EpubBuilder, self).copy_image_files()
else:
self.copy_image_files_pil()
else:
super(EpubBuilder, self).copy_image_files()
def handle_page(self, pagename, addctx, templatename='page.html', def handle_page(self, pagename, addctx, templatename='page.html',
outfilename=None, event_arg=None): outfilename=None, event_arg=None):
"""Create a rendered page. """Create a rendered page.

View File

@ -126,6 +126,7 @@ class Config(object):
epub_exclude_files = ([], 'env'), epub_exclude_files = ([], 'env'),
epub_tocdepth = (3, 'env'), epub_tocdepth = (3, 'env'),
epub_tocdup = (True, 'env'), epub_tocdup = (True, 'env'),
epub_fix_images = (False, 'html'),
# LaTeX options # LaTeX options
latex_documents = ([], None), latex_documents = ([], None),

View File

@ -285,6 +285,9 @@ epub_copyright = u'%(copyright_str)s'
# Allow duplicate toc entries. # Allow duplicate toc entries.
#epub_tocdup = True #epub_tocdup = True
# Fix unsupported image types using the PIL.
#epub_fix_images = False
''' '''
INTERSPHINX_CONFIG = ''' INTERSPHINX_CONFIG = '''