Optional inline TOC for RB, PDB, TXT, FB2 output

This commit is contained in:
Kovid Goyal 2009-08-11 20:16:08 -06:00
commit 98af0f1f99
19 changed files with 273 additions and 68 deletions

View File

@ -42,6 +42,7 @@ class Device(DeviceConfig, DevicePlugin):
WINDOWS_MAIN_MEM = None WINDOWS_MAIN_MEM = None
WINDOWS_CARD_A_MEM = None WINDOWS_CARD_A_MEM = None
WINDOWS_CARD_B_MEM = None WINDOWS_CARD_B_MEM = None
ALLOW_NO_MAIN_MEMORY = False
# The following are used by the check_ioreg_line method and can be either: # The following are used by the check_ioreg_line method and can be either:
# None, a string, a list of strings or a compiled regular expression # None, a string, a list of strings or a compiled regular expression
@ -266,6 +267,9 @@ class Device(DeviceConfig, DevicePlugin):
drives['cardb'] = self.windows_get_drive_prefix(drive) drives['cardb'] = self.windows_get_drive_prefix(drive)
elif self.windows_match_device(drive, 'WINDOWS_MAIN_MEM') and not drives.get('main', None): elif self.windows_match_device(drive, 'WINDOWS_MAIN_MEM') and not drives.get('main', None):
drives['main'] = self.windows_get_drive_prefix(drive) drives['main'] = self.windows_get_drive_prefix(drive)
if not self.ALLOW_NO_MAIN_MEMORY:
raise DeviceError('Failed to find the drive corresponding'
' to the main memory')
if 'main' in drives.keys() and 'carda' in drives.keys() and \ if 'main' in drives.keys() and 'carda' in drives.keys() and \
'cardb' in drives.keys(): 'cardb' in drives.keys():

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 self.opts.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,12 @@ class FB2Output(OutputFormatPlugin):
author = 'John Schember' author = 'John Schember'
file_type = 'fb2' file_type = 'fb2'
options = set([
OptionRecommendation(name='inline_toc',
recommended_value=False, level=OptionRecommendation.LOW,
help=_('Add Table of Contents to begenning of the book.')),
])
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)

View File

@ -1,37 +0,0 @@
# -*- coding: utf-8 -*-
__license__ = 'GPL 3'
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
__docformat__ = 'restructuredtext en'
import os
from calibre.customize.conversion import OutputFormatPlugin
from calibre.ebooks.pdb.ereader.writer import Writer
class EREADEROutput(OutputFormatPlugin):
name = 'eReader PDB Output'
author = 'John Schember'
file_type = 'erpdb'
def convert(self, oeb_book, output_path, input_plugin, opts, log):
writer = Writer(log)
close = False
if not hasattr(output_path, 'write'):
close = True
if not os.path.exists(os.path.dirname(output_path)) and os.path.dirname(output_path) != '':
os.makedirs(os.path.dirname(output_path))
out_stream = open(output_path, 'wb')
else:
out_stream = output_path
out_stream.seek(0)
out_stream.truncate()
writer.dump(oeb_book, out_stream)
if close:
out_stream.close()

View File

@ -27,6 +27,9 @@ class PDBOutput(OutputFormatPlugin):
help=_('Specify the character encoding of the output document. ' \ help=_('Specify the character encoding of the output document. ' \
'The default is cp1252. Note: This option is not honored by all ' \ 'The default is cp1252. Note: This option is not honored by all ' \
'formats.')), 'formats.')),
OptionRecommendation(name='inline_toc',
recommended_value=False, level=OptionRecommendation.LOW,
help=_('Add Table of Contents to begenning of the book.')),
]) ])
def convert(self, oeb_book, output_path, input_plugin, opts, log): def convert(self, oeb_book, output_path, input_plugin, opts, log):

View File

@ -31,8 +31,10 @@ class PMLOutput(OutputFormatPlugin):
OptionRecommendation(name='output_encoding', recommended_value='cp1252', OptionRecommendation(name='output_encoding', recommended_value='cp1252',
level=OptionRecommendation.LOW, level=OptionRecommendation.LOW,
help=_('Specify the character encoding of the output document. ' \ help=_('Specify the character encoding of the output document. ' \
'The default is cp1252. Note: This option is not honored by all ' \ 'The default is cp1252.')),
'formats.')), OptionRecommendation(name='inline_toc',
recommended_value=False, level=OptionRecommendation.LOW,
help=_('Add Table of Contents to begenning of the book.')),
]) ])
def convert(self, oeb_book, output_path, input_plugin, opts, log): def convert(self, oeb_book, output_path, input_plugin, opts, log):

View File

@ -27,7 +27,7 @@ TAG_MAP = {
'del' : 'o', 'del' : 'o',
'h1' : 'x', 'h1' : 'x',
'h2' : 'X0', 'h2' : 'X0',
'h3' : 'x1', 'h3' : 'X1',
'h4' : 'X2', 'h4' : 'X2',
'h5' : 'X3', 'h5' : 'X3',
'h6' : 'X4', 'h6' : 'X4',
@ -85,6 +85,15 @@ class PMLMLizer(object):
def pmlmlize_spine(self): def pmlmlize_spine(self):
self.image_hrefs = {} self.image_hrefs = {}
self.link_hrefs = {} self.link_hrefs = {}
output = u''
output += self.get_cover_page()
output += u'ghji87yhjko0Caliblre-toc-placeholder-for-insertion-later8ujko0987yjk'
output += self.get_text()
output = output.replace(u'ghji87yhjko0Caliblre-toc-placeholder-for-insertion-later8ujko0987yjk', self.get_toc())
output = self.clean_text(output)
return output
def get_cover_page(self):
output = u'' output = u''
if 'titlepage' in self.oeb_book.guide: if 'titlepage' in self.oeb_book.guide:
self.log.debug('Generating title page...') self.log.debug('Generating title page...')
@ -93,14 +102,28 @@ class PMLMLizer(object):
if item.spine_position is None: if item.spine_position is None:
stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts.output_profile) stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts.output_profile)
output += self.dump_text(item.data.find(XHTML('body')), stylizer, item) output += self.dump_text(item.data.find(XHTML('body')), stylizer, item)
return output
def get_toc(self):
toc = u''
if self.opts.inline_toc:
self.log.debug('Generating table of contents...')
toc += u'\\X0%s\\X0\n\n' % _('Table of Contents:')
for item in self.oeb_book.toc:
if item.href in self.link_hrefs.keys():
toc += '* \\q="#%s"%s\\q\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: for item in self.oeb_book.spine:
self.log.debug('Converting %s to PML markup...' % item.href) self.log.debug('Converting %s to PML markup...' % item.href)
stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts.output_profile) stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts.output_profile)
output += self.add_page_anchor(item) text += self.add_page_anchor(item)
output += self.dump_text(item.data.find(XHTML('body')), stylizer, item) text += self.dump_text(item.data.find(XHTML('body')), stylizer, item)
output = self.clean_text(output) return text
return output
def add_page_anchor(self, page): def add_page_anchor(self, page):
return self.get_anchor(page, '') return self.get_anchor(page, '')

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.rb.writer import RBWriter from calibre.ebooks.rb.writer import RBWriter
class RBOutput(OutputFormatPlugin): class RBOutput(OutputFormatPlugin):
@ -15,6 +15,12 @@ class RBOutput(OutputFormatPlugin):
author = 'John Schember' author = 'John Schember'
file_type = 'rb' file_type = 'rb'
options = set([
OptionRecommendation(name='inline_toc',
recommended_value=False, level=OptionRecommendation.LOW,
help=_('Add Table of Contents to begenning of the book.')),
])
def convert(self, oeb_book, output_path, input_plugin, opts, log): def convert(self, oeb_book, output_path, input_plugin, opts, log):
close = False close = False
if not hasattr(output_path, 'write'): if not hasattr(output_path, 'write'):

View File

