Output encoding options for some formats.

This commit is contained in:
John Schember 2009-06-04 06:35:51 -04:00
parent d7015bdf36
commit b665378698
5 changed files with 31 additions and 12 deletions

View File

@ -22,6 +22,11 @@ class PDBOutput(OutputFormatPlugin):
short_switch='f', choices=FORMAT_WRITERS.keys(), short_switch='f', choices=FORMAT_WRITERS.keys(),
help=(_('Format to use inside the pdb container. Choices are:')+\ help=(_('Format to use inside the pdb container. Choices are:')+\
' %s' % FORMAT_WRITERS.keys())), ' %s' % FORMAT_WRITERS.keys())),
OptionRecommendation(name='output_encoding', recommended_value='cp1252',
level=OptionRecommendation.LOW,
help=_('Specify the character encoding of the output document. ' \
'The default is cp1252. Note: This option is not honored by all ' \
'formats.')),
]) ])
def convert(self, oeb_book, output_path, input_plugin, opts, log): def convert(self, oeb_book, output_path, input_plugin, opts, log):

View File

@ -34,7 +34,7 @@ class Writer(FormatWriter):
self.log.info('Compessing data...') self.log.info('Compessing data...')
for i in range(0, len(txt_records)): for i in range(0, len(txt_records)):
self.log.debug('\tCompressing record %i' % i) self.log.debug('\tCompressing record %i' % i)
txt_records[i] = compress_doc(txt_records[i].encode('cp1252', 'replace')) txt_records[i] = compress_doc(txt_records[i])
section_lengths.append(len(txt_records[i])) section_lengths.append(len(txt_records[i]))
out_stream.seek(0) out_stream.seek(0)
@ -46,7 +46,7 @@ class Writer(FormatWriter):
def _generate_text(self, spine): def _generate_text(self, spine):
txt_writer = TxtWriter(TxtNewlines('system').newline, self.log) txt_writer = TxtWriter(TxtNewlines('system').newline, self.log)
txt = txt_writer.dump(spine) txt = txt_writer.dump(spine).encode(self.opts.output_encoding, 'replace')
txt_length = len(txt) txt_length = len(txt)

View File

@ -50,7 +50,7 @@ class Writer(FormatWriter):
def _generate_text(self, spine): def _generate_text(self, spine):
txt_writer = TxtWriter(TxtNewlines('system').newline, self.log) txt_writer = TxtWriter(TxtNewlines('system').newline, self.log)
txt = txt_writer.dump(spine) txt = txt_writer.dump(spine).encode(self.opts.output_encoding, 'replace')
txt_length = len(txt) txt_length = len(txt)

View File

@ -15,6 +15,7 @@ except ImportError:
import cStringIO import cStringIO
from calibre.customize.conversion import OutputFormatPlugin from calibre.customize.conversion import OutputFormatPlugin
from calibre.customize.conversion import OptionRecommendation
from calibre.ptempfile import TemporaryDirectory from calibre.ptempfile import TemporaryDirectory
from calibre.utils.zipfile import ZipFile from calibre.utils.zipfile import ZipFile
from calibre.ebooks.oeb.base import OEB_IMAGES from calibre.ebooks.oeb.base import OEB_IMAGES
@ -26,12 +27,20 @@ class PMLOutput(OutputFormatPlugin):
author = 'John Schember' author = 'John Schember'
file_type = 'pmlz' file_type = 'pmlz'
options = set([
OptionRecommendation(name='output_encoding', recommended_value='cp1252',
level=OptionRecommendation.LOW,
help=_('Specify the character encoding of the output document. ' \
'The default is cp1252. Note: This option is not honored by all ' \
'formats.')),
])
def convert(self, oeb_book, output_path, input_plugin, opts, log): def convert(self, oeb_book, output_path, input_plugin, opts, log):
with TemporaryDirectory('_pmlz_output') as tdir: with TemporaryDirectory('_pmlz_output') as tdir:
pmlmlizer = PMLMLizer(ignore_tables=opts.linearize_tables) pmlmlizer = PMLMLizer(ignore_tables=opts.linearize_tables)
content = pmlmlizer.extract_content(oeb_book, opts) content = pmlmlizer.extract_content(oeb_book, opts)
with open(os.path.join(tdir, 'index.pml'), 'wb') as out: with open(os.path.join(tdir, 'index.pml'), 'wb') as out:
out.write(content.encode('utf-8')) out.write(content.encode(self.opts.output_encoding, 'replace'))
self.write_images(oeb_book.manifest, tdir) self.write_images(oeb_book.manifest, tdir)

View File

@ -17,13 +17,18 @@ class TXTOutput(OutputFormatPlugin):
file_type = 'txt' file_type = 'txt'
options = set([ options = set([
OptionRecommendation(name='newline', recommended_value='system', OptionRecommendation(name='newline', recommended_value='system',
level=OptionRecommendation.LOW, level=OptionRecommendation.LOW,
short_switch='n', choices=TxtNewlines.NEWLINE_TYPES.keys(), short_switch='n', choices=TxtNewlines.NEWLINE_TYPES.keys(),
help=_('Type of newline to use. Options are %s. Default is \'system\'. ' help=_('Type of newline to use. Options are %s. Default is \'system\'. '
'Use \'old_mac\' for compatibility with Mac OS 9 and earlier. ' 'Use \'old_mac\' for compatibility with Mac OS 9 and earlier. '
'For Mac OS X use \'unix\'. \'system\' will default to the newline ' 'For Mac OS X use \'unix\'. \'system\' will default to the newline '
'type used by this OS.') % sorted(TxtNewlines.NEWLINE_TYPES.keys())), 'type used by this OS.') % sorted(TxtNewlines.NEWLINE_TYPES.keys())),
OptionRecommendation(name='output_encoding', recommended_value='utf-8',
level=OptionRecommendation.LOW,
help=_('Specify the character encoding of the output document. ' \
'The default is utf-8. Note: This option is not honored by all ' \
'formats.')),
]) ])
def convert(self, oeb_book, output_path, input_plugin, opts, log): def convert(self, oeb_book, output_path, input_plugin, opts, log):
@ -41,7 +46,7 @@ class TXTOutput(OutputFormatPlugin):
out_stream.seek(0) out_stream.seek(0)
out_stream.truncate() out_stream.truncate()
out_stream.write(txt.encode('utf-8')) out_stream.write(txt.encode(self.opts.output_encoding, 'replace'))
if close: if close:
out_stream.close() out_stream.close()