~ubuntu-branches/ubuntu/karmic/calibre/karmic

« back to all changes in this revision

Viewing changes to src/calibre/ebooks/txt/input.py

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-07-30 12:49:41 UTC
  • mfrom: (1.3.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090730124941-qjdsmri25zt8zocn
Tags: 0.6.3+dfsg-0ubuntu1
* New upstream release. Please see http://calibre.kovidgoyal.net/new_in_6/
  for the list of new features and changes.
* remove_postinstall.patch: Update for new version.
* build_debug.patch: Does not apply any more, disable for now. Might not be
  necessary any more.
* debian/copyright: Fix reference to versionless GPL.
* debian/rules: Drop obsolete dh_desktop call.
* debian/rules: Add workaround for weird Python 2.6 setuptools behaviour of
  putting compiled .so files into src/calibre/plugins/calibre/plugins
  instead of src/calibre/plugins.
* debian/rules: Drop hal fdi moving, new upstream version does not use hal
  any more. Drop hal dependency, too.
* debian/rules: Install udev rules into /lib/udev/rules.d.
* Add debian/calibre.preinst: Remove unmodified
  /etc/udev/rules.d/95-calibre.rules on upgrade.
* debian/control: Bump Python dependencies to 2.6, since upstream needs
  it now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
 
 
3
__license__ = 'GPL 3'
 
4
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
 
5
__docformat__ = 'restructuredtext en'
 
6
 
 
7
import os
 
8
 
 
9
from calibre.customize.conversion import InputFormatPlugin
 
10
from calibre.ebooks.txt.processor import txt_to_markdown, opf_writer
 
11
 
 
12
class TXTInput(InputFormatPlugin):
 
13
 
 
14
    name        = 'TXT Input'
 
15
    author      = 'John Schember'
 
16
    description = 'Convert TXT files to HTML'
 
17
    file_types  = set(['txt'])
 
18
 
 
19
    def convert(self, stream, options, file_ext, log,
 
20
                accelerators):
 
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')
 
26
 
 
27
        log.debug('Running text though markdown conversion...')
 
28
        try:
 
29
            html = txt_to_markdown(txt)
 
30
        except RuntimeError:
 
31
            raise ValueError('This txt file has malformed markup, it cannot be'
 
32
                'converted by calibre. See http://daringfireball.net/projects/markdown/syntax')
 
33
 
 
34
        log.debug('Writing html output...')
 
35
        with open('index.html', 'wb') as index:
 
36
            index.write(html.encode('utf-8'))
 
37
 
 
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)
 
45
 
 
46
        return os.path.join(os.getcwd(), 'metadata.opf')
 
47