TXT Input: preserve spaces option.

This commit is contained in:
John Schember 2010-05-30 13:53:18 -04:00
parent 0ff10fe05f
commit 36bb8ba76e
2 changed files with 15 additions and 1 deletions

View File

@ -8,7 +8,8 @@ import os
from calibre.customize.conversion import InputFormatPlugin, OptionRecommendation from calibre.customize.conversion import InputFormatPlugin, OptionRecommendation
from calibre.ebooks.txt.processor import convert_basic, convert_markdown, \ from calibre.ebooks.txt.processor import convert_basic, convert_markdown, \
separate_paragraphs_single_line, separate_paragraphs_print_formatted separate_paragraphs_single_line, separate_paragraphs_print_formatted, \
preserve_spaces
class TXTInput(InputFormatPlugin): class TXTInput(InputFormatPlugin):
@ -28,6 +29,9 @@ class TXTInput(InputFormatPlugin):
'an indent (either a tab or 2+ spaces) represents a paragraph. ' 'an indent (either a tab or 2+ spaces) represents a paragraph. '
'Paragraphs end when the next line that starts with an indent ' 'Paragraphs end when the next line that starts with an indent '
'is reached.')), 'is reached.')),
OptionRecommendation(name='preserve_spaces', recommended_value=False,
help=_('Normally extra spaces are condensed into a single space. '
'With this option all spaces will be displayed.')),
OptionRecommendation(name='markdown', recommended_value=False, OptionRecommendation(name='markdown', recommended_value=False,
help=_('Run the text input through the markdown pre-processor. To ' help=_('Run the text input through the markdown pre-processor. To '
'learn more about markdown see')+' http://daringfireball.net/projects/markdown/'), 'learn more about markdown see')+' http://daringfireball.net/projects/markdown/'),
@ -48,6 +52,8 @@ class TXTInput(InputFormatPlugin):
txt = separate_paragraphs_single_line(txt) txt = separate_paragraphs_single_line(txt)
if options.print_formatted_paras: if options.print_formatted_paras:
txt = separate_paragraphs_print_formatted(txt) txt = separate_paragraphs_print_formatted(txt)
if options.preserve_spaces:
txt = preserve_spaces(txt)
if options.markdown: if options.markdown:
log.debug('Running text though markdown conversion...') log.debug('Running text though markdown conversion...')

View File

@ -25,6 +25,9 @@ def convert_basic(txt, title=''):
lines.append(line.strip()) lines.append(line.strip())
txt = '\n'.join(lines) txt = '\n'.join(lines)
# Condense redundant spaces
txt = re.sub('[ ]{2,}', ' ', txt)
# Remove blank lines from the beginning and end of the document. # Remove blank lines from the beginning and end of the document.
txt = re.sub('^\s+(?=.)', '', txt) txt = re.sub('^\s+(?=.)', '', txt)
txt = re.sub('(?<=.)\s+$', '', txt) txt = re.sub('(?<=.)\s+$', '', txt)
@ -56,6 +59,11 @@ def separate_paragraphs_print_formatted(txt):
txt = re.sub('(?miu)^(\t+|[ ]{2,})(?=.)', '\n\t', txt) txt = re.sub('(?miu)^(\t+|[ ]{2,})(?=.)', '\n\t', txt)
return txt return txt
def preserve_spaces(txt):
txt = txt.replace(' ', '&nbsp;')
txt = txt.replace('\t', '&#09;')
return txt
def opf_writer(path, opf_name, manifest, spine, mi): def opf_writer(path, opf_name, manifest, spine, mi):
opf = OPFCreator(path, mi) opf = OPFCreator(path, mi)
opf.create_manifest(manifest) opf.create_manifest(manifest)