MOBI Output: Make generating the navpoints for chapter to chapter skipping more robust. Also geenrate navpoints for every item in the TOC, not just the items at the deepest level.

This commit is contained in:
Kovid Goyal 2011-07-29 17:11:50 -06:00
parent 7c3aaf5864
commit d72b9ff142
4 changed files with 47 additions and 3 deletions

View File

@ -50,6 +50,13 @@ class MOBIOutput(OutputFormatPlugin):
help=_('When adding the Table of Contents to the book, add it at the start of the ' help=_('When adding the Table of Contents to the book, add it at the start of the '
'book instead of the end. Not recommended.') 'book instead of the end. Not recommended.')
), ),
OptionRecommendation(name='mobi_navpoints_only_deepest',
recommended_value=False,
help=_('When adding navpoints for the chapter-to-chapter'
' navigation on the kindle, use only the lowest level '
'of items in the TOC, instead of items at every level.')
),
OptionRecommendation(name='kindlegen', OptionRecommendation(name='kindlegen',
recommended_value=False, recommended_value=False,
help=('Use kindlegen (must be in your PATH) to generate the' help=('Use kindlegen (must be in your PATH) to generate the'

View File

@ -1231,6 +1231,9 @@ class MobiWriter(object):
self._oeb.logger.info(' Compressing markup content...') self._oeb.logger.info(' Compressing markup content...')
data, overlap = self._read_text_record(text) data, overlap = self._read_text_record(text)
if not self.opts.mobi_periodical:
self._flatten_toc()
# Evaluate toc for conformance # Evaluate toc for conformance
if self.opts.mobi_periodical : if self.opts.mobi_periodical :
self._oeb.logger.info(' MOBI periodical specified, evaluating TOC for periodical conformance ...') self._oeb.logger.info(' MOBI periodical specified, evaluating TOC for periodical conformance ...')
@ -1697,6 +1700,32 @@ class MobiWriter(object):
# Index {{{ # Index {{{
def _flatten_toc(self):
'''
Flatten and re-order entries in TOC so that chapter to chapter jumping
never fails on the Kindle.
'''
from calibre.ebooks.oeb.base import TOC
items = list(self._oeb.toc.iterdescendants())
if self.opts.mobi_navpoints_only_deepest:
items = [i for i in items if i.depth == 1]
offsets = {i:self._id_offsets.get(i.href, -1) for i in items if i.href}
items = [i for i in items if offsets[i] > -1]
items.sort(key=lambda i:offsets[i])
filt = []
seen = set()
for i in items:
off = offsets[i]
if off in seen: continue
seen.add(off)
filt.append(i)
items = filt
newtoc = TOC()
for c, i in enumerate(items):
newtoc.add(i.title, i.href, play_order=c+1, id=str(c),
klass='chapter')
self._oeb.toc = newtoc
def _generate_index(self): def _generate_index(self):
self._oeb.log('Generating INDX ...') self._oeb.log('Generating INDX ...')
self._primary_index_record = None self._primary_index_record = None

View File

@ -25,7 +25,8 @@ class PluginWidget(Widget, Ui_Form):
Widget.__init__(self, parent, Widget.__init__(self, parent,
['prefer_author_sort', 'rescale_images', 'toc_title', ['prefer_author_sort', 'rescale_images', 'toc_title',
'mobi_ignore_margins', 'mobi_toc_at_start', 'mobi_ignore_margins', 'mobi_toc_at_start',
'dont_compress', 'no_inline_toc', 'masthead_font','personal_doc'] 'dont_compress', 'no_inline_toc',
'masthead_font','personal_doc', 'mobi_navpoints_only_deepest']
) )
from calibre.utils.fonts import fontconfig from calibre.utils.fonts import fontconfig
self.db, self.book_id = db, book_id self.db, self.book_id = db, book_id

View File

@ -55,7 +55,7 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="8" column="0" colspan="2"> <item row="9" column="0" colspan="2">
<widget class="QGroupBox" name="groupBox"> <widget class="QGroupBox" name="groupBox">
<property name="title"> <property name="title">
<string>Kindle options</string> <string>Kindle options</string>
@ -101,7 +101,7 @@
</layout> </layout>
</widget> </widget>
</item> </item>
<item row="9" column="0"> <item row="10" column="0">
<spacer name="verticalSpacer_2"> <spacer name="verticalSpacer_2">
<property name="orientation"> <property name="orientation">
<enum>Qt::Vertical</enum> <enum>Qt::Vertical</enum>
@ -128,6 +128,13 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="7" column="0" colspan="2">
<widget class="QCheckBox" name="opt_mobi_navpoints_only_deepest">
<property name="text">
<string>Use only &amp;lowest level of items in the TOC for chapter-to-chapter navigation</string>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
<resources/> <resources/>