mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Optional inline TOC for RB, PDB, TXT, FB2 output.
This commit is contained in:
parent
12c99d6a9d
commit
a0821a6882
@ -104,7 +104,7 @@ class FB2MLizer(object):
|
||||
'</description>\n<body>\n<section>' % (author_first, author_middle,
|
||||
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:
|
||||
|
@ -18,8 +18,7 @@ class FB2Output(OutputFormatPlugin):
|
||||
options = set([
|
||||
OptionRecommendation(name='inline_toc',
|
||||
recommended_value=False, level=OptionRecommendation.LOW,
|
||||
help=_('Add Table of Contents to begenning of the book. Useful if '
|
||||
'the book has its own table of contents.')),
|
||||
help=_('Add Table of Contents to begenning of the book.')),
|
||||
])
|
||||
|
||||
def convert(self, oeb_book, output_path, input_plugin, opts, log):
|
||||
|
@ -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()
|
||||
|
@ -27,6 +27,9 @@ class PDBOutput(OutputFormatPlugin):
|
||||
help=_('Specify the character encoding of the output document. ' \
|
||||
'The default is cp1252. Note: This option is not honored by all ' \
|
||||
'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):
|
||||
|
@ -31,8 +31,10 @@ class PMLOutput(OutputFormatPlugin):
|
||||
OptionRecommendation(name='output_encoding', recommended_value='cp1252',
|
||||
level=OptionRecommendation.LOW,
|
||||
help=_('Specify the character encoding of the output document. ' \
|
||||
'The default is cp1252. Note: This option is not honored by all ' \
|
||||
'formats.')),
|
||||
'The default is cp1252.')),
|
||||
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):
|
||||
|
@ -85,6 +85,15 @@ class PMLMLizer(object):
|
||||
def pmlmlize_spine(self):
|
||||
self.image_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''
|
||||
if 'titlepage' in self.oeb_book.guide:
|
||||
self.log.debug('Generating title page...')
|
||||
@ -93,14 +102,28 @@ class PMLMLizer(object):
|
||||
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'\\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:
|
||||
self.log.debug('Converting %s to PML markup...' % 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.clean_text(output)
|
||||
|
||||
return output
|
||||
text += self.add_page_anchor(item)
|
||||
text += self.dump_text(item.data.find(XHTML('body')), stylizer, item)
|
||||
return text
|
||||
|
||||
def add_page_anchor(self, page):
|
||||
return self.get_anchor(page, '')
|
||||
|
@ -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.rb.writer import RBWriter
|
||||
|
||||
class RBOutput(OutputFormatPlugin):
|
||||
@ -15,6 +15,12 @@ class RBOutput(OutputFormatPlugin):
|
||||
author = 'John Schember'
|
||||
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):
|
||||
close = False
|
||||
if not hasattr(output_path, 'write'):
|
||||
|
@ -72,6 +72,16 @@ class RBMLizer(object):
|
||||
def mlize_spine(self):
|
||||
self.link_hrefs = {}
|
||||
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:
|
||||
self.log.debug('Generating cover page...')
|
||||
href = self.oeb_book.guide['titlepage'].href
|
||||
@ -79,13 +89,28 @@ class RBMLizer(object):
|
||||
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'<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:
|
||||
self.log.debug('Converting %s to RocketBook HTML...' % 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 += u'</BODY></HTML>'
|
||||
output = self.clean_text(output)
|
||||
return output
|
||||
|
||||
def add_page_anchor(self, page):
|
||||
|
@ -30,7 +30,10 @@ class TXTOutput(OutputFormatPlugin):
|
||||
help=_('Specify the character encoding of the output document. ' \
|
||||
'The default is utf-8. Note: This option is not honored by all ' \
|
||||
'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):
|
||||
writer = TXTMLizer(log)
|
||||
|
@ -44,6 +44,7 @@ class TXTMLizer(object):
|
||||
|
||||
def mlize_spine(self):
|
||||
output = u''
|
||||
output += self.get_toc()
|
||||
for item in self.oeb_book.spine:
|
||||
self.log.debug('Converting %s to TXT...' % item.href)
|
||||
stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts.output_profile)
|
||||
@ -62,6 +63,15 @@ class TXTMLizer(object):
|
||||
|
||||
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):
|
||||
self.log.debug('\tClean up text...')
|
||||
# Replace bad characters.
|
||||
|
20
src/calibre/gui2/convert/fb2_output.py
Normal file
20
src/calibre/gui2/convert/fb2_output.py
Normal 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)
|
41
src/calibre/gui2/convert/fb2_output.ui
Normal file
41
src/calibre/gui2/convert/fb2_output.ui
Normal 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>&Inline TOC</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
@ -17,7 +17,7 @@ class PluginWidget(Widget, Ui_Form):
|
||||
HELP = _('Options specific to')+' PDB '+_('output')
|
||||
|
||||
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.initialize_options(get_option, get_help, db, book_id)
|
||||
|
||||
|
@ -27,7 +27,7 @@
|
||||
<item row="0" column="1">
|
||||
<widget class="QComboBox" name="opt_format"/>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<item row="2" column="0">
|
||||
<spacer name="verticalSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Vertical</enum>
|
||||
@ -40,6 +40,13 @@
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QCheckBox" name="opt_inline_toc">
|
||||
<property name="text">
|
||||
<string>&Inline TOC</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
|
20
src/calibre/gui2/convert/rb_output.py
Normal file
20
src/calibre/gui2/convert/rb_output.py
Normal 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)
|
41
src/calibre/gui2/convert/rb_output.ui
Normal file
41
src/calibre/gui2/convert/rb_output.ui
Normal 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>&Inline TOC</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
@ -17,7 +17,7 @@ class PluginWidget(Widget, Ui_Form):
|
||||
HELP = _('Options specific to')+' TXT '+_('output')
|
||||
|
||||
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.initialize_options(get_option, get_help, db, book_id)
|
||||
|
||||
|
@ -27,7 +27,7 @@
|
||||
<item row="0" column="1">
|
||||
<widget class="QComboBox" name="opt_newline"/>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<item row="2" column="0">
|
||||
<spacer name="verticalSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Vertical</enum>
|
||||
@ -40,6 +40,13 @@
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QCheckBox" name="opt_inline_toc">
|
||||
<property name="text">
|
||||
<string>&Inline TOC</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
|
Loading…
x
Reference in New Issue
Block a user