Implement #1769 (Remove first image in file during Conversion)

This commit is contained in:
Kovid Goyal 2009-02-04 19:11:26 -08:00
parent 70630c5dbc
commit e06000a20b
2 changed files with 15 additions and 1 deletions

View File

@ -141,6 +141,10 @@ help on using this feature.
action='store_true',
help=_('Use the cover detected from the source file in preference '
'to the specified cover.'))
structure('remove_first_image', ['--remove-first-image'], default=False,
help=_('Remove the first image from the input ebook. Useful if '
'the first image in the source file is a cover and you '
'are specifying an external cover.'))
structure('dont_split_on_page_breaks', ['--dont-split-on-page-breaks'], default=False,
help=_('Turn off splitting at page breaks. Normally, input files '
'are automatically split at every page break into '

View File

@ -141,7 +141,7 @@ class HTMLProcessor(Processor, Rationalizer):
p = QPixmap()
p.load(path)
if not p.isNull():
p.save(path+'_calibre_converted.jpg')
p.save(path + '_calibre_converted.jpg')
os.remove(path)
for key, val in self.resource_map.items():
if val == rpath:
@ -206,6 +206,13 @@ class HTMLProcessor(Processor, Rationalizer):
# self.convert_image(img)
Processor.save(self)
def remove_first_image(self):
images = self.root.xpath('//img')
if images:
images[0].getparent().remove(images[0])
return True
return False
@ -227,10 +234,13 @@ def parse_content(filelist, opts, tdir):
resource_map, stylesheets = {}, {}
toc = TOC(base_path=tdir, type='root')
stylesheet_map = {}
first_image_removed = False
for htmlfile in filelist:
logging.getLogger('html2epub').debug('Processing %s...'%htmlfile)
hp = HTMLProcessor(htmlfile, opts, os.path.join(tdir, 'content'),
resource_map, filelist, stylesheets)
if not first_image_removed and opts.remove_first_image:
first_image_removed = hp.remove_first_image()
hp.populate_toc(toc)
hp.save()
stylesheet_map[os.path.basename(hp.save_path())] = \