1
# -*- coding: utf-8 -*-
4
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
5
__docformat__ = 'restructuredtext en'
9
from calibre.customize.conversion import OutputFormatPlugin, \
11
from calibre.ebooks.txt.txtml import TXTMLizer
12
from calibre.ebooks.txt.newlines import TxtNewlines, specified_newlines
14
class TXTOutput(OutputFormatPlugin):
17
author = 'John Schember'
21
OptionRecommendation(name='newline', recommended_value='system',
22
level=OptionRecommendation.LOW,
23
short_switch='n', choices=TxtNewlines.NEWLINE_TYPES.keys(),
24
help=_('Type of newline to use. Options are %s. Default is \'system\'. '
25
'Use \'old_mac\' for compatibility with Mac OS 9 and earlier. '
26
'For Mac OS X use \'unix\'. \'system\' will default to the newline '
27
'type used by this OS.') % sorted(TxtNewlines.NEWLINE_TYPES.keys())),
28
OptionRecommendation(name='output_encoding', recommended_value='utf-8',
29
level=OptionRecommendation.LOW,
30
help=_('Specify the character encoding of the output document. ' \
31
'The default is utf-8. Note: This option is not honored by all ' \
35
def convert(self, oeb_book, output_path, input_plugin, opts, log):
36
writer = TXTMLizer(log)
37
txt = writer.extract_content(oeb_book, opts)
39
log.debug('\tReplacing newlines with selected type...')
40
txt = specified_newlines(TxtNewlines(opts.newline).newline, txt)
43
if not hasattr(output_path, 'write'):
45
if not os.path.exists(os.path.dirname(output_path)) and os.path.dirname(output_path) != '':
46
os.makedirs(os.path.dirname(output_path))
47
out_stream = open(output_path, 'wb')
49
out_stream = output_path
53
out_stream.write(txt.encode(opts.output_encoding, 'replace'))