mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-06-23 15:30:45 -04:00
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:
parent
acf94d4786
commit
f6acabca50
@ -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):
|
||||
|
@ -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)
|
||||
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')
|
||||
|
||||
# Process 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)
|
||||
|
@ -17,7 +17,8 @@ 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', 'inline_toc'])
|
||||
Widget.__init__(self, parent, 'txt_output', ['newline', 'inline_toc',
|
||||
'flush_paras', 'indent_paras'])
|
||||
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="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>&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/>
|
||||
|
Loading…
x
Reference in New Issue
Block a user