AZW3 Output: Fix a bug that caused popup footnotes in some AZW3 files to not work properly. The popup would show all remaining footnotes instead of only the current footnote. Fixes #1293290 [popup Comments can not be displayed correctly in azw3 file](https://bugs.launchpad.net/calibre/+bug/1293290)

This was caused by the offsets to links in the AZW3 generated by calibre
to be slightly incorrect, confusing the heuristic algorithm used by the
kindle to detect footnote extents.
This commit is contained in:
Kovid Goyal 2014-03-28 16:51:44 +05:30
parent 37b6242c3f
commit 69db89156e

View File

@ -201,7 +201,7 @@ class Chunker(object):
# Set internal links # Set internal links
text = b''.join(x.raw_text for x in self.skeletons) text = b''.join(x.raw_text for x in self.skeletons)
self.text = self.set_internal_links(text) self.text = self.set_internal_links(text, b''.join(x.rebuild() for x in self.skeletons))
def remove_namespaces(self, root): def remove_namespaces(self, root):
lang = None lang = None
@ -346,16 +346,16 @@ class Chunker(object):
cp += len(chunk.raw) cp += len(chunk.raw)
num += 1 num += 1
def set_internal_links(self, text): def set_internal_links(self, text, rebuilt_text):
''' Update the internal link placeholders to point to the correct ''' Update the internal link placeholders to point to the correct
location, based on the chunk table.''' location, based on the chunk table.'''
# A kindle:pos:fid link contains two base 32 numbers of the form # A kindle:pos:fid:off link contains two base 32 numbers of the form
# XXXX:YYYYYYYYYY # XXXX:YYYYYYYYYY
# The first number is an index into the chunk table and the second is # The first number is an index into the chunk table and the second is
# an offset from the start of the chunk to the start of the tag pointed # an offset from the start of the chunk to the start of the tag pointed
# to by the link. # to by the link.
aid_map = {} # Map of aid to (pos, fid) aid_map = {} # Map of aid to (fid, offset_from_start_of_chunk, offset_from_start_of_text)
for match in re.finditer(br'<[^>]+? aid=[\'"]([A-Z0-9]+)[\'"]', text): for match in re.finditer(br'<[^>]+? aid=[\'"]([A-Z0-9]+)[\'"]', rebuilt_text):
offset = match.start() offset = match.start()
pos_fid = None pos_fid = None
for chunk in self.chunk_table: for chunk in self.chunk_table:
@ -369,8 +369,8 @@ class Chunker(object):
pos_fid = (chunk.sequence_number, 0, offset) pos_fid = (chunk.sequence_number, 0, offset)
break break
if chunk is self.chunk_table[-1]: if chunk is self.chunk_table[-1]:
# This can happen for aids very close to the end of the the # This can happen for aids very close to the end of the
# end of the text (https://bugs.launchpad.net/bugs/1011330) # text (https://bugs.launchpad.net/bugs/1011330)
pos_fid = (chunk.sequence_number, offset-chunk.insert_pos, pos_fid = (chunk.sequence_number, offset-chunk.insert_pos,
offset) offset)
if pos_fid is None: if pos_fid is None: