diff --git a/src/calibre/ebooks/fb2/fb2ml.py b/src/calibre/ebooks/fb2/fb2ml.py index c0ea8ea197..a30fdd94f4 100644 --- a/src/calibre/ebooks/fb2/fb2ml.py +++ b/src/calibre/ebooks/fb2/fb2ml.py @@ -68,21 +68,13 @@ class FB2MLizer(object): self.image_hrefs = {} self.link_hrefs = {} output = self.fb2_header() - if 'titlepage' in self.oeb_book.guide: - self.log.debug('Generating cover page...') - href = self.oeb_book.guide['titlepage'].href - item = self.oeb_book.manifest.hrefs[href] - if item.spine_position is None: - stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts.output_profile) - output += self.dump_text(item.data.find(XHTML('body')), stylizer, item) - for item in self.oeb_book.spine: - self.log.debug('Converting %s to FictionBook2 XML' % item.href) - stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts.output_profile) - output += self.add_page_anchor(item) - output += self.dump_text(item.data.find(XHTML('body')), stylizer, item) + output += self.get_cover_page() + output += u'ghji87yhjko0Caliblre-toc-placeholder-for-insertion-later8ujko0987yjk' + output += self.get_text() output += self.fb2_body_footer() output += self.fb2mlize_images() output += self.fb2_footer() + output = output.replace(u'ghji87yhjko0Caliblre-toc-placeholder-for-insertion-later8ujko0987yjk', self.get_toc()) return u'\n%s' % etree.tostring(etree.fromstring(output), encoding=unicode, pretty_print=True) def fb2_header(self): @@ -113,6 +105,38 @@ class FB2MLizer(object): author_last, self.oeb_book.metadata.title[0].value, __appname__, __version__) + def get_cover_page(self): + output = u'' + if 'titlepage' in self.oeb_book.guide: + self.log.debug('Generating cover page...') + href = self.oeb_book.guide['titlepage'].href + item = self.oeb_book.manifest.hrefs[href] + if item.spine_position is None: + stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts.output_profile) + output += self.dump_text(item.data.find(XHTML('body')), stylizer, item) + return output + + def get_toc(self): + toc = u'' + if not self.opts.no_inline_toc: + self.log.debug('Generating table of contents...') + toc += u'

%s

' % _('Table of Contents:') + for item in self.oeb_book.toc: + if item.href in self.link_hrefs.keys(): + toc += '

%s

\n' % (self.link_hrefs[item.href], item.title) + else: + self.oeb.warn('Ignoring toc item: %s not found in document.' % item) + return toc + + def get_text(self): + text = u'' + for item in self.oeb_book.spine: + self.log.debug('Converting %s to FictionBook2 XML' % item.href) + stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts.output_profile) + text += self.add_page_anchor(item) + text += self.dump_text(item.data.find(XHTML('body')), stylizer, item) + return text + def fb2_body_footer(self): return u'\n\n' @@ -135,7 +159,7 @@ class FB2MLizer(object): for item in self.oeb_book.manifest: if item.media_type in OEB_RASTER_IMAGES: try: - im = Image.open(cStringIO.StringIO(item.data)) + im = Image.open(cStringIO.StringIO(item.data)).convert('RGB') data = cStringIO.StringIO() im.save(data, 'JPEG') data = data.getvalue() diff --git a/src/calibre/ebooks/fb2/output.py b/src/calibre/ebooks/fb2/output.py index af04179e9a..d68fb4ddad 100644 --- a/src/calibre/ebooks/fb2/output.py +++ b/src/calibre/ebooks/fb2/output.py @@ -6,7 +6,7 @@ __docformat__ = 'restructuredtext en' import os -from calibre.customize.conversion import OutputFormatPlugin +from calibre.customize.conversion import OutputFormatPlugin, OptionRecommendation from calibre.ebooks.fb2.fb2ml import FB2MLizer class FB2Output(OutputFormatPlugin): @@ -15,6 +15,13 @@ class FB2Output(OutputFormatPlugin): author = 'John Schember' file_type = 'fb2' + options = set([ + OptionRecommendation(name='no_inline_toc', + recommended_value=False, level=OptionRecommendation.LOW, + help=_('Don\'t add Table of Contents to the book. Useful if ' + 'the book has its own table of contents.')), + ]) + def convert(self, oeb_book, output_path, input_plugin, opts, log): fb2mlizer = FB2MLizer(log) fb2_content = fb2mlizer.extract_content(oeb_book, opts)