TXT Output: Option to produce not add a blank line between paragraphs. Option to indent the beginning of each paragraph by a tab.

This commit is contained in:
John Schember 2009-09-02 17:56:54 -04:00
parent 34e9857ab0
commit 8245d7d7e8
3 changed files with 33 additions and 6 deletions

View File

@ -33,6 +33,12 @@ class TXTOutput(OutputFormatPlugin):
OptionRecommendation(name='inline_toc',
recommended_value=False, level=OptionRecommendation.LOW,
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):

View File

@ -41,6 +41,7 @@ class TXTMLizer(object):
self.log.info('Converting XHTML to TXT...')
self.oeb_book = oeb_book
self.opts = opts
return self.mlize_spine()
def mlize_spine(self):
@ -92,12 +93,18 @@ class TXTMLizer(object):
# Remove excessive newlines.
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
text = re.sub('(?imu)^[ ]+', '', text)
text = re.sub('(?imu)[ ]+$', '', text)
if self.opts.indent_paras:
text = re.sub('(?imu)^(?=.)', '\t', text)
return text
def get_text(self, elem, stylizer):
@ -137,8 +144,8 @@ class TXTMLizer(object):
# Are we in a paragraph block?
if tag in BLOCK_TAGS or style['display'] in BLOCK_STYLES:
in_block = True
if not end.endswith('\n\n') and hasattr(elem, 'text') and elem.text != None and elem.text.strip() != '':
text.append('\n\n')
if not end.endswith(u'\n\n') and hasattr(elem, 'text') and elem.text != None and elem.text.strip() != '':
text.append(u'\n\n')
# Proccess tags that contain text.
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)
if in_block:
text.append('\n\n')
text.append(u'\n\n')
if hasattr(elem, 'tail') and elem.tail != None and elem.tail.strip() != '':
text.append(elem.tail)

View File

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