From 2951a9c696762aeabf17ad563bc043a37bb1ceab Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 23 Apr 2012 13:19:14 +0530 Subject: [PATCH] KF8 debug: Dump the guide --- src/calibre/ebooks/mobi/debug/index.py | 24 ++++++++++++++++++++++++ src/calibre/ebooks/mobi/debug/mobi8.py | 9 ++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/calibre/ebooks/mobi/debug/index.py b/src/calibre/ebooks/mobi/debug/index.py index 94f252e231..6065d12e5e 100644 --- a/src/calibre/ebooks/mobi/debug/index.py +++ b/src/calibre/ebooks/mobi/debug/index.py @@ -21,6 +21,8 @@ Elem = namedtuple('Chunk', 'insert_pos toc_text file_number sequence_number start_pos ' 'length') +GuideRef = namedtuple('GuideRef', 'type title pos_fid') + def read_index(sections, idx, 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): def __init__(self, ncxidx, records, codec): diff --git a/src/calibre/ebooks/mobi/debug/mobi8.py b/src/calibre/ebooks/mobi/debug/mobi8.py index 4306d565e2..a91213f889 100644 --- a/src/calibre/ebooks/mobi/debug/mobi8.py +++ b/src/calibre/ebooks/mobi/debug/mobi8.py @@ -12,7 +12,8 @@ from itertools import izip from calibre import CurrentDir 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.debug import format_bytes from calibre.ebooks.mobi.reader.headers import NULL_INDEX @@ -114,6 +115,8 @@ class MOBIFile(object): self.header.encoding) self.ncx_index = NCXIndex(self.header.primary_index_record, self.mf.records, self.header.encoding) + self.guide_index = GuideIndex(self.header.oth_idx, self.mf.records, + self.header.encoding) def build_files(self): 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: 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: part.dump(os.path.join(ddir, 'files'))