TXT Output: Options to not add a blank line between paragraphs and to add a TAB at the start of each paragraph

This commit is contained in:
Kovid Goyal 2009-09-02 16:26:16 -06:00
parent acf94d4786
commit f6acabca50
4 changed files with 35 additions and 7 deletions

View File

@ -33,6 +33,12 @@ class TXTOutput(OutputFormatPlugin):
OptionRecommendation(name='inline_toc', OptionRecommendation(name='inline_toc',
recommended_value=False, level=OptionRecommendation.LOW, recommended_value=False, level=OptionRecommendation.LOW,
help=_('Add Table of Contents to beginning of the book.')), help=_('Add Table of Contents to beginning of the book.')),
OptionRecommendation(name='flush_paras',
recommended_value=False, level=OptionRecommendation.LOW,
help=_('Do not add a blank line between paragraphs.')),
OptionRecommendation(name='indent_paras',
recommended_value=False, level=OptionRecommendation.LOW,
help=_('Add a tab at the beginning of each paragraph.')),
]) ])
def convert(self, oeb_book, output_path, input_plugin, opts, log): def convert(self, oeb_book, output_path, input_plugin, opts, log):

View File

@ -41,6 +41,7 @@ class TXTMLizer(object):
self.log.info('Converting XHTML to TXT...') self.log.info('Converting XHTML to TXT...')
self.oeb_book = oeb_book self.oeb_book = oeb_book
self.opts = opts self.opts = opts
return self.mlize_spine() return self.mlize_spine()
def mlize_spine(self): def mlize_spine(self):
@ -92,12 +93,18 @@ class TXTMLizer(object):
# Remove excessive newlines. # Remove excessive newlines.
text = re.sub('\n[ ]+\n', '\n\n', text) text = re.sub('\n[ ]+\n', '\n\n', text)
text = re.sub('\n{3,}', '\n\n', text) if self.opts.flush_paras:
text = re.sub('\n{2,}', '\n', text)
else:
text = re.sub('\n{3,}', '\n\n', text)
# Replace spaces at the beginning and end of lines # Replace spaces at the beginning and end of lines
text = re.sub('(?imu)^[ ]+', '', text) text = re.sub('(?imu)^[ ]+', '', text)
text = re.sub('(?imu)[ ]+$', '', text) text = re.sub('(?imu)[ ]+$', '', text)
if self.opts.indent_paras:
text = re.sub('(?imu)^(?=.)', '\t', text)
return text return text
def get_text(self, elem, stylizer): def get_text(self, elem, stylizer):
@ -137,8 +144,8 @@ class TXTMLizer(object):
# Are we in a paragraph block? # Are we in a paragraph block?
if tag in BLOCK_TAGS or style['display'] in BLOCK_STYLES: if tag in BLOCK_TAGS or style['display'] in BLOCK_STYLES:
in_block = True in_block = True
if not end.endswith('\n\n') and hasattr(elem, 'text') and elem.text != None and elem.text.strip() != '': if not end.endswith(u'\n\n') and hasattr(elem, 'text') and elem.text != None and elem.text.strip() != '':
text.append('\n\n') text.append(u'\n\n')
# Process tags that contain text. # Process tags that contain text.
if hasattr(elem, 'text') and elem.text != None and elem.text.strip() != '': if hasattr(elem, 'text') and elem.text != None and elem.text.strip() != '':
@ -151,7 +158,7 @@ class TXTMLizer(object):
text += self.dump_text(item, stylizer, en) text += self.dump_text(item, stylizer, en)
if in_block: if in_block:
text.append('\n\n') text.append(u'\n\n')
if hasattr(elem, 'tail') and elem.tail != None and elem.tail.strip() != '': if hasattr(elem, 'tail') and elem.tail != None and elem.tail.strip() != '':
text.append(elem.tail) text.append(elem.tail)

View File

@ -17,7 +17,8 @@ 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', 'inline_toc']) Widget.__init__(self, parent, 'txt_output', ['newline', 'inline_toc',
'flush_paras', 'indent_paras'])
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="2" column="0"> <item row="4" column="0">
<spacer name="verticalSpacer"> <spacer name="verticalSpacer">
<property name="orientation"> <property name="orientation">
<enum>Qt::Vertical</enum> <enum>Qt::Vertical</enum>
@ -40,13 +40,27 @@
</property> </property>
</spacer> </spacer>
</item> </item>
<item row="1" column="0"> <item row="1" column="0" colspan="2">
<widget class="QCheckBox" name="opt_inline_toc"> <widget class="QCheckBox" name="opt_inline_toc">
<property name="text"> <property name="text">
<string>&amp;Inline TOC</string> <string>&amp;Inline TOC</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="0" colspan="2">
<widget class="QCheckBox" name="opt_flush_paras">
<property name="text">
<string>Do not add a blank line between paragraphs.</string>
</property>
</widget>
</item>
<item row="3" column="0" colspan="2">
<widget class="QCheckBox" name="opt_indent_paras">
<property name="text">
<string>Add a tab at the beginning of each paragraph</string>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
<resources/> <resources/>