home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_980 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  2.4 KB  |  60 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. __license__ = 'GPL 3'
  6. __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
  7. __docformat__ = 'restructuredtext en'
  8. import re
  9. from calibre.customize.conversion import InputFormatPlugin
  10.  
  11. class MOBIInput(InputFormatPlugin):
  12.     name = 'MOBI Input'
  13.     author = 'Kovid Goyal'
  14.     description = 'Convert MOBI files (.mobi, .prc, .azw) to HTML'
  15.     file_types = set([
  16.         'mobi',
  17.         'prc',
  18.         'azw'])
  19.     
  20.     def convert(self, stream, options, file_ext, log, accelerators):
  21.         MobiReader = MobiReader
  22.         import calibre.ebooks.mobi.reader
  23.         html = html
  24.         import lxml
  25.         parse_cache = { }
  26.         
  27.         try:
  28.             mr = MobiReader(stream, log, options.input_encoding, options.debug_pipeline)
  29.             mr.extract_content('.', parse_cache)
  30.         except:
  31.             mr = MobiReader(stream, log, options.input_encoding, options.debug_pipeline, try_extra_data_fix = True)
  32.             mr.extract_content('.', parse_cache)
  33.  
  34.         raw = parse_cache.pop('calibre_raw_mobi_markup', False)
  35.         if raw:
  36.             if isinstance(raw, unicode):
  37.                 raw = raw.encode('utf-8')
  38.             
  39.             open('debug-raw.html', 'wb').write(raw)
  40.         
  41.         for f, root in parse_cache.items():
  42.             
  43.             try:
  44.                 q = _[1]
  45.                 q.write(html.tostring(root, encoding = 'utf-8', method = 'xml', include_meta_content_type = False))
  46.                 accelerators['pagebreaks'] = '//h:div[@class="mbp_pagebreak"]'
  47.             finally:
  48.                 pass
  49.  
  50.         
  51.         return mr.created_opf_path
  52.  
  53.     
  54.     def preprocess_html(self, options, html):
  55.         doubleheading = re.compile('(?P<firsthead><h(1|2)[^>]*>.+?</h(1|2)>\\s*(<(?!h\\d)[^>]*>\\s*)*)<h(1|2)(?P<secondhead>[^>]*>.+?)</h(1|2)>', re.IGNORECASE)
  56.         html = doubleheading.sub('\\g<firsthead>\n<h3' + '\\g<secondhead>' + '</h3>', html)
  57.         return html
  58.  
  59.  
  60.