KF8: Improved handling of startoffset

This commit is contained in:
Kovid Goyal 2012-04-23 14:39:57 +05:30
parent 2951a9c696
commit 9f7a30d378
3 changed files with 26 additions and 20 deletions

View File

@ -109,7 +109,7 @@ class Mobi8Reader(object):
table, cncx = read_index(self.kf8_sections, self.header.othidx, table, cncx = read_index(self.kf8_sections, self.header.othidx,
self.header.codec) self.header.codec)
Item = namedtuple('Item', Item = namedtuple('Item',
'type title div_frag_num') 'type title pos_fid')
for i, ref_type in enumerate(table.iterkeys()): for i, ref_type in enumerate(table.iterkeys()):
tag_map = table[ref_type] tag_map = table[ref_type]
@ -119,7 +119,7 @@ class Mobi8Reader(object):
if 3 in tag_map.keys(): if 3 in tag_map.keys():
fileno = tag_map[3][0] fileno = tag_map[3][0]
if 6 in tag_map.keys(): if 6 in tag_map.keys():
fileno = tag_map[6][0] fileno = tag_map[6]
self.guide.append(Item(ref_type.decode(self.header.codec), self.guide.append(Item(ref_type.decode(self.header.codec),
title, fileno)) title, fileno))
@ -287,23 +287,24 @@ class Mobi8Reader(object):
def create_guide(self): def create_guide(self):
guide = Guide() guide = Guide()
for ref_type, ref_title, fileno in self.guide: has_start = False
for ref_type, ref_title, pos_fid in self.guide:
try: try:
elem = self.elems[fileno] if len(pos_fid) != 2:
except IndexError:
# Happens for thumbnailstandard in Amazon book samples
continue continue
fi = self.get_file_info(elem.insert_pos) except TypeError:
idtext = self.get_id_tag(elem.insert_pos).decode(self.header.codec) continue # thumbnailstandard record, ignore it
linktgt = fi.filename linktgt, idtext = self.get_id_tag_by_pos_fid(*pos_fid)
if idtext: if idtext:
linktgt += b'#' + idtext linktgt += b'#' + idtext
g = Guide.Reference('%s/%s'%(fi.type, linktgt), os.getcwdu()) g = Guide.Reference(linktgt, os.getcwdu())
g.title, g.type = ref_title, ref_type g.title, g.type = ref_title, ref_type
if g.title == 'start' or g.type == 'text':
has_start = True
guide.append(g) guide.append(g)
so = self.header.exth.start_offset so = self.header.exth.start_offset
if so not in {None, NULL_INDEX}: if so not in {None, NULL_INDEX} and not has_start:
fi = self.get_file_info(so) fi = self.get_file_info(so)
if fi.filename is not None: if fi.filename is not None:
idtext = self.get_id_tag(so).decode(self.header.codec) idtext = self.get_id_tag(so).decode(self.header.codec)

View File

@ -153,8 +153,13 @@ def build_exth(metadata, prefer_author_sort=False, is_periodical=False,
nrecs += 1 nrecs += 1
if start_offset is not None: if start_offset is not None:
try:
len(start_offset)
except TypeError:
start_offset = [start_offset]
for so in start_offset:
exth.write(pack(b'>III', EXTH_CODES['startreading'], 12, exth.write(pack(b'>III', EXTH_CODES['startreading'], 12,
start_offset)) so))
nrecs += 1 nrecs += 1
if num_of_resources is not None: if num_of_resources is not None:

View File

@ -368,11 +368,11 @@ class KF8Writer(object):
if aid is None: if aid is None:
continue continue
pos, fid = self.aid_offset_map[aid] pos, fid = self.aid_offset_map[aid]
if is_guide_ref_start(ref) and fid == 0: if is_guide_ref_start(ref):
# If fid != 0 then we cannot represent the start position as a chunk = self.chunk_table[pos]
# single number in the EXTH header, so we do not write it to skel = [s for s in self.skel_table if s.file_number ==
# EXTH chunk.file_number][0]
self.start_offset = pos self.start_offset = skel.start_pos + skel.length + chunk.start_pos + fid
self.guide_table.append(GuideRef(ref.title or self.guide_table.append(GuideRef(ref.title or
_('Unknown'), ref.type, (pos, fid))) _('Unknown'), ref.type, (pos, fid)))