IGN:Display covers when viewing lit and mobi files

This commit is contained in:
Kovid Goyal 2008-10-15 13:08:04 -07:00
parent 4c351035c5
commit db4ff1e605
2 changed files with 28 additions and 13 deletions

View File

@ -145,6 +145,20 @@ def resize_cover(im, opts):
im = im.resize((int(nwidth), int(nheight)), PILImage.ANTIALIAS) im = im.resize((int(nwidth), int(nheight)), PILImage.ANTIALIAS)
return im return im
TITLEPAGE = '''\
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Cover</title>
<style type="text/css">@page {padding: 0pt; margin:0pt}</style>
</head>
<body style="padding: 0pt; margin: 0pt">
<div style="text-align:center">
<img style="text-align: center" src="%s" alt="cover" />
</div>
</body>
</html>
'''
def process_title_page(mi, filelist, htmlfilemap, opts, tdir): def process_title_page(mi, filelist, htmlfilemap, opts, tdir):
old_title_page = None old_title_page = None
f = lambda x : os.path.normcase(os.path.normpath(x)) f = lambda x : os.path.normcase(os.path.normpath(x))
@ -188,19 +202,7 @@ def process_title_page(mi, filelist, htmlfilemap, opts, tdir):
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
if hasattr(cover, 'save'): if hasattr(cover, 'save'):
titlepage = '''\ titlepage = TITLEPAGE%cpath
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Cover</title>
<style type="text/css">@page {padding: 0pt; margin:0pt}</style>
</head>
<body style="padding: 0pt; margin: 0pt">
<div style="text-align:center">
<img style="text-align: center" src="%s" alt="cover" />
</div>
</body>
</html>
'''%cpath
tp = 'calibre_title_page.html' if old_title_page is None else old_title_page tp = 'calibre_title_page.html' if old_title_page is None else old_title_page
tppath = os.path.join(tdir, 'content', tp) tppath = os.path.join(tdir, 'content', tp)
with open(tppath, 'wb') as f: with open(tppath, 'wb') as f:

View File

@ -8,6 +8,7 @@ Iterate over the HTML files in an ebook. Useful for writing viewers.
import re, os, math, copy import re, os, math, copy
from calibre.ebooks.epub.from_any import MAP from calibre.ebooks.epub.from_any import MAP
from calibre.ebooks.epub.from_html import TITLEPAGE
from calibre.ebooks.epub import config from calibre.ebooks.epub import config
from calibre.ebooks.metadata.opf2 import OPF from calibre.ebooks.metadata.opf2 import OPF
from calibre.ptempfile import TemporaryDirectory from calibre.ptempfile import TemporaryDirectory
@ -83,16 +84,28 @@ class EbookIterator(object):
self.pathtoopf = self.to_opf(self.pathtoebook, self.base, opts) self.pathtoopf = self.to_opf(self.pathtoebook, self.base, opts)
self.opf = OPF(self.pathtoopf, os.path.dirname(self.pathtoopf)) self.opf = OPF(self.pathtoopf, os.path.dirname(self.pathtoopf))
self.spine = [SpineItem(i.path) for i in self.opf.spine] self.spine = [SpineItem(i.path) for i in self.opf.spine]
cover = self.opf.cover
if os.path.splitext(self.pathtoebook)[1].lower() in ('.lit', '.mobi', '.prc') and cover:
cfile = os.path.join(os.path.dirname(self.spine[0]), 'calibre_ei_cover.html')
open(cfile, 'wb').write(TITLEPAGE%cover)
self.spine[0:0] = [SpineItem(cfile)]
sizes = [i.character_count for i in self.spine] sizes = [i.character_count for i in self.spine]
self.pages = [math.ceil(i/float(self.CHARACTERS_PER_PAGE)) for i in sizes] self.pages = [math.ceil(i/float(self.CHARACTERS_PER_PAGE)) for i in sizes]
for p, s in zip(self.pages, self.spine): for p, s in zip(self.pages, self.spine):
s.pages = p s.pages = p
start = 1 start = 1
for s in self.spine: for s in self.spine:
s.start_page = start s.start_page = start
start += s.pages start += s.pages
s.max_page = s.start_page + s.pages - 1 s.max_page = s.start_page + s.pages - 1
self.toc = self.opf.toc self.toc = self.opf.toc
return self return self
def __exit__(self, *args): def __exit__(self, *args):