Add missing import. Allow TXT output to split lines based on a maximum line length value.

This commit is contained in:
John Schember 2009-09-06 14:09:43 -04:00
parent 2de625b3e1
commit bd1b37f3a9
6 changed files with 74 additions and 8 deletions

View File

@ -14,7 +14,7 @@ import struct
from calibre.ebooks.compression.palmdoc import decompress_doc from calibre.ebooks.compression.palmdoc import decompress_doc
from calibre.ebooks.pdb.formatreader import FormatReader from calibre.ebooks.pdb.formatreader import FormatReader
from calibre.ebooks.txt.processor import convert_basic, opf_writer, \ 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): class HeaderRecord(object):
''' '''

View File

@ -13,7 +13,7 @@ import os, struct, zlib
from calibre.ebooks.pdb.formatreader import FormatReader from calibre.ebooks.pdb.formatreader import FormatReader
from calibre.ebooks.pdb.ztxt import zTXTError from calibre.ebooks.pdb.ztxt import zTXTError
from calibre.ebooks.txt.processor import convert_basic, opf_writer, \ 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) SUPPORTED_VERSION = (1, 40)

View File

@ -36,6 +36,17 @@ class TXTOutput(OutputFormatPlugin):
OptionRecommendation(name='flush_paras', OptionRecommendation(name='flush_paras',
recommended_value=False, level=OptionRecommendation.LOW, recommended_value=False, level=OptionRecommendation.LOW,
help=_('Do not add a blank line between paragraphs.')), 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', OptionRecommendation(name='indent_paras',
recommended_value=False, level=OptionRecommendation.LOW, recommended_value=False, level=OptionRecommendation.LOW,
help=_('Add a tab at the beginning of each paragraph.')), help=_('Add a tab at the beginning of each paragraph.')),

View File

@ -105,6 +105,40 @@ class TXTMLizer(object):
if self.opts.indent_paras: if self.opts.indent_paras:
text = re.sub('(?imu)^(?=.)', '\t', text) 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 return text
def dump_text(self, elem, stylizer, end=''): def dump_text(self, elem, stylizer, end=''):

View File

@ -17,8 +17,9 @@ 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',
'flush_paras', 'indent_paras']) ['newline', 'max_line_length', 'force_max_line_length',
'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="4" column="0"> <item row="6" column="0">
<spacer name="verticalSpacer"> <spacer name="verticalSpacer">
<property name="orientation"> <property name="orientation">
<enum>Qt::Vertical</enum> <enum>Qt::Vertical</enum>
@ -40,27 +40,47 @@
</property> </property>
</spacer> </spacer>
</item> </item>
<item row="1" column="0" colspan="2"> <item row="3" 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"> <item row="4" column="0" colspan="2">
<widget class="QCheckBox" name="opt_flush_paras"> <widget class="QCheckBox" name="opt_flush_paras">
<property name="text"> <property name="text">
<string>Do not add a blank line between paragraphs.</string> <string>Do not add a blank line between paragraphs.</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="3" column="0" colspan="2"> <item row="5" column="0" colspan="2">
<widget class="QCheckBox" name="opt_indent_paras"> <widget class="QCheckBox" name="opt_indent_paras">
<property name="text"> <property name="text">
<string>Add a tab at the beginning of each paragraph</string> <string>Add a tab at the beginning of each paragraph</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="1">
<widget class="QSpinBox" name="opt_max_line_length"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>&amp;Maximum line length:</string>
</property>
<property name="buddy">
<cstring>opt_max_line_length</cstring>
</property>
</widget>
</item>
<item row="2" column="0" colspan="2">
<widget class="QCheckBox" name="opt_force_max_line_length">
<property name="text">
<string>Force maximum line lenght</string>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
<resources/> <resources/>