home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_990 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  3.9 KB  |  117 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. __license__ = 'GPL v3'
  6. __copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
  7. __docformat__ = 'restructuredtext en'
  8. import os
  9. from odf.odf2xhtml import ODF2XHTML
  10. from calibre import CurrentDir, walk
  11. from calibre.customize.conversion import InputFormatPlugin
  12.  
  13. class Extract(ODF2XHTML):
  14.     
  15.     def extract_pictures(self, zf):
  16.         if not os.path.exists('Pictures'):
  17.             os.makedirs('Pictures')
  18.         
  19.         for name in zf.namelist():
  20.             if name.startswith('Pictures'):
  21.                 data = zf.read(name)
  22.                 
  23.                 try:
  24.                     f = _[1]
  25.                     f.write(data)
  26.                 finally:
  27.                     pass
  28.  
  29.                 continue
  30.             open(name, 'wb').__exit__
  31.         
  32.  
  33.     
  34.     def __call__(self, stream, odir):
  35.         ZipFile = ZipFile
  36.         import calibre.utils.zipfile
  37.         get_metadata = get_metadata
  38.         import calibre.ebooks.metadata.meta
  39.         OPFCreator = OPFCreator
  40.         import calibre.ebooks.metadata.opf2
  41.         if not os.path.exists(odir):
  42.             os.makedirs(odir)
  43.         
  44.         CurrentDir(odir).__enter__()
  45.         
  46.         try:
  47.             print 'Extracting ODT file...'
  48.             html = self.odf2xhtml(stream)
  49.             html = html.replace('img { width: 100%; height: 100%; }', '')
  50.             
  51.             try:
  52.                 f = _[1]
  53.                 f.write(html.encode('utf-8'))
  54.             finally:
  55.                 pass
  56.  
  57.             zf = ZipFile(stream, 'r')
  58.             self.extract_pictures(zf)
  59.             stream.seek(0)
  60.             mi = get_metadata(stream, 'odt')
  61.             if not mi.authors:
  62.                 mi.authors = [
  63.                     _('Unknown')]
  64.             
  65.             opf = OPFCreator(os.path.abspath(os.getcwdu()), mi)
  66.             []([ (os.path.abspath(f), None) for f in walk(os.getcwd()) ])
  67.             opf.create_spine([
  68.                 os.path.abspath('index.xhtml')])
  69.             
  70.             try:
  71.                 f = _[3]
  72.                 opf.render(f)
  73.             finally:
  74.                 pass
  75.  
  76.             return os.path.abspath('metadata.opf')
  77.         finally:
  78.             pass
  79.  
  80.  
  81.  
  82.  
  83. class ODTInput(InputFormatPlugin):
  84.     name = 'ODT Input'
  85.     author = 'Kovid Goyal'
  86.     description = 'Convert ODT (OpenOffice) files to HTML'
  87.     file_types = set([
  88.         'odt'])
  89.     
  90.     def convert(self, stream, options, file_ext, log, accelerators):
  91.         return Extract()(stream, '.')
  92.  
  93.     
  94.     def postprocess_book(self, oeb, opts, log):
  95.         XPath = XPath
  96.         XHTML = XHTML
  97.         import calibre.ebooks.oeb.base
  98.         path = XPath('//h:p/h:div')
  99.         path2 = XPath('//h:div[@style]/h:img[@style]')
  100.         for item in oeb.spine:
  101.             root = item.data
  102.             if not hasattr(root, 'xpath'):
  103.                 continue
  104.             
  105.             for div in path(root):
  106.                 div.getparent().tag = XHTML('div')
  107.             
  108.             for img in path2(root):
  109.                 div = img.getparent()
  110.                 if 'position:relative' in div.attrib['style'] and len(div) == 1 and 'img' in div[0].tag:
  111.                     del div.attrib['style']
  112.                     continue
  113.             
  114.         
  115.  
  116.  
  117.