mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
IGN:Improve handling of covers in any2lit
This commit is contained in:
parent
83086312a3
commit
ba0ae51457
@ -122,6 +122,8 @@ def freeze():
|
|||||||
elif exe not in executables:
|
elif exe not in executables:
|
||||||
print >>sys.stderr, 'Invalid invocation of calibre loader. CALIBRE_CX_EXE=%%s is unknown'%%exe
|
print >>sys.stderr, 'Invalid invocation of calibre loader. CALIBRE_CX_EXE=%%s is unknown'%%exe
|
||||||
else:
|
else:
|
||||||
|
from PyQt4.QtCore import QCoreApplication
|
||||||
|
QCoreApplication.setLibraryPaths([sys.frozen_path, os.path.join(sys.frozen_path, "qtplugins")])
|
||||||
sys.argv[0] = exe
|
sys.argv[0] = exe
|
||||||
module, func = executables[exe]
|
module, func = executables[exe]
|
||||||
module = __import__(module, fromlist=[1])
|
module = __import__(module, fromlist=[1])
|
||||||
@ -179,7 +181,7 @@ def freeze():
|
|||||||
if not f.endswith('.so') or 'designer' in dirpath or 'codecs' in dirpath or 'sqldrivers' in dirpath:
|
if not f.endswith('.so') or 'designer' in dirpath or 'codecs' in dirpath or 'sqldrivers' in dirpath:
|
||||||
continue
|
continue
|
||||||
f = os.path.join(dirpath, f)
|
f = os.path.join(dirpath, f)
|
||||||
dest_dir = dirpath.replace(plugdir, os.path.join(FREEZE_DIR, 'qtlugins'))
|
dest_dir = dirpath.replace(plugdir, os.path.join(FREEZE_DIR, 'qtplugins'))
|
||||||
copy_binary(f, dest_dir)
|
copy_binary(f, dest_dir)
|
||||||
|
|
||||||
print 'Creating launchers'
|
print 'Creating launchers'
|
||||||
|
@ -116,7 +116,8 @@ def unarchive(path, tdir):
|
|||||||
return f, ext
|
return f, ext
|
||||||
return find_html_index(files)
|
return find_html_index(files)
|
||||||
|
|
||||||
def any2epub(opts, path, notification=None):
|
def any2epub(opts, path, notification=None, create_epub=True,
|
||||||
|
oeb_cover=False, extract_to=None):
|
||||||
ext = os.path.splitext(path)[1]
|
ext = os.path.splitext(path)[1]
|
||||||
if not ext:
|
if not ext:
|
||||||
raise ValueError('Unknown file type: '+path)
|
raise ValueError('Unknown file type: '+path)
|
||||||
@ -139,7 +140,9 @@ def any2epub(opts, path, notification=None):
|
|||||||
raise ValueError('Conversion from %s is not supported'%ext.upper())
|
raise ValueError('Conversion from %s is not supported'%ext.upper())
|
||||||
|
|
||||||
print 'Creating EPUB file...'
|
print 'Creating EPUB file...'
|
||||||
html2epub(path, opts, notification=notification)
|
html2epub(path, opts, notification=notification,
|
||||||
|
create_epub=create_epub, oeb_cover=oeb_cover,
|
||||||
|
extract_to=extract_to)
|
||||||
|
|
||||||
def config(defaults=None):
|
def config(defaults=None):
|
||||||
return common_config(defaults=defaults)
|
return common_config(defaults=defaults)
|
||||||
|
@ -32,7 +32,7 @@ Conversion of HTML/OPF files follows several stages:
|
|||||||
* The EPUB container is created.
|
* The EPUB container is created.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import os, sys, cStringIO, logging, re, functools
|
import os, sys, cStringIO, logging, re, functools, shutil
|
||||||
|
|
||||||
from lxml.etree import XPath
|
from lxml.etree import XPath
|
||||||
from lxml import html
|
from lxml import html
|
||||||
@ -210,17 +210,16 @@ TITLEPAGE = '''\
|
|||||||
</html>
|
</html>
|
||||||
'''
|
'''
|
||||||
|
|
||||||
def create_cover_image(src, dest, screen_size):
|
def create_cover_image(src, dest, screen_size, rescale_cover=True):
|
||||||
from PyQt4.Qt import QApplication, QImage, Qt
|
|
||||||
if QApplication.instance() is None:
|
|
||||||
app = QApplication([])
|
|
||||||
app
|
|
||||||
im = QImage()
|
|
||||||
try:
|
try:
|
||||||
|
from PyQt4.Qt import QImage, Qt
|
||||||
|
if QApplication.instance() is None:
|
||||||
|
QApplication([])
|
||||||
|
im = QImage()
|
||||||
im.load(src)
|
im.load(src)
|
||||||
if im.isNull():
|
if im.isNull():
|
||||||
raise ValueError
|
raise ValueError('Invalid cover image')
|
||||||
if screen_size is not None:
|
if rescale_cover and screen_size is not None:
|
||||||
width, height = im.width(), im.height()
|
width, height = im.width(), im.height()
|
||||||
dw, dh = (screen_size[0]-width)/float(width), (screen_size[1]-height)/float(height)
|
dw, dh = (screen_size[0]-width)/float(width), (screen_size[1]-height)/float(height)
|
||||||
delta = min(dw, dh)
|
delta = min(dw, dh)
|
||||||
@ -228,7 +227,7 @@ def create_cover_image(src, dest, screen_size):
|
|||||||
nwidth = int(width + delta*(width))
|
nwidth = int(width + delta*(width))
|
||||||
nheight = int(height + delta*(height))
|
nheight = int(height + delta*(height))
|
||||||
im = im.scaled(int(nwidth), int(nheight), Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
|
im = im.scaled(int(nwidth), int(nheight), Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
|
||||||
im.save(dest)
|
im.save(dest)
|
||||||
except:
|
except:
|
||||||
import traceback
|
import traceback
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
@ -241,7 +240,6 @@ def process_title_page(mi, filelist, htmlfilemap, opts, tdir):
|
|||||||
if mi.cover:
|
if mi.cover:
|
||||||
if f(filelist[0].path) == f(mi.cover):
|
if f(filelist[0].path) == f(mi.cover):
|
||||||
old_title_page = htmlfilemap[filelist[0].path]
|
old_title_page = htmlfilemap[filelist[0].path]
|
||||||
|
|
||||||
#logger = logging.getLogger('html2epub')
|
#logger = logging.getLogger('html2epub')
|
||||||
metadata_cover = mi.cover
|
metadata_cover = mi.cover
|
||||||
if metadata_cover and not os.path.exists(metadata_cover):
|
if metadata_cover and not os.path.exists(metadata_cover):
|
||||||
@ -250,14 +248,15 @@ def process_title_page(mi, filelist, htmlfilemap, opts, tdir):
|
|||||||
cpath = '/'.join(('resources', '_cover_.jpg'))
|
cpath = '/'.join(('resources', '_cover_.jpg'))
|
||||||
cover_dest = os.path.join(tdir, 'content', *cpath.split('/'))
|
cover_dest = os.path.join(tdir, 'content', *cpath.split('/'))
|
||||||
if metadata_cover is not None:
|
if metadata_cover is not None:
|
||||||
if not create_cover_image(metadata_cover, cover_dest, opts.profile.screen_size):
|
if not create_cover_image(metadata_cover, cover_dest,
|
||||||
|
opts.profile.screen_size):
|
||||||
metadata_cover = None
|
metadata_cover = None
|
||||||
|
|
||||||
specified_cover = opts.cover
|
specified_cover = opts.cover
|
||||||
if specified_cover and not os.path.exists(specified_cover):
|
if specified_cover and not os.path.exists(specified_cover):
|
||||||
specified_cover = None
|
specified_cover = None
|
||||||
if specified_cover is not None:
|
if specified_cover is not None:
|
||||||
if not create_cover_image(specified_cover, cover_dest, opts.profile.screen_size):
|
if not create_cover_image(specified_cover, cover_dest,
|
||||||
|
opts.profile.screen_size):
|
||||||
specified_cover = None
|
specified_cover = None
|
||||||
|
|
||||||
cover = metadata_cover if specified_cover is None or (opts.prefer_metadata_cover and metadata_cover is not None) else specified_cover
|
cover = metadata_cover if specified_cover is None or (opts.prefer_metadata_cover and metadata_cover is not None) else specified_cover
|
||||||
@ -272,9 +271,16 @@ def process_title_page(mi, filelist, htmlfilemap, opts, tdir):
|
|||||||
elif os.path.exists(cover_dest):
|
elif os.path.exists(cover_dest):
|
||||||
os.remove(cover_dest)
|
os.remove(cover_dest)
|
||||||
return None, old_title_page is not None
|
return None, old_title_page is not None
|
||||||
|
|
||||||
|
|
||||||
def convert(htmlfile, opts, notification=None):
|
def find_oeb_cover(htmlfile):
|
||||||
|
if os.stat(htmlfile).st_size > 2048:
|
||||||
|
return None
|
||||||
|
match = re.search(r'(?i)<img[^<>]+src\s*=\s*[\'"](.+?)[\'"]', open(htmlfile, 'rb').read())
|
||||||
|
if match:
|
||||||
|
return match.group(1)
|
||||||
|
|
||||||
|
def convert(htmlfile, opts, notification=None, create_epub=True,
|
||||||
|
oeb_cover=False, extract_to=None):
|
||||||
htmlfile = os.path.abspath(htmlfile)
|
htmlfile = os.path.abspath(htmlfile)
|
||||||
if opts.output is None:
|
if opts.output is None:
|
||||||
opts.output = os.path.splitext(os.path.basename(htmlfile))[0] + '.epub'
|
opts.output = os.path.splitext(os.path.basename(htmlfile))[0] + '.epub'
|
||||||
@ -326,7 +332,7 @@ def convert(htmlfile, opts, notification=None):
|
|||||||
|
|
||||||
title_page, has_title_page = process_title_page(mi, filelist, htmlfile_map, opts, tdir)
|
title_page, has_title_page = process_title_page(mi, filelist, htmlfile_map, opts, tdir)
|
||||||
spine = [htmlfile_map[f.path] for f in filelist]
|
spine = [htmlfile_map[f.path] for f in filelist]
|
||||||
if title_page is not None:
|
if not oeb_cover and title_page is not None:
|
||||||
spine = [title_page] + spine
|
spine = [title_page] + spine
|
||||||
mi.cover = None
|
mi.cover = None
|
||||||
mi.cover_data = (None, None)
|
mi.cover_data = (None, None)
|
||||||
@ -358,24 +364,43 @@ def convert(htmlfile, opts, notification=None):
|
|||||||
check(opf_path, opts.pretty_print)
|
check(opf_path, opts.pretty_print)
|
||||||
opf = OPF(opf_path, tdir)
|
opf = OPF(opf_path, tdir)
|
||||||
opf.remove_guide()
|
opf.remove_guide()
|
||||||
if has_title_page:
|
oeb_cover_file = None
|
||||||
|
if oeb_cover and title_page is not None:
|
||||||
|
oeb_cover_file = find_oeb_cover(os.path.join(tdir, 'content', title_page))
|
||||||
|
if has_title_page or (oeb_cover and oeb_cover_file):
|
||||||
opf.create_guide_element()
|
opf.create_guide_element()
|
||||||
opf.add_guide_item('cover', 'Cover', 'content/'+spine[0])
|
if has_title_page and not oeb_cover:
|
||||||
|
opf.add_guide_item('cover', 'Cover', 'content/'+spine[0])
|
||||||
|
if oeb_cover and oeb_cover_file:
|
||||||
|
opf.add_guide_item('cover', 'Cover', 'content/'+oeb_cover_file)
|
||||||
|
|
||||||
opf.add_path_to_manifest(os.path.join(tdir, 'content', 'resources', '_cover_.jpg'), 'image/jpeg')
|
cpath = os.path.join(tdir, 'content', 'resources', '_cover_.jpg')
|
||||||
|
if os.path.exists(cpath):
|
||||||
|
opf.add_path_to_manifest(cpath, 'image/jpeg')
|
||||||
with open(opf_path, 'wb') as f:
|
with open(opf_path, 'wb') as f:
|
||||||
raw = opf.render()
|
raw = opf.render()
|
||||||
if not raw.startswith('<?xml '):
|
if not raw.startswith('<?xml '):
|
||||||
raw = '<?xml version="1.0" encoding="UTF-8"?>\n'+raw
|
raw = '<?xml version="1.0" encoding="UTF-8"?>\n'+raw
|
||||||
f.write(raw)
|
f.write(raw)
|
||||||
epub = initialize_container(opts.output)
|
if create_epub:
|
||||||
epub.add_dir(tdir)
|
epub = initialize_container(opts.output)
|
||||||
|
epub.add_dir(tdir)
|
||||||
|
epub.close()
|
||||||
|
logger.info(_('Output written to ')+opts.output)
|
||||||
|
|
||||||
if opts.show_opf:
|
if opts.show_opf:
|
||||||
print open(os.path.join(tdir, 'metadata.opf')).read()
|
print open(os.path.join(tdir, 'metadata.opf')).read()
|
||||||
logger.info('Output written to %s'%opts.output)
|
|
||||||
if opts.extract_to is not None:
|
if opts.extract_to is not None:
|
||||||
epub.extractall(opts.extract_to)
|
if os.path.exists(opts.extract_to):
|
||||||
epub.close()
|
shutil.rmtree(opts.extract_to)
|
||||||
|
shutil.copytree(tdir, opts.extract_to)
|
||||||
|
|
||||||
|
if extract_to is not None:
|
||||||
|
if os.path.exists(extract_to):
|
||||||
|
shutil.rmtree(extract_to)
|
||||||
|
shutil.copytree(tdir, extract_to)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def main(args=sys.argv):
|
def main(args=sys.argv):
|
||||||
|
@ -37,9 +37,8 @@ def any2lit(opts, path):
|
|||||||
oebdir = os.path.join(tdir, 'oeb')
|
oebdir = os.path.join(tdir, 'oeb')
|
||||||
os.mkdir(oebdir)
|
os.mkdir(oebdir)
|
||||||
opts.output = os.path.join(tdir, 'dummy.epub')
|
opts.output = os.path.join(tdir, 'dummy.epub')
|
||||||
opts.extract_to = oebdir
|
|
||||||
opts.profile = 'None'
|
opts.profile = 'None'
|
||||||
any2epub(opts, path)
|
any2epub(opts, path, create_epub=False, oeb_cover=True, extract_to=oebdir)
|
||||||
opf = glob.glob(os.path.join(oebdir, '*.opf'))[0]
|
opf = glob.glob(os.path.join(oebdir, '*.opf'))[0]
|
||||||
opts.output = orig_output
|
opts.output = orig_output
|
||||||
logging.getLogger('html2epub').info(_('Creating LIT file from EPUB...'))
|
logging.getLogger('html2epub').info(_('Creating LIT file from EPUB...'))
|
||||||
|
@ -313,6 +313,9 @@ class LitWriter(object):
|
|||||||
elif MS_COVER_TYPE in oeb.guide:
|
elif MS_COVER_TYPE in oeb.guide:
|
||||||
href = oeb.guide[MS_COVER_TYPE].href
|
href = oeb.guide[MS_COVER_TYPE].href
|
||||||
cover = oeb.manifest.hrefs[href]
|
cover = oeb.manifest.hrefs[href]
|
||||||
|
elif 'cover' in oeb.guide:
|
||||||
|
href = oeb.guide['cover'].href
|
||||||
|
cover = oeb.manifest.hrefs[href]
|
||||||
else:
|
else:
|
||||||
html = oeb.spine[0].data
|
html = oeb.spine[0].data
|
||||||
imgs = xpath(html, '//img[position()=1]')
|
imgs = xpath(html, '//img[position()=1]')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user