diff options
| author | Roland Meister <devnull@localhost> | 2011-02-27 22:02:30 +0100 |
|---|---|---|
| committer | Roland Meister <devnull@localhost> | 2011-02-27 22:02:30 +0100 |
| commit | 53a881ffc232399655a5086eddca84885f8e3d62 (patch) | |
| tree | 179eb6eae95607da9295d54d1ffc8c62de868cb1 /sphinx | |
| parent | 819629aace7f57694f6d399c5e7b50a9c69021b7 (diff) | |
| download | sphinx-53a881ffc232399655a5086eddca84885f8e3d62.tar.gz | |
Added epub_fix_images configuration option
Diffstat (limited to 'sphinx')
| -rw-r--r-- | sphinx/builders/epub.py | 55 | ||||
| -rw-r--r-- | sphinx/config.py | 1 | ||||
| -rw-r--r-- | sphinx/quickstart.py | 3 |
3 files changed, 58 insertions, 1 deletions
diff --git a/sphinx/builders/epub.py b/sphinx/builders/epub.py index 218a93e1..101cb936 100644 --- a/sphinx/builders/epub.py +++ b/sphinx/builders/epub.py @@ -17,12 +17,21 @@ import time import zipfile from os import path +try: + from PIL import Image +except ImportError: + try: + import Image + except ImportError: + Image = None + from docutils import nodes from sphinx import addnodes 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.console import brown # (Fragment) templates from which the metainfo files content.opf, toc.ncx, @@ -299,6 +308,50 @@ class EpubBuilder(StandaloneHTMLBuilder): subentrylinks[i] = \ 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', outfilename=None, event_arg=None): """Create a rendered page. diff --git a/sphinx/config.py b/sphinx/config.py index 58d5c76a..aa689f2e 100644 --- a/sphinx/config.py +++ b/sphinx/config.py @@ -126,6 +126,7 @@ class Config(object): epub_exclude_files = ([], 'env'), epub_tocdepth = (3, 'env'), epub_tocdup = (True, 'env'), + epub_fix_images = (False, 'html'), # LaTeX options latex_documents = ([], None), diff --git a/sphinx/quickstart.py b/sphinx/quickstart.py index b606c9be..e1a5d9e8 100644 --- a/sphinx/quickstart.py +++ b/sphinx/quickstart.py @@ -285,6 +285,9 @@ epub_copyright = u'%(copyright_str)s' # Allow duplicate toc entries. #epub_tocdup = True + +# Fix unsupported image types using the PIL. +#epub_fix_images = False ''' INTERSPHINX_CONFIG = ''' |
