From e43cb7da48b8b9d7b7eff57d0e7cbb947ab54d6c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 16 Jul 2011 11:42:10 -0600 Subject: [PATCH] MOBI Output: Add an option to place the automatically generated TOC at the start of the book. It is highly recommended that this option not be used, unless you know what you are doing. See http://manual.calibre-ebook.com/faq.html#what-s-the-deal-with-table-of-contents-in-mobi-files --- src/calibre/ebooks/mobi/output.py | 11 +++++++++-- src/calibre/ebooks/oeb/transforms/htmltoc.py | 8 ++++++-- src/calibre/gui2/convert/mobi_output.py | 2 +- src/calibre/gui2/convert/mobi_output.ui | 19 +++++++++++++------ src/calibre/manual/faq.rst | 2 +- 5 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/calibre/ebooks/mobi/output.py b/src/calibre/ebooks/mobi/output.py index a6f6c52b7f..f96f6bf14f 100644 --- a/src/calibre/ebooks/mobi/output.py +++ b/src/calibre/ebooks/mobi/output.py @@ -27,7 +27,7 @@ class MOBIOutput(OutputFormatPlugin): ), OptionRecommendation(name='no_inline_toc', recommended_value=False, level=OptionRecommendation.LOW, - help=_('Don\'t add Table of Contents to end of book. Useful if ' + help=_('Don\'t add Table of Contents to the book. Useful if ' 'the book has its own table of contents.')), OptionRecommendation(name='toc_title', recommended_value=None, help=_('Title for any generated in-line table of contents.') @@ -45,6 +45,12 @@ class MOBIOutput(OutputFormatPlugin): 'the MOBI output plugin will try to convert margins specified' ' in the input document, otherwise it will ignore them.') ), + OptionRecommendation(name='mobi_toc_at_start', + recommended_value=False, + help=_('When adding the Table of Contents to the book, add it at the start of the ' + 'book instead of the end. Not recommended.') + ), + ]) def check_for_periodical(self): @@ -167,7 +173,8 @@ class MOBIOutput(OutputFormatPlugin): from calibre.customize.ui import plugin_for_input_format imagemax = PALM_MAX_IMAGE_SIZE if opts.rescale_images else None if not opts.no_inline_toc: - tocadder = HTMLTOCAdder(title=opts.toc_title) + tocadder = HTMLTOCAdder(title=opts.toc_title, position='start' if + opts.mobi_toc_at_start else 'end') tocadder(oeb, opts) mangler = CaseMangler() mangler(oeb, opts) diff --git a/src/calibre/ebooks/oeb/transforms/htmltoc.py b/src/calibre/ebooks/oeb/transforms/htmltoc.py index b570420756..9f0fd21754 100644 --- a/src/calibre/ebooks/oeb/transforms/htmltoc.py +++ b/src/calibre/ebooks/oeb/transforms/htmltoc.py @@ -45,9 +45,10 @@ body > .calibre_toc_block { } class HTMLTOCAdder(object): - def __init__(self, title=None, style='nested'): + def __init__(self, title=None, style='nested', position='end'): self.title = title self.style = style + self.position = position @classmethod def config(cls, cfg): @@ -98,7 +99,10 @@ class HTMLTOCAdder(object): self.add_toc_level(body, oeb.toc) id, href = oeb.manifest.generate('contents', 'contents.xhtml') item = oeb.manifest.add(id, href, XHTML_MIME, data=contents) - oeb.spine.add(item, linear=False) + if self.position == 'end': + oeb.spine.add(item, linear=False) + else: + oeb.spine.insert(0, item, linear=True) oeb.guide.add('toc', 'Table of Contents', href) def add_toc_level(self, elem, toc): diff --git a/src/calibre/gui2/convert/mobi_output.py b/src/calibre/gui2/convert/mobi_output.py index be9c5b4658..fbd9ad4fcb 100644 --- a/src/calibre/gui2/convert/mobi_output.py +++ b/src/calibre/gui2/convert/mobi_output.py @@ -24,7 +24,7 @@ class PluginWidget(Widget, Ui_Form): def __init__(self, parent, get_option, get_help, db=None, book_id=None): Widget.__init__(self, parent, ['prefer_author_sort', 'rescale_images', 'toc_title', - 'mobi_ignore_margins', + 'mobi_ignore_margins', 'mobi_toc_at_start', 'dont_compress', 'no_inline_toc', 'masthead_font','personal_doc'] ) from calibre.utils.fonts import fontconfig diff --git a/src/calibre/gui2/convert/mobi_output.ui b/src/calibre/gui2/convert/mobi_output.ui index e9eab45e1a..7601f80df9 100644 --- a/src/calibre/gui2/convert/mobi_output.ui +++ b/src/calibre/gui2/convert/mobi_output.ui @@ -27,21 +27,21 @@ - + Rescale images for &Palm devices - + Use author &sort for author - + Disable compression of the file contents @@ -55,7 +55,7 @@ - + Kindle options @@ -101,7 +101,7 @@ - + Qt::Vertical @@ -114,7 +114,14 @@ - + + + + Put generated Table of Contents at &start of book instead of end + + + + Ignore &margins diff --git a/src/calibre/manual/faq.rst b/src/calibre/manual/faq.rst index b3fdba215e..ea6f243304 100644 --- a/src/calibre/manual/faq.rst +++ b/src/calibre/manual/faq.rst @@ -61,7 +61,7 @@ In the MOBI format, the situation is a little confused. This is because the MOBI Now it might well seem to you that the MOBI book has two identical TOCs. Remember that one is semantically a content TOC and the other is a metadata TOC, even though both might have exactly the same entries and look the same. One can be accessed directly from the Kindle's menus, the other cannot. -When converting to MOBI, calibre detects the *metadata TOC* in the input document and generates an end-of-file TOC in the output MOBI file. You can turn this off by an option in the MOBI Output settings. You cannot control where this generated TOC will go. Remember this TOC is semantically a *metadata TOC*, in any format other than MOBI it *cannot not be part of the text*. The fact that it is part of the text in MOBI is an accident caused by the limitations of MOBI. If you want a TOC at a particular location in your document text, create one by hand. +When converting to MOBI, calibre detects the *metadata TOC* in the input document and generates an end-of-file TOC in the output MOBI file. You can turn this off by an option in the MOBI Output settings. You can also tell calibre whether to put it and the start or the end of the book via an option in the MOBI Output settings. Remember this TOC is semantically a *metadata TOC*, in any format other than MOBI it *cannot not be part of the text*. The fact that it is part of the text in MOBI is an accident caused by the limitations of MOBI. If you want a TOC at a particular location in your document text, create one by hand. So we strongly recommend that you leave the default as it is, i.e. with the metadata TOC at the end of the book. If you have a hand edited TOC in the input document, you can use the TOC detection options in calibre to automatically generate the metadata TOC from it. See the conversion section of the User Manual for more details on how to use these options.