[Options] Add three basic options to SNBOutput plugin.

This commit is contained in:
Li Fanxi 2010-10-18 16:12:19 +08:00
parent 8b3ae522bc
commit 9f453fa65e
4 changed files with 68 additions and 86 deletions

View File

@ -20,20 +20,10 @@ class SNBOutput(OutputFormatPlugin):
file_type = 'snb' file_type = 'snb'
options = set([ options = set([
# OptionRecommendation(name='newline', recommended_value='system',
# level=OptionRecommendation.LOW,
# short_switch='n', choices=TxtNewlines.NEWLINE_TYPES.keys(),
# help=_('Type of newline to use. Options are %s. Default is \'system\'. '
# 'Use \'old_mac\' for compatibility with Mac OS 9 and earlier. '
# 'For Mac OS X use \'unix\'. \'system\' will default to the newline '
# 'type used by this OS.') % sorted(TxtNewlines.NEWLINE_TYPES.keys())),
OptionRecommendation(name='snb_output_encoding', recommended_value='utf-8', OptionRecommendation(name='snb_output_encoding', recommended_value='utf-8',
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 utf-8.')), 'The default is utf-8.')),
# OptionRecommendation(name='inline_toc',
# recommended_value=False, level=OptionRecommendation.LOW,
# help=_('Add Table of Contents to beginning of the book.')),
OptionRecommendation(name='snb_max_line_length', OptionRecommendation(name='snb_max_line_length',
recommended_value=0, level=OptionRecommendation.LOW, recommended_value=0, level=OptionRecommendation.LOW,
help=_('The maximum number of characters per line. This splits on ' help=_('The maximum number of characters per line. This splits on '
@ -41,10 +31,18 @@ class SNBOutput(OutputFormatPlugin):
'the line will be broken at the space after and will exceed the ' 'the line will be broken at the space after and will exceed the '
'specified value. Also, there is a minimum of 25 characters. ' 'specified value. Also, there is a minimum of 25 characters. '
'Use 0 to disable line splitting.')), 'Use 0 to disable line splitting.')),
# OptionRecommendation(name='force_max_line_length', OptionRecommendation(name='insert_empty_line',
# recommended_value=False, level=OptionRecommendation.LOW, recommended_value=False, level=OptionRecommendation.LOW,
# help=_('Force splitting on the max-line-length value when no space ' help=_('Speicfy whether or not to insert an empty line between '
# 'is present. Also allows max-line-length to be below the minimum')), 'two paragraphs.')),
OptionRecommendation(name='indent_first_line',
recommended_value=True, level=OptionRecommendation.LOW,
help=_('Speicfy whether or not to insert two space characters '
'to indent the first line of each paragraph.')),
OptionRecommendation(name='hide_chapter_name',
recommended_value=False, level=OptionRecommendation.LOW,
help=_('Speicfy whether or not to hide the chapter title for each '
'chapter. Useful for image-only output (eg. comics).')),
]) ])
def convert(self, oeb_book, output_path, input_plugin, opts, log): def convert(self, oeb_book, output_path, input_plugin, opts, log):

View File

@ -88,7 +88,10 @@ class SNBMLizer(object):
trees = { } trees = { }
for subitem, subtitle in self.subitems: for subitem, subtitle in self.subitems:
snbcTree = etree.Element("snbc") snbcTree = etree.Element("snbc")
etree.SubElement(etree.SubElement(snbcTree, "head"), "title").text = subtitle snbcHead = etree.SubElement(snbcTree, "head")
etree.SubElement(snbcHead, "title").text = subtitle
if self.opts and self.opts.hide_chapter_name:
etree.SubElement(snbcHead, "hidetitle").text = u"true"
etree.SubElement(snbcTree, "body") etree.SubElement(snbcTree, "body")
trees[subitem] = snbcTree trees[subitem] = snbcTree
output.append(u'%s%s\n\n' % (CALIBRE_SNB_BM_TAG, "")) output.append(u'%s%s\n\n' % (CALIBRE_SNB_BM_TAG, ""))
@ -96,27 +99,37 @@ class SNBMLizer(object):
output = self.cleanup_text(u''.join(output)) output = self.cleanup_text(u''.join(output))
subitem = '' subitem = ''
bodyTree = trees[subitem].find(".//body")
for line in output.splitlines(): for line in output.splitlines():
if not line.find(CALIBRE_SNB_PRE_TAG) == 0: if not line.find(CALIBRE_SNB_PRE_TAG) == 0:
line = line.strip(u' \t\n\r\u3000') line = line.strip(u' \t\n\r\u3000')
else: else:
etree.SubElement(trees[subitem].find(".//body"), "text").text = \ etree.SubElement(bodyTree, "text").text = \
etree.CDATA(line[len(CALIBRE_SNB_PRE_TAG):]) etree.CDATA(line[len(CALIBRE_SNB_PRE_TAG):])
continue continue
if len(line) != 0: if len(line) != 0:
if line.find(CALIBRE_SNB_IMG_TAG) == 0: if line.find(CALIBRE_SNB_IMG_TAG) == 0:
prefix = ProcessFileName(os.path.dirname(self.item.href)) prefix = ProcessFileName(os.path.dirname(self.item.href))
if prefix != '': if prefix != '':
etree.SubElement(trees[subitem].find(".//body"), "img").text = \ etree.SubElement(bodyTree, "img").text = \
prefix + '_' + line[len(CALIBRE_SNB_IMG_TAG):] prefix + '_' + line[len(CALIBRE_SNB_IMG_TAG):]
else: else:
etree.SubElement(trees[subitem].find(".//body"), "img").text = \ etree.SubElement(bodyTree, "img").text = \
line[len(CALIBRE_SNB_IMG_TAG):] line[len(CALIBRE_SNB_IMG_TAG):]
elif line.find(CALIBRE_SNB_BM_TAG) == 0: elif line.find(CALIBRE_SNB_BM_TAG) == 0:
subitem = line[len(CALIBRE_SNB_BM_TAG):] subitem = line[len(CALIBRE_SNB_BM_TAG):]
bodyTree = trees[subitem].find(".//body")
else: else:
etree.SubElement(trees[subitem].find(".//body"), "text").text = \ if self.opts and self.opts.indent_first_line:
etree.CDATA(unicode(u'\u3000\u3000' + line)) prefix = u'\u3000\u3000'
else:
prefix = u''
etree.SubElement(bodyTree, "text").text = \
etree.CDATA(unicode(prefix + line))
if self.opts and self.opts.insert_empty_line:
etree.SubElement(bodyTree, "text").text = \
etree.CDATA(u'')
return trees return trees
def remove_newlines(self, text): def remove_newlines(self, text):

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
__license__ = 'GPL 3' __license__ = 'GPL 3'
__copyright__ = '2009, John Schember <john@nachtimwald.com>' __copyright__ = '2010, Li Fanxi <lifanxi@freemindworld.com>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
from calibre.gui2.convert.snb_output_ui import Ui_Form from calibre.gui2.convert.snb_output_ui import Ui_Form
@ -18,18 +18,8 @@ class PluginWidget(Widget, Ui_Form):
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, Widget.__init__(self, parent,
[]) ['insert_empty_line', 'indent_first_line', 'hide_chapter_name',])
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)
# default = self.opt_newline.currentText()
# global newline_model
# if newline_model is None:
# newline_model = BasicComboModel(TxtNewlines.NEWLINE_TYPES.keys())
# self.newline_model = newline_model
# self.opt_newline.setModel(self.newline_model)
# default_index = self.opt_newline.findText(default)
# system_index = self.opt_newline.findText('system')
# self.opt_newline.setCurrentIndex(default_index if default_index != -1 else system_index if system_index != -1 else 0)

View File

@ -13,60 +13,41 @@
<property name="windowTitle"> <property name="windowTitle">
<string>Form</string> <string>Form</string>
</property> </property>
<layout class="QGridLayout" name="gridLayout"> <layout class="QGridLayout" name="gridLayout" rowstretch="0,0,0,0,0">
<!-- <item row="0" column="0"> --> <item row="4" column="0">
<!-- <widget class="QLabel" name="label"> --> <spacer name="verticalSpacer">
<!-- <property name="text"> --> <property name="orientation">
<!-- <string>&amp;Line ending style:</string> --> <enum>Qt::Vertical</enum>
<!-- </property> --> </property>
<!-- <property name="buddy"> --> <property name="sizeHint" stdset="0">
<!-- <cstring>opt_newline</cstring> --> <size>
<!-- </property> --> <width>20</width>
<!-- </widget> --> <height>40</height>
<!-- </item> --> </size>
<!-- <item row="0" column="1"> --> </property>
<!-- <widget class="QComboBox" name="opt_newline"/> --> </spacer>
<!-- </item> --> </item>
<!-- <item row="4" column="0"> --> <item row="3" column="0">
<!-- <spacer name="verticalSpacer"> --> <widget class="QCheckBox" name="opt_hide_chapter_name">
<!-- <property name="orientation"> --> <property name="text">
<!-- <enum>Qt::Vertical</enum> --> <string>Hide chapter name</string>
<!-- </property> --> </property>
<!-- <property name="sizeHint" stdset="0"> --> </widget>
<!-- <size> --> </item>
<!-- <width>20</width> --> <item row="2" column="0">
<!-- <height>246</height> --> <widget class="QCheckBox" name="opt_indent_first_line">
<!-- </size> --> <property name="text">
<!-- </property> --> <string>Insert space before the first line for each paragraph</string>
<!-- </spacer> --> </property>
<!-- </item> --> </widget>
<!-- <item row="3" column="0" colspan="2"> --> </item>
<!-- <widget class="QCheckBox" name="opt_inline_toc"> --> <item row="1" column="0">
<!-- <property name="text"> --> <widget class="QCheckBox" name="opt_insert_empty_line">
<!-- <string>&amp;Inline TOC</string> --> <property name="text">
<!-- </property> --> <string>Insert empty line between paragraphs</string>
<!-- </widget> --> </property>
<!-- </item> --> </widget>
<!-- <item row="1" column="1"> --> </item>
<!-- <widget class="QSpinBox" name="opt_max_line_length"/> -->
<!-- </item> -->
<!-- <item row="1" column="0"> -->
<!-- <widget class="QLabel" name="label_2"> -->
<!-- <property name="text"> -->
<!-- <string>&amp;Maximum line length:</string> -->
<!-- </property> -->
<!-- <property name="buddy"> -->
<!-- <cstring>opt_max_line_length</cstring> -->
<!-- </property> -->
<!-- </widget> -->
<!-- </item> -->
<!-- <item row="2" column="0" colspan="2"> -->
<!-- <widget class="QCheckBox" name="opt_force_max_line_length"> -->
<!-- <property name="text"> -->
<!-- <string>Force maximum line length</string> -->
<!-- </property> -->
<!-- </widget> -->
<!-- </item> -->
</layout> </layout>
</widget> </widget>
<resources/> <resources/>