This commit is contained in:
Kovid Goyal 2011-07-26 22:17:50 -06:00
parent 7665453242
commit 3453746d90
4 changed files with 12 additions and 10 deletions

View File

@ -844,6 +844,7 @@ class TextRecord(object): # {{{
def __init__(self, idx, record, extra_data_flags, decompress): def __init__(self, idx, record, extra_data_flags, decompress):
self.trailing_data, self.raw = get_trailing_data(record.raw, extra_data_flags) self.trailing_data, self.raw = get_trailing_data(record.raw, extra_data_flags)
raw_trailing_bytes = record.raw[len(self.raw):]
self.raw = decompress(self.raw) self.raw = decompress(self.raw)
if 0 in self.trailing_data: if 0 in self.trailing_data:
self.trailing_data['multibyte_overlap'] = self.trailing_data.pop(0) self.trailing_data['multibyte_overlap'] = self.trailing_data.pop(0)
@ -851,6 +852,7 @@ class TextRecord(object): # {{{
self.trailing_data['indexing'] = self.trailing_data.pop(1) self.trailing_data['indexing'] = self.trailing_data.pop(1)
if 2 in self.trailing_data: if 2 in self.trailing_data:
self.trailing_data['uncrossable_breaks'] = self.trailing_data.pop(2) self.trailing_data['uncrossable_breaks'] = self.trailing_data.pop(2)
self.trailing_data['raw_bytes'] = raw_trailing_bytes
self.idx = idx self.idx = idx

View File

@ -191,7 +191,7 @@ def encode_trailing_data(raw):
<data><size> <data><size>
where size is a backwards encoded vwi whose value is the length of the where size is a backwards encoded vwi whose value is the length of the
entire return bytestring. entire returned bytestring. data is the bytestring passed in as raw.
This is the encoding used for trailing data entries at the end of text This is the encoding used for trailing data entries at the end of text
records. See get_trailing_data() for details. records. See get_trailing_data() for details.

View File

@ -14,7 +14,7 @@ from collections import OrderedDict, defaultdict
from calibre.ebooks.mobi.writer2 import RECORD_SIZE from calibre.ebooks.mobi.writer2 import RECORD_SIZE
from calibre.ebooks.mobi.utils import (encint, encode_number_as_hex, from calibre.ebooks.mobi.utils import (encint, encode_number_as_hex,
encode_trailing_data, encode_tbs, align_block, utf8_text) encode_tbs, align_block, utf8_text)
class CNCX(object): # {{{ class CNCX(object): # {{{
@ -198,7 +198,7 @@ class TBS(object): # {{{
# This can happen if a record contains only text between # This can happen if a record contains only text between
# the periodical start and the first section # the periodical start and the first section
byts = self.type_011 byts = self.type_011
self.bytestring = encode_trailing_data(byts) self.bytestring = byts
else: else:
depth_map = defaultdict(list) depth_map = defaultdict(list)
for x in ('starts', 'ends', 'completes'): for x in ('starts', 'ends', 'completes'):
@ -209,7 +209,7 @@ class TBS(object): # {{{
self.periodical_tbs(data, first, depth_map) self.periodical_tbs(data, first, depth_map)
else: else:
if not data: if not data:
self.bytestring = encode_trailing_data(b'') self.bytestring = b''
else: else:
self.book_tbs(data, first) self.book_tbs(data, first)
@ -302,10 +302,10 @@ class TBS(object): # {{{
buf.write(encode_tbs(spanner.index - parent_section_index, buf.write(encode_tbs(spanner.index - parent_section_index,
{0b0001: 0})) {0b0001: 0}))
self.bytestring = encode_trailing_data(buf.getvalue()) self.bytestring = buf.getvalue()
def book_tbs(self, data, first): def book_tbs(self, data, first):
self.bytestring = encode_trailing_data(b'') self.bytestring = b''
# }}} # }}}
class Indexer(object): # {{{ class Indexer(object): # {{{

View File

@ -99,7 +99,7 @@ class MobiWriter(object):
for i in xrange(len(self.records)): for i in xrange(len(self.records)):
if i == 0: continue if i == 0: continue
tbs = self.indexer.get_trailing_byte_sequence(i) tbs = self.indexer.get_trailing_byte_sequence(i)
self.records[i] += tbs self.records[i] += encode_trailing_data(tbs)
self.records.extend(self.indexer.records) self.records.extend(self.indexer.records)
@property @property
@ -212,15 +212,15 @@ class MobiWriter(object):
if self.compression == PALMDOC: if self.compression == PALMDOC:
data = compress_doc(data) data = compress_doc(data)
record = StringIO() record = StringIO()
record.write(data)
self.records.append(record.getvalue())
nrecords += 1 nrecords += 1
data, overlap = self.read_text_record(text) data, overlap = self.read_text_record(text)
record.write(data)
# Write information about the mutibyte character overlap, if any # Write information about the multibyte character overlap, if any
record.write(overlap) record.write(overlap)
record.write(pack(b'>B', len(overlap))) record.write(pack(b'>B', len(overlap)))
self.records.append(record.getvalue())
self.last_text_record_idx = nrecords self.last_text_record_idx = nrecords