diff --git a/src/calibre/ebooks/pdb/palmdoc/reader.py b/src/calibre/ebooks/pdb/palmdoc/reader.py index aaa121f1ed..0a57e3f51a 100644 --- a/src/calibre/ebooks/pdb/palmdoc/reader.py +++ b/src/calibre/ebooks/pdb/palmdoc/reader.py @@ -14,7 +14,7 @@ import struct from calibre.ebooks.compression.palmdoc import decompress_doc from calibre.ebooks.pdb.formatreader import FormatReader from calibre.ebooks.txt.processor import convert_basic, opf_writer, \ - separate_paragraphs_single_line + separate_paragraphs_single_line, separate_paragraphs_print_formatted class HeaderRecord(object): ''' diff --git a/src/calibre/ebooks/pdb/ztxt/reader.py b/src/calibre/ebooks/pdb/ztxt/reader.py index 4379159d81..86fb9d868c 100644 --- a/src/calibre/ebooks/pdb/ztxt/reader.py +++ b/src/calibre/ebooks/pdb/ztxt/reader.py @@ -13,7 +13,7 @@ import os, struct, zlib from calibre.ebooks.pdb.formatreader import FormatReader from calibre.ebooks.pdb.ztxt import zTXTError from calibre.ebooks.txt.processor import convert_basic, opf_writer, \ - separate_paragraphs_single_line + separate_paragraphs_single_line, separate_paragraphs_print_formatted SUPPORTED_VERSION = (1, 40) diff --git a/src/calibre/ebooks/txt/output.py b/src/calibre/ebooks/txt/output.py index b3bda7fa9d..32bde90fe8 100644 --- a/src/calibre/ebooks/txt/output.py +++ b/src/calibre/ebooks/txt/output.py @@ -36,6 +36,17 @@ class TXTOutput(OutputFormatPlugin): OptionRecommendation(name='flush_paras', recommended_value=False, level=OptionRecommendation.LOW, help=_('Do not add a blank line between paragraphs.')), + OptionRecommendation(name='max_line_length', + recommended_value=0, level=OptionRecommendation.LOW, + help=_('The maximum number of characters per line. This splits on ' + 'the first space before the specified value. If no space is found ' + 'the line will be broken at the space after and will exceed the ' + 'specified value. Also, there is a minimum of 25 characters. ' + 'Use 0 to disable line splitting.')), + OptionRecommendation(name='force_max_line_length', + recommended_value=False, level=OptionRecommendation.LOW, + help=_('Force splitting on the max-line-length value when no space ' + 'is present. Also allows max-line-length to be below the minimum')), OptionRecommendation(name='indent_paras', recommended_value=False, level=OptionRecommendation.LOW, help=_('Add a tab at the beginning of each paragraph.')), diff --git a/src/calibre/ebooks/txt/txtml.py b/src/calibre/ebooks/txt/txtml.py index c705bcf221..63a5cdc8af 100644 --- a/src/calibre/ebooks/txt/txtml.py +++ b/src/calibre/ebooks/txt/txtml.py @@ -105,6 +105,40 @@ class TXTMLizer(object): if self.opts.indent_paras: text = re.sub('(?imu)^(?=.)', '\t', text) + if self.opts.max_line_length: + max_length = self.opts.max_line_length + if self.opts.max_line_length < 25 and not self.opts.force_max_line_length: + max_length = 25 + short_lines = [] + lines = text.splitlines() + for line in lines: + while len(line) > max_length: + space = line.rfind(' ', 0, max_length) + if space != -1: + # Space was found. + short_lines.append(line[:space]) + line = line[space + 1:] + else: + # Space was not found. + if self.opts.force_max_line_length: + # Force breaking at max_lenght. + short_lines.append(line[:max_length]) + line = line[max_length:] + else: + # Look for the first space after max_length. + space = line.find(' ', max_length, len(line)) + if space != -1: + # Space was found. + short_lines.append(line[:space]) + line = line[space + 1:] + else: + # No space was found cannot break line. + short_lines.append(line) + line = '' + # Add the text that was less than max_lengh to the list + short_lines.append(line) + text = '\n'.join(short_lines) + return text def dump_text(self, elem, stylizer, end=''): diff --git a/src/calibre/gui2/convert/txt_output.py b/src/calibre/gui2/convert/txt_output.py index eca85f1292..2fc7f19908 100644 --- a/src/calibre/gui2/convert/txt_output.py +++ b/src/calibre/gui2/convert/txt_output.py @@ -17,8 +17,9 @@ 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', - 'flush_paras', 'indent_paras']) + Widget.__init__(self, parent, 'txt_output', + ['newline', 'max_line_length', 'force_max_line_length', + '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 900198aca9..8e5429b0ce 100644 --- a/src/calibre/gui2/convert/txt_output.ui +++ b/src/calibre/gui2/convert/txt_output.ui @@ -27,7 +27,7 @@ - + Qt::Vertical @@ -40,27 +40,47 @@ - + &Inline TOC - + Do not add a blank line between paragraphs. - + Add a tab at the beginning of each paragraph + + + + + + + &Maximum line length: + + + opt_max_line_length + + + + + + + Force maximum line lenght + + +