@ -72,6 +72,16 @@ class RBMLizer(object):
def mlize_spine(self): def mlize_spine(self):
self.link_hrefs = {} self.link_hrefs = {}
output = u'<HTML><HEAD><TITLE></TITLE></HEAD><BODY>' output = u'<HTML><HEAD><TITLE></TITLE></HEAD><BODY>'
output += self.get_cover_page()
output += u'ghji87yhjko0Caliblre-toc-placeholder-for-insertion-later8ujko0987yjk'
output += self.get_text()
output += u'</BODY></HTML>'
output = output.replace(u'ghji87yhjko0Caliblre-toc-placeholder-for-insertion-later8ujko0987yjk', self.get_toc())
output = self.clean_text(output)
return output
def get_cover_page(self):
output = u''
if 'titlepage' in self.oeb_book.guide: if 'titlepage' in self.oeb_book.guide:
self.log.debug('Generating cover page...') self.log.debug('Generating cover page...')
href = self.oeb_book.guide['titlepage'].href href = self.oeb_book.guide['titlepage'].href
@ -79,13 +89,28 @@ class RBMLizer(object):
if item.spine_position is None: if item.spine_position is None:
stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts.output_profile) stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts.output_profile)
output += self.dump_text(item.data.find(XHTML('body')), stylizer, item) output += self.dump_text(item.data.find(XHTML('body')), stylizer, item)
return output
def get_toc(self):
toc = u''
if self.opts.inline_toc:
self.log.debug('Generating table of contents...')
toc += u'<H1>%s</H1><UL>\n' % _('Table of Contents:')
for item in self.oeb_book.toc:
if item.href in self.link_hrefs.keys():
toc += '<LI><A HREF="#%s">%s</A></LI>\n' % (self.link_hrefs[item.href], item.title)
else:
self.oeb.warn('Ignoring toc item: %s not found in document.' % item)
toc += '</UL>'
return toc
def get_text(self):
output = u''
for item in self.oeb_book.spine: for item in self.oeb_book.spine:
self.log.debug('Converting %s to RocketBook HTML...' % item.href) self.log.debug('Converting %s to RocketBook HTML...' % item.href)
stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts.output_profile) stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts.output_profile)
output += self.add_page_anchor(item) output += self.add_page_anchor(item)
output += self.dump_text(item.data.find(XHTML('body')), stylizer, item) output += self.dump_text(item.data.find(XHTML('body')), stylizer, item)
output += u'</BODY></HTML>'
output = self.clean_text(output)
return output return output
def add_page_anchor(self, page): def add_page_anchor(self, page):

View File

@ -30,7 +30,10 @@ class TXTOutput(OutputFormatPlugin):
help=_('Specify the character encoding of the output document. ' \ help=_('Specify the character encoding of the output document. ' \
'The default is utf-8. Note: This option is not honored by all ' \ 'The default is utf-8. Note: This option is not honored by all ' \
'formats.')), 'formats.')),
]) OptionRecommendation(name='inline_toc',
recommended_value=False, level=OptionRecommendation.LOW,
help=_('Add Table of Contents to begenning of the book.')),
])
def convert(self, oeb_book, output_path, input_plugin, opts, log): def convert(self, oeb_book, output_path, input_plugin, opts, log):
writer = TXTMLizer(log) writer = TXTMLizer(log)

View File

@ -44,6 +44,7 @@ class TXTMLizer(object):
def mlize_spine(self): def mlize_spine(self):
output = u'' output = u''
output += self.get_toc()
for item in self.oeb_book.spine: for item in self.oeb_book.spine:
self.log.debug('Converting %s to TXT...' % item.href) self.log.debug('Converting %s to TXT...' % item.href)
stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts.output_profile) stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts.output_profile)
@ -62,6 +63,15 @@ class TXTMLizer(object):
return text return text
def get_toc(self):
toc = u''
if getattr(self.opts, 'inline_toc', None):
self.log.debug('Generating table of contents...')
toc += u'%s\n\n' % _(u'Table of Contents:')
for item in self.oeb_book.toc:
toc += u'* %s\n\n' % item.title
return toc
def cleanup_text(self, text): def cleanup_text(self, text):
self.log.debug('\tClean up text...') self.log.debug('\tClean up text...')
# Replace bad characters. # Replace bad characters.

