Fix bug #3130: Generate TOC for FB2 and do not choke on gif images.

This commit is contained in:
John Schember 2009-08-11 18:14:51 -04:00
parent 98f2f556fa
commit eab0d189b0
2 changed files with 45 additions and 14 deletions

View File

@ -68,21 +68,13 @@ class FB2MLizer(object):
self.image_hrefs = {} self.image_hrefs = {}
self.link_hrefs = {} self.link_hrefs = {}
output = self.fb2_header() output = self.fb2_header()
if 'titlepage' in self.oeb_book.guide: output += self.get_cover_page()
self.log.debug('Generating cover page...') output += u'ghji87yhjko0Caliblre-toc-placeholder-for-insertion-later8ujko0987yjk'
href = self.oeb_book.guide['titlepage'].href output += self.get_text()
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.fb2_body_footer() output += self.fb2_body_footer()
output += self.fb2mlize_images() output += self.fb2mlize_images()
output += self.fb2_footer() output += self.fb2_footer()
output = output.replace(u'ghji87yhjko0Caliblre-toc-placeholder-for-insertion-later8ujko0987yjk', self.get_toc())
return u'<?xml version="1.0" encoding="UTF-8"?>\n%s' % etree.tostring(etree.fromstring(output), encoding=unicode, pretty_print=True) return u'<?xml version="1.0" encoding="UTF-8"?>\n%s' % etree.tostring(etree.fromstring(output), encoding=unicode, pretty_print=True)
def fb2_header(self): def fb2_header(self):
@ -113,6 +105,38 @@ class FB2MLizer(object):
author_last, self.oeb_book.metadata.title[0].value, author_last, self.oeb_book.metadata.title[0].value,
__appname__, __version__) __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'<p>%s</p>' % _('Table of Contents:')
for item in self.oeb_book.toc:
if item.href in self.link_hrefs.keys():
toc += '<p><a xlink:href="#%s">%s</a></p>\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): def fb2_body_footer(self):
return u'\n</section>\n</body>' return u'\n</section>\n</body>'
@ -135,7 +159,7 @@ class FB2MLizer(object):
for item in self.oeb_book.manifest: for item in self.oeb_book.manifest:
if item.media_type in OEB_RASTER_IMAGES: if item.media_type in OEB_RASTER_IMAGES:
try: try:
im = Image.open(cStringIO.StringIO(item.data)) im = Image.open(cStringIO.StringIO(item.data)).convert('RGB')
data = cStringIO.StringIO() data = cStringIO.StringIO()
im.save(data, 'JPEG') im.save(data, 'JPEG')
data = data.getvalue() data = data.getvalue()

View File

@ -6,7 +6,7 @@ __docformat__ = 'restructuredtext en'
import os import os
from calibre.customize.conversion import OutputFormatPlugin from calibre.customize.conversion import OutputFormatPlugin, OptionRecommendation
from calibre.ebooks.fb2.fb2ml import FB2MLizer from calibre.ebooks.fb2.fb2ml import FB2MLizer
class FB2Output(OutputFormatPlugin): class FB2Output(OutputFormatPlugin):
@ -15,6 +15,13 @@ class FB2Output(OutputFormatPlugin):
author = 'John Schember' author = 'John Schember'
file_type = 'fb2' 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): def convert(self, oeb_book, output_path, input_plugin, opts, log):
fb2mlizer = FB2MLizer(log) fb2mlizer = FB2MLizer(log)
fb2_content = fb2mlizer.extract_content(oeb_book, opts) fb2_content = fb2mlizer.extract_content(oeb_book, opts)