KF8 debug: Dump the guide

This commit is contained in:
Kovid Goyal 2012-04-23 13:19:14 +05:30
parent b13b7f8a50
commit 2951a9c696
2 changed files with 32 additions and 1 deletions

View File

@ -21,6 +21,8 @@ Elem = namedtuple('Chunk',
'insert_pos toc_text file_number sequence_number start_pos ' 'insert_pos toc_text file_number sequence_number start_pos '
'length') 'length')
GuideRef = namedtuple('GuideRef', 'type title pos_fid')
def read_index(sections, idx, codec): def read_index(sections, idx, codec):
table, cncx = OrderedDict(), CNCX([], codec) table, cncx = OrderedDict(), CNCX([], codec)
@ -124,6 +126,28 @@ class SECTIndex(Index):
) )
) )
class GuideIndex(Index):
def __init__(self, guideidx, records, codec):
super(GuideIndex, self).__init__(guideidx, records, codec)
self.records = []
if self.table is not None:
for i, text in enumerate(self.table.iterkeys()):
tag_map = self.table[text]
if set(tag_map.iterkeys()) not in ({1, 6}, {1, 2, 3}):
raise ValueError('Guide Index has unknown tags: %s'%
tag_map)
title = self.cncx[tag_map[1][0]]
self.records.append(GuideRef(
text,
title,
tag_map[6] if 6 in tag_map else (tag_map[2], tag_map[3])
)
)
class NCXIndex(Index): class NCXIndex(Index):
def __init__(self, ncxidx, records, codec): def __init__(self, ncxidx, records, codec):

View File

@ -12,7 +12,8 @@ from itertools import izip
from calibre import CurrentDir from calibre import CurrentDir
from calibre.ebooks.mobi.debug.headers import TextRecord from calibre.ebooks.mobi.debug.headers import TextRecord
from calibre.ebooks.mobi.debug.index import (SKELIndex, SECTIndex, NCXIndex) from calibre.ebooks.mobi.debug.index import (SKELIndex, SECTIndex, NCXIndex,
GuideIndex)
from calibre.ebooks.mobi.utils import read_font_record from calibre.ebooks.mobi.utils import read_font_record
from calibre.ebooks.mobi.debug import format_bytes from calibre.ebooks.mobi.debug import format_bytes
from calibre.ebooks.mobi.reader.headers import NULL_INDEX from calibre.ebooks.mobi.reader.headers import NULL_INDEX
@ -114,6 +115,8 @@ class MOBIFile(object):
self.header.encoding) self.header.encoding)
self.ncx_index = NCXIndex(self.header.primary_index_record, self.ncx_index = NCXIndex(self.header.primary_index_record,
self.mf.records, self.header.encoding) self.mf.records, self.header.encoding)
self.guide_index = GuideIndex(self.header.oth_idx, self.mf.records,
self.header.encoding)
def build_files(self): def build_files(self):
text = self.raw_text text = self.raw_text
@ -211,6 +214,10 @@ def inspect_mobi(mobi_file, ddir):
with open(os.path.join(ddir, 'ncx.record'), 'wb') as fo: with open(os.path.join(ddir, 'ncx.record'), 'wb') as fo:
fo.write(str(f.ncx_index).encode('utf-8')) fo.write(str(f.ncx_index).encode('utf-8'))
with open(os.path.join(ddir, 'guide.record'), 'wb') as fo:
fo.write(str(f.guide_index).encode('utf-8'))
for part in f.files: for part in f.files:
part.dump(os.path.join(ddir, 'files')) part.dump(os.path.join(ddir, 'files'))