View File

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
__license__ = 'GPL 3'
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
__docformat__ = 'restructuredtext en'
from calibre.gui2.convert.fb2_output_ui import Ui_Form
from calibre.gui2.convert import Widget
format_model = None
class PluginWidget(Widget, Ui_Form):
TITLE = _('FB2 Output')
HELP = _('Options specific to')+' FB2 '+_('output')
def __init__(self, parent, get_option, get_help, db=None, book_id=None):
Widget.__init__(self, parent, 'fb2_output', ['inline_toc'])
self.db, self.book_id = db, book_id
self.initialize_options(get_option, get_help, db, book_id)

View File

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>246</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="0">
<widget class="QCheckBox" name="opt_inline_toc">
<property name="text">
<string>&amp;Inline TOC</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

View File

@ -17,7 +17,7 @@ class PluginWidget(Widget, Ui_Form):
HELP = _('Options specific to')+' PDB '+_('output') HELP = _('Options specific to')+' PDB '+_('output')
def __init__(self, parent, get_option, get_help, db=None, book_id=None): def __init__(self, parent, get_option, get_help, db=None, book_id=None):
Widget.__init__(self, parent, 'pdb_output', ['format']) Widget.__init__(self, parent, 'pdb_output', ['format', 'inline_toc'])
self.db, self.book_id = db, book_id self.db, self.book_id = db, book_id
self.initialize_options(get_option, get_help, db, book_id) self.initialize_options(get_option, get_help, db, book_id)

View File

@ -27,7 +27,7 @@
<item row="0" column="1"> <item row="0" column="1">
<widget class="QComboBox" name="opt_format"/> <widget class="QComboBox" name="opt_format"/>
</item> </item>
<item row="1" column="0"> <item row="2" column="0">
<spacer name="verticalSpacer"> <spacer name="verticalSpacer">
<property name="orientation"> <property name="orientation">
<enum>Qt::Vertical</enum> <enum>Qt::Vertical</enum>
@ -40,6 +40,13 @@
</property> </property>
</spacer> </spacer>
</item> </item>
<item row="1" column="0">
<widget class="QCheckBox" name="opt_inline_toc">
<property name="text">
<string>&amp;Inline TOC</string>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
<resources/> <resources/>

View File

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
__license__ = 'GPL 3'
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
__docformat__ = 'restructuredtext en'
from calibre.gui2.convert.rb_output_ui import Ui_Form
from calibre.gui2.convert import Widget
format_model = None
class PluginWidget(Widget, Ui_Form):
TITLE = _('RB Output')
HELP = _('Options specific to')+' RB '+_('output')
def __init__(self, parent, get_option, get_help, db=None, book_id=None):
Widget.__init__(self, parent, 'rb_output', ['inline_toc'])
self.db, self.book_id = db, book_id
self.initialize_options(get_option, get_help, db, book_id)

View File

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>246</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="0">
<widget class="QCheckBox" name="opt_inline_toc">
<property name="text">
<string>&amp;Inline TOC</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

View File

@ -17,7 +17,7 @@ class PluginWidget(Widget, Ui_Form):
HELP = _('Options specific to')+' TXT '+_('output') HELP = _('Options specific to')+' TXT '+_('output')
def __init__(self, parent, get_option, get_help, db=None, book_id=None): def __init__(self, parent, get_option, get_help, db=None, book_id=None):
Widget.__init__(self, parent, 'txt_output', ['newline']) Widget.__init__(self, parent, 'txt_output', ['newline', 'inline_toc'])
self.db, self.book_id = db, book_id self.db, self.book_id = db, book_id
self.initialize_options(get_option, get_help, db, book_id) self.initialize_options(get_option, get_help, db, book_id)

View File

@ -27,7 +27,7 @@
<item row="0" column="1"> <item row="0" column="1">
<widget class="QComboBox" name="opt_newline"/> <widget class="QComboBox" name="opt_newline"/>
</item> </item>
<item row="1" column="0"> <item row="2" column="0">
<spacer name="verticalSpacer"> <spacer name="verticalSpacer">
<property name="orientation"> <property name="orientation">
<enum>Qt::Vertical</enum> <enum>Qt::Vertical</enum>
@ -40,6 +40,13 @@
</property> </property>
</spacer> </spacer>
</item> </item>
<item row="1" column="0">
<widget class="QCheckBox" name="opt_inline_toc">
<property name="text">
<string>&amp;Inline TOC</string>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
<resources/> <resources/>