From f6acabca5035d60238d8ed854e206b621f86ca0e Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 2 Sep 2009 16:26:16 -0600 Subject: [PATCH] TXT Output: Options to not add a blank line between paragraphs and to add a TAB at the start of each paragraph --- src/calibre/ebooks/txt/output.py | 6 ++++++ src/calibre/ebooks/txt/txtml.py | 15 +++++++++++---- src/calibre/gui2/convert/txt_output.py | 3 ++- src/calibre/gui2/convert/txt_output.ui | 18 ++++++++++++++++-- 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/calibre/ebooks/txt/output.py b/src/calibre/ebooks/txt/output.py index 6f0a768b8f..b3bda7fa9d 100644 --- a/src/calibre/ebooks/txt/output.py +++ b/src/calibre/ebooks/txt/output.py @@ -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): diff --git a/src/calibre/ebooks/txt/txtml.py b/src/calibre/ebooks/txt/txtml.py index 28ba9eea4a..cba383ef9a 100644 --- a/src/calibre/ebooks/txt/txtml.py +++ b/src/calibre/ebooks/txt/txtml.py @@ -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') # 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) diff --git a/src/calibre/gui2/convert/txt_output.py b/src/calibre/gui2/convert/txt_output.py index 407c308089..c2474ac4b8 100644 --- a/src/calibre/gui2/convert/txt_output.py +++ b/src/calibre/gui2/convert/txt_output.py @@ -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) diff --git a/src/calibre/gui2/convert/txt_output.ui b/src/calibre/gui2/convert/txt_output.ui index 6e62040533..900198aca9 100644 --- a/src/calibre/gui2/convert/txt_output.ui +++ b/src/calibre/gui2/convert/txt_output.ui @@ -27,7 +27,7 @@ - + Qt::Vertical @@ -40,13 +40,27 @@ - + &Inline TOC + + + + Do not add a blank line between paragraphs. + + + + + + + Add a tab at the beginning of each paragraph + + +