mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
New mobi output: Use same algorithm for chapter to chapter navigation as the old mobi writer
This commit is contained in:
parent
c16fdd9af7
commit
e326c63498
@ -661,13 +661,12 @@ class Indexer(object): # {{{
|
|||||||
def create_book_index(self): # {{{
|
def create_book_index(self): # {{{
|
||||||
self.book_has_subchapters = False
|
self.book_has_subchapters = False
|
||||||
indices = []
|
indices = []
|
||||||
seen, sub_seen = set(), set()
|
seen = set()
|
||||||
id_offsets = self.serializer.id_offsets
|
id_offsets = self.serializer.id_offsets
|
||||||
|
|
||||||
# Flatten toc to contain only chapters and subchapters
|
# Flatten toc so that chapter to chapter jumps work with all sub
|
||||||
# Anything deeper than a subchapter is made into a subchapter
|
# chapter levels as well
|
||||||
chapters = []
|
for node in self.oeb.toc.iterdescendants():
|
||||||
for node in self.oeb.toc:
|
|
||||||
try:
|
try:
|
||||||
offset = id_offsets[node.href]
|
offset = id_offsets[node.href]
|
||||||
label = self.cncx[node.title]
|
label = self.cncx[node.title]
|
||||||
@ -680,77 +679,33 @@ class Indexer(object): # {{{
|
|||||||
continue
|
continue
|
||||||
seen.add(offset)
|
seen.add(offset)
|
||||||
|
|
||||||
subchapters = []
|
indices.append(BookIndexEntry(offset, label))
|
||||||
chapters.append((offset, label, subchapters))
|
|
||||||
|
|
||||||
for descendant in node.iterdescendants():
|
indices.sort(key=lambda x:x.offset)
|
||||||
try:
|
|
||||||
offset = id_offsets[descendant.href]
|
|
||||||
label = self.cncx[descendant.title]
|
|
||||||
except:
|
|
||||||
self.log.warn('TOC item %s [%s] not found in document'%(
|
|
||||||
descendant.title, descendant.href))
|
|
||||||
continue
|
|
||||||
|
|
||||||
if offset in sub_seen:
|
# Set lengths
|
||||||
continue
|
for i, index in enumerate(indices):
|
||||||
sub_seen.add(offset)
|
try:
|
||||||
subchapters.append((offset, label))
|
next_offset = indices[i+1].offset
|
||||||
|
except:
|
||||||
|
next_offset = self.serializer.body_end_offset
|
||||||
|
index.length = next_offset - index.offset
|
||||||
|
|
||||||
subchapters.sort(key=lambda x:x[0])
|
|
||||||
|
|
||||||
chapters.sort(key=lambda x:x[0])
|
# Remove empty indices
|
||||||
|
indices = [x for x in indices if x[0].length > 0]
|
||||||
|
|
||||||
chapters = [(BookIndexEntry(x[0], x[1]), [
|
# Reset lengths in case any were removed
|
||||||
BookIndexEntry(y[0], y[1]) for y in x[2]]) for x in chapters]
|
for i, index in enumerate(indices):
|
||||||
|
try:
|
||||||
def set_length(indices):
|
next_offset = indices[i+1].offset
|
||||||
for i, index in enumerate(indices):
|
except:
|
||||||
try:
|
next_offset = self.serializer.body_end_offset
|
||||||
next_offset = indices[i+1].offset
|
index.length = next_offset - index.offset
|
||||||
except:
|
|
||||||
next_offset = self.serializer.body_end_offset
|
|
||||||
index.length = next_offset - index.offset
|
|
||||||
|
|
||||||
# Set chapter and subchapter lengths
|
|
||||||
set_length([x[0] for x in chapters])
|
|
||||||
for x in chapters:
|
|
||||||
set_length(x[1])
|
|
||||||
|
|
||||||
# Remove empty chapters
|
|
||||||
chapters = [x for x in chapters if x[0].length > 0]
|
|
||||||
|
|
||||||
# Remove invalid subchapters
|
|
||||||
for i, x in enumerate(list(chapters)):
|
|
||||||
chapter, subchapters = x
|
|
||||||
ok_subchapters = []
|
|
||||||
for sc in subchapters:
|
|
||||||
if sc.offset < chapter.next_offset and sc.length > 0:
|
|
||||||
ok_subchapters.append(sc)
|
|
||||||
chapters[i] = (chapter, ok_subchapters)
|
|
||||||
|
|
||||||
# Reset chapter and subchapter lengths in case any were removed
|
|
||||||
set_length([x[0] for x in chapters])
|
|
||||||
for x in chapters:
|
|
||||||
set_length(x[1])
|
|
||||||
|
|
||||||
# Set index and depth values
|
# Set index and depth values
|
||||||
indices = []
|
for index, x in enumerate(indices):
|
||||||
for index, x in enumerate(chapters):
|
|
||||||
x[0].index = index
|
x[0].index = index
|
||||||
indices.append(x[0])
|
|
||||||
|
|
||||||
for chapter, subchapters in chapters:
|
|
||||||
for sc in subchapters:
|
|
||||||
index += 1
|
|
||||||
sc.index = index
|
|
||||||
sc.parent_index = chapter.index
|
|
||||||
indices.append(sc)
|
|
||||||
sc.depth = 1
|
|
||||||
self.book_has_subchapters = True
|
|
||||||
if subchapters:
|
|
||||||
chapter.first_child_index = subchapters[0].index
|
|
||||||
chapter.last_child_index = subchapters[-1].index
|
|
||||||
|
|
||||||
return indices
|
return indices
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user