1
# -*- coding: utf-8 -*-
4
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
5
__docformat__ = 'restructuredtext en'
9
from calibre.customize.conversion import InputFormatPlugin
10
from calibre.ebooks.txt.processor import txt_to_markdown, opf_writer
12
class TXTInput(InputFormatPlugin):
15
author = 'John Schember'
16
description = 'Convert TXT files to HTML'
17
file_types = set(['txt'])
19
def convert(self, stream, options, file_ext, log,
21
ienc = stream.encoding if stream.encoding else 'utf-8'
22
if options.input_encoding:
23
ienc = options.input_encoding
24
log.debug('Reading text from file...')
25
txt = stream.read().decode(ienc, 'replace')
27
log.debug('Running text though markdown conversion...')
29
html = txt_to_markdown(txt)
31
raise ValueError('This txt file has malformed markup, it cannot be'
32
'converted by calibre. See http://daringfireball.net/projects/markdown/syntax')
34
log.debug('Writing html output...')
35
with open('index.html', 'wb') as index:
36
index.write(html.encode('utf-8'))
38
from calibre.ebooks.metadata.meta import get_metadata
39
log.debug('Retrieving source document metadata...')
40
mi = get_metadata(stream, 'txt')
41
manifest = [('index.html', None)]
42
spine = ['index.html']
43
log.debug('Generating manifest...')
44
opf_writer(os.getcwd(), 'metadata.opf', manifest, spine, mi)
46
return os.path.join(os.getcwd(), 'metadata.opf')