mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Fix #2130 (Calibre Stops Reading MOBI Files)
This commit is contained in:
parent
aacd082e54
commit
802385787a
@ -12,7 +12,7 @@ from PyQt4.Qt import QFontDatabase
|
||||
|
||||
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.ptempfile import TemporaryDirectory
|
||||
from calibre.ebooks.chardet import xml_to_unicode
|
||||
@ -31,15 +31,15 @@ def character_count(html):
|
||||
return count
|
||||
|
||||
class UnsupportedFormatError(Exception):
|
||||
|
||||
|
||||
def __init__(self, fmt):
|
||||
Exception.__init__(self, _('%s format books are not supported')%fmt.upper())
|
||||
Exception.__init__(self, _('%s format books are not supported')%fmt.upper())
|
||||
|
||||
class SpineItem(unicode):
|
||||
|
||||
|
||||
def __new__(cls, *args):
|
||||
obj = super(SpineItem, cls).__new__(cls, *args)
|
||||
path = args[0]
|
||||
path = args[0].partition('#')[0]
|
||||
raw = open(path, 'rb').read()
|
||||
raw, obj.encoding = xml_to_unicode(raw)
|
||||
obj.character_count = character_count(raw)
|
||||
@ -63,9 +63,9 @@ def is_supported(path):
|
||||
return ext in list(MAP.keys())+['html', 'opf']
|
||||
|
||||
class EbookIterator(object):
|
||||
|
||||
|
||||
CHARACTERS_PER_PAGE = 1000
|
||||
|
||||
|
||||
def __init__(self, pathtoebook):
|
||||
self.pathtoebook = os.path.abspath(pathtoebook)
|
||||
self.config = DynamicConfig(name='iterator')
|
||||
@ -77,17 +77,17 @@ class EbookIterator(object):
|
||||
if ext not in map.keys():
|
||||
raise UnsupportedFormatError(ext)
|
||||
self.to_opf = map[ext]
|
||||
|
||||
|
||||
def search(self, text, index):
|
||||
text = text.lower()
|
||||
for i, path in enumerate(self.spine):
|
||||
if i > index:
|
||||
if text in open(path, 'rb').read().decode(path.encoding).lower():
|
||||
return i
|
||||
|
||||
|
||||
def find_embedded_fonts(self):
|
||||
'''
|
||||
This will become unnecessary once Qt WebKit supports the @font-face rule.
|
||||
This will become unnecessary once Qt WebKit supports the @font-face rule.
|
||||
'''
|
||||
for item in self.opf.manifest:
|
||||
if item.mime_type and 'css' in item.mime_type.lower():
|
||||
@ -108,7 +108,7 @@ class EbookIterator(object):
|
||||
print 'WARNING: Family aliasing not supported:', block
|
||||
else:
|
||||
print 'Loaded embedded font:', repr(family)
|
||||
|
||||
|
||||
def __enter__(self):
|
||||
self._tdir = TemporaryDirectory('_ebook_iter')
|
||||
self.base = self._tdir.__enter__()
|
||||
@ -116,50 +116,50 @@ class EbookIterator(object):
|
||||
self.pathtoopf = self.to_opf(self.pathtoebook, self.base, opts)
|
||||
self.opf = OPF(self.pathtoopf, os.path.dirname(self.pathtoopf))
|
||||
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)]
|
||||
|
||||
|
||||
if self.opf.path_to_html_toc is not None and \
|
||||
self.opf.path_to_html_toc not in self.spine:
|
||||
self.spine.append(SpineItem(self.opf.path_to_html_toc))
|
||||
|
||||
|
||||
|
||||
|
||||
sizes = [i.character_count for i in self.spine]
|
||||
self.pages = [math.ceil(i/float(self.CHARACTERS_PER_PAGE)) for i in sizes]
|
||||
for p, s in zip(self.pages, self.spine):
|
||||
s.pages = p
|
||||
start = 1
|
||||
|
||||
|
||||
|
||||
|
||||
for s in self.spine:
|
||||
s.start_page = start
|
||||
start += s.pages
|
||||
s.max_page = s.start_page + s.pages - 1
|
||||
self.toc = self.opf.toc
|
||||
|
||||
|
||||
self.find_embedded_fonts()
|
||||
self.read_bookmarks()
|
||||
|
||||
self.read_bookmarks()
|
||||
|
||||
return self
|
||||
|
||||
|
||||
def parse_bookmarks(self, raw):
|
||||
for line in raw.splitlines():
|
||||
if line.count('^') > 0:
|
||||
tokens = line.rpartition('^')
|
||||
title, ref = tokens[0], tokens[2]
|
||||
self.bookmarks.append((title, ref))
|
||||
|
||||
|
||||
def serialize_bookmarks(self, bookmarks):
|
||||
dat = []
|
||||
for title, bm in bookmarks:
|
||||
dat.append(u'%s^%s'%(title, bm))
|
||||
return (u'\n'.join(dat) +'\n').encode('utf-8')
|
||||
|
||||
|
||||
def read_bookmarks(self):
|
||||
self.bookmarks = []
|
||||
bmfile = os.path.join(self.base, 'META-INF', 'calibre_bookmarks.txt')
|
||||
@ -170,8 +170,8 @@ class EbookIterator(object):
|
||||
saved = self.config['bookmarks_'+self.pathtoebook]
|
||||
if saved:
|
||||
raw = saved
|
||||
self.parse_bookmarks(raw)
|
||||
|
||||
self.parse_bookmarks(raw)
|
||||
|
||||
def save_bookmarks(self, bookmarks=None):
|
||||
if bookmarks is None:
|
||||
bookmarks = self.bookmarks
|
||||
@ -190,7 +190,7 @@ class EbookIterator(object):
|
||||
zipf.writestr('META-INF/calibre_bookmarks.txt', dat)
|
||||
else:
|
||||
self.config['bookmarks_'+self.pathtoebook] = dat
|
||||
|
||||
|
||||
def add_bookmark(self, bm):
|
||||
dups = []
|
||||
for x in self.bookmarks:
|
||||
@ -200,6 +200,6 @@ class EbookIterator(object):
|
||||
self.bookmarks.remove(x)
|
||||
self.bookmarks.append(bm)
|
||||
self.save_bookmarks()
|
||||
|
||||
|
||||
def __exit__(self, *args):
|
||||
self._tdir.__exit__(*args)
|
||||
self._tdir.__exit__(*args)
|
||||
|
Loading…
x
Reference in New Issue
Block a user