mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
EPUB Input plugin now rasterizes cover and sets a type='titlepage' guide element to point to the HTML cover. The HTML cover is removed from the spine.
This commit is contained in:
parent
1424632286
commit
c37c5436b0
@ -59,7 +59,10 @@ class HTMLRenderer(object):
|
|||||||
|
|
||||||
def render_html(path_to_html, width=590, height=750):
|
def render_html(path_to_html, width=590, height=750):
|
||||||
from PyQt4.QtWebKit import QWebPage
|
from PyQt4.QtWebKit import QWebPage
|
||||||
from PyQt4.Qt import QEventLoop, QPalette, Qt, SIGNAL, QUrl, QSize
|
from PyQt4.Qt import QEventLoop, QPalette, Qt, SIGNAL, QUrl, QSize, \
|
||||||
|
QApplication
|
||||||
|
if QApplication.instance() is None:
|
||||||
|
QApplication([])
|
||||||
path_to_html = os.path.abspath(path_to_html)
|
path_to_html = os.path.abspath(path_to_html)
|
||||||
with CurrentDir(os.path.dirname(path_to_html)):
|
with CurrentDir(os.path.dirname(path_to_html)):
|
||||||
page = QWebPage()
|
page = QWebPage()
|
||||||
|
@ -51,6 +51,39 @@ class EPUBInput(InputFormatPlugin):
|
|||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def rationalize_cover(self, opf):
|
||||||
|
guide_cover, guide_elem = None, None
|
||||||
|
for guide_elem in opf.iterguide():
|
||||||
|
if guide_elem.get('type', '').lower() == 'cover':
|
||||||
|
guide_cover = guide_elem.get('href', '')
|
||||||
|
break
|
||||||
|
if not guide_cover:
|
||||||
|
return
|
||||||
|
spine = list(opf.iterspine())
|
||||||
|
if not spine:
|
||||||
|
return
|
||||||
|
idref = spine[0].get('idref', '')
|
||||||
|
manifest = list(opf.itermanifest())
|
||||||
|
if not manifest:
|
||||||
|
return
|
||||||
|
if manifest[0].get('id', False) != idref:
|
||||||
|
return
|
||||||
|
spine[0].getparent().remove(spine[0])
|
||||||
|
guide_elem.set('href', 'calibre_raster_cover.jpg')
|
||||||
|
for elem in list(opf.iterguide()):
|
||||||
|
if elem.get('type', '').lower() == 'titlepage':
|
||||||
|
elem.getparent().remove(elem)
|
||||||
|
from calibre.ebooks.oeb.base import OPF
|
||||||
|
t = etree.SubElement(guide_elem.getparent(), OPF('reference'))
|
||||||
|
t.set('type', 'titlepage')
|
||||||
|
t.set('href', guide_cover)
|
||||||
|
t.set('title', 'Title Page')
|
||||||
|
from calibre.ebooks import render_html
|
||||||
|
open('calibre_raster_cover.jpg', 'wb').write(
|
||||||
|
render_html(guide_cover).data)
|
||||||
|
|
||||||
|
|
||||||
def convert(self, stream, options, file_ext, log, accelerators):
|
def convert(self, stream, options, file_ext, log, accelerators):
|
||||||
from calibre.utils.zipfile import ZipFile
|
from calibre.utils.zipfile import ZipFile
|
||||||
from calibre import walk
|
from calibre import walk
|
||||||
@ -75,14 +108,17 @@ class EPUBInput(InputFormatPlugin):
|
|||||||
|
|
||||||
opf = os.path.relpath(opf, os.getcwdu())
|
opf = os.path.relpath(opf, os.getcwdu())
|
||||||
parts = os.path.split(opf)
|
parts = os.path.split(opf)
|
||||||
|
opf = OPF(opf, os.path.dirname(os.path.abspath(opf)))
|
||||||
|
|
||||||
if len(parts) > 1:
|
if len(parts) > 1:
|
||||||
delta = '/'.join(parts[:-1])+'/'
|
delta = '/'.join(parts[:-1])+'/'
|
||||||
opf = OPF(opf, os.path.dirname(os.path.abspath(opf)))
|
|
||||||
for elem in opf.itermanifest():
|
for elem in opf.itermanifest():
|
||||||
elem.set('href', delta+elem.get('href'))
|
elem.set('href', delta+elem.get('href'))
|
||||||
for elem in opf.iterguide():
|
for elem in opf.iterguide():
|
||||||
elem.set('href', delta+elem.get('href'))
|
elem.set('href', delta+elem.get('href'))
|
||||||
|
|
||||||
|
self.rationalize_cover(opf)
|
||||||
|
|
||||||
with open('content.opf', 'wb') as nopf:
|
with open('content.opf', 'wb') as nopf:
|
||||||
nopf.write(opf.render())
|
nopf.write(opf.render())
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user