home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_985 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  6.9 KB  |  183 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__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
  7. __docformat__ = 'restructuredtext en'
  8. from cStringIO import StringIO
  9. from calibre.customize.conversion import OutputFormatPlugin
  10. from calibre.customize.conversion import OptionRecommendation
  11.  
  12. class MOBIOutput(OutputFormatPlugin):
  13.     name = 'MOBI Output'
  14.     author = 'Marshall T. Vandegrift'
  15.     file_type = 'mobi'
  16.     options = set([
  17.         OptionRecommendation(name = 'rescale_images', recommended_value = False, help = _('Modify images to meet Palm device size limitations.')),
  18.         OptionRecommendation(name = 'prefer_author_sort', recommended_value = False, level = OptionRecommendation.LOW, help = _('When present, use author sort field as author.')),
  19.         OptionRecommendation(name = 'no_inline_toc', recommended_value = False, level = OptionRecommendation.LOW, help = _("Don't add Table of Contents to end of book. Useful if the book has its own table of contents.")),
  20.         OptionRecommendation(name = 'toc_title', recommended_value = None, help = _('Title for any generated in-line table of contents.')),
  21.         OptionRecommendation(name = 'dont_compress', recommended_value = False, level = OptionRecommendation.LOW, help = _('Disable compression of the file contents.')),
  22.         OptionRecommendation(name = 'personal_doc', recommended_value = '[PDOC]', help = _('Tag marking book to be filed with Personal Docs'))])
  23.     
  24.     def check_for_periodical(self):
  25.         if self.oeb.metadata.publication_type and unicode(self.oeb.metadata.publication_type[0]).startswith('periodical:'):
  26.             self.periodicalize_toc()
  27.             self.check_for_masthead()
  28.             self.opts.mobi_periodical = True
  29.         else:
  30.             self.opts.mobi_periodical = False
  31.  
  32.     
  33.     def check_for_masthead(self):
  34.         found = 'masthead' in self.oeb.guide
  35.         if not found:
  36.             self.oeb.log.debug('No masthead found in manifest, generating default mastheadImage...')
  37.             
  38.             try:
  39.                 PILImage = Image
  40.                 import PIL
  41.                 PILImage
  42.             except ImportError:
  43.                 import Image as PILImage
  44.  
  45.             raw = open(P('content_server/calibre_banner.png'), 'rb')
  46.             im = PILImage.open(raw)
  47.             of = StringIO()
  48.             im.save(of, 'GIF')
  49.             raw = of.getvalue()
  50.             (id, href) = self.oeb.manifest.generate('masthead', 'masthead')
  51.             self.oeb.manifest.add(id, href, 'image/gif', data = raw)
  52.             self.oeb.guide.add('masthead', 'Masthead Image', href)
  53.         else:
  54.             self.oeb.log.debug('Using mastheadImage supplied in manifest...')
  55.  
  56.     
  57.     def dump_toc(self, toc):
  58.         self.log('\n         >>> TOC contents <<<')
  59.         self.log('     toc.title: %s' % toc.title)
  60.         self.log('      toc.href: %s' % toc.href)
  61.         for periodical in toc.nodes:
  62.             self.log('\tperiodical title: %s' % periodical.title)
  63.             self.log('\t            href: %s' % periodical.href)
  64.             for section in periodical:
  65.                 self.log('\t\tsection title: %s' % section.title)
  66.                 self.log('\t\tfirst article: %s' % section.href)
  67.                 for article in section:
  68.                     self.log('\t\t\tarticle title: %s' % repr(article.title))
  69.                     self.log('\t\t\t         href: %s' % article.href)
  70.                 
  71.             
  72.         
  73.  
  74.     
  75.     def dump_manifest(self):
  76.         self.log('\n         >>> Manifest entries <<<')
  77.         for href in self.oeb.manifest.hrefs:
  78.             self.log('\t%s' % href)
  79.         
  80.  
  81.     
  82.     def periodicalize_toc(self):
  83.         TOC = TOC
  84.         import calibre.ebooks.oeb.base
  85.         toc = self.oeb.toc
  86.         if not toc or len(self.oeb.spine) < 3:
  87.             return None
  88.         if toc and toc[0].klass != 'periodical':
  89.             one = self.oeb.spine[0]
  90.             two = self.oeb.spine[1]
  91.             self.log('Converting TOC for MOBI periodical indexing...')
  92.             articles = { }
  93.             if toc.depth() < 3:
  94.                 self.oeb.manifest.remove(one)
  95.                 self.oeb.manifest.remove(two)
  96.                 sections = [
  97.                     TOC(klass = 'section', title = _('All articles'), href = self.oeb.spine[0].href)]
  98.                 for x in toc:
  99.                     sections[0].nodes.append(x)
  100.                 
  101.             else:
  102.                 self.oeb.manifest.remove(one)
  103.                 sections = list(toc)
  104.                 for i, x in enumerate(sections):
  105.                     x.klass = 'section'
  106.                     articles_ = list(x)
  107.                     if articles_:
  108.                         self.oeb.manifest.remove(self.oeb.manifest.hrefs[x.href])
  109.                         x.href = articles_[0].href
  110.                         continue
  111.                 
  112.             for sec in sections:
  113.                 articles[id(sec)] = []
  114.                 for a in list(sec):
  115.                     a.klass = 'article'
  116.                     articles[id(sec)].append(a)
  117.                     sec.nodes.remove(a)
  118.                 
  119.             
  120.             root = TOC(klass = 'periodical', href = self.oeb.spine[0].href, title = unicode(self.oeb.metadata.title[0]))
  121.             for s in sections:
  122.                 if articles[id(s)]:
  123.                     for a in articles[id(s)]:
  124.                         s.nodes.append(a)
  125.                     
  126.                     root.nodes.append(s)
  127.                     continue
  128.             
  129.             for x in list(toc.nodes):
  130.                 toc.nodes.remove(x)
  131.             
  132.             toc.nodes.append(root)
  133.             toc.nodes[0].href = toc.nodes[0].nodes[0].href
  134.             if self.opts.verbose > 3:
  135.                 self.dump_toc(toc)
  136.                 self.dump_manifest()
  137.             
  138.         
  139.  
  140.     
  141.     def convert(self, oeb, output_path, input_plugin, opts, log):
  142.         self.log = log
  143.         self.opts = opts
  144.         self.oeb = oeb
  145.         PALM_MAX_IMAGE_SIZE = PALM_MAX_IMAGE_SIZE
  146.         MobiWriter = MobiWriter
  147.         PALMDOC = PALMDOC
  148.         UNCOMPRESSED = UNCOMPRESSED
  149.         import calibre.ebooks.mobi.writer
  150.         MobiMLizer = MobiMLizer
  151.         import calibre.ebooks.mobi.mobiml
  152.         CaseMangler = CaseMangler
  153.         import calibre.ebooks.oeb.transforms.manglecase
  154.         SVGRasterizer = SVGRasterizer
  155.         Unavailable = Unavailable
  156.         import calibre.ebooks.oeb.transforms.rasterize
  157.         HTMLTOCAdder = HTMLTOCAdder
  158.         import calibre.ebooks.oeb.transforms.htmltoc
  159.         plugin_for_input_format = plugin_for_input_format
  160.         import calibre.customize.ui
  161.         imagemax = None if opts.rescale_images else None
  162.         if not opts.no_inline_toc:
  163.             tocadder = HTMLTOCAdder(title = opts.toc_title)
  164.             tocadder(oeb, opts)
  165.         
  166.         mangler = CaseMangler()
  167.         mangler(oeb, opts)
  168.         
  169.         try:
  170.             rasterizer = SVGRasterizer()
  171.             rasterizer(oeb, opts)
  172.         except Unavailable:
  173.             self.log.warn('SVG rasterizer unavailable, SVG will not be converted')
  174.  
  175.         mobimlizer = MobiMLizer(ignore_tables = opts.linearize_tables)
  176.         mobimlizer(oeb, opts)
  177.         self.check_for_periodical()
  178.         write_page_breaks_after_item = input_plugin is not plugin_for_input_format('cbz')
  179.         writer = None(MobiWriter, opts = 'imagemax', imagemax = 'compression' if opts.dont_compress else PALMDOC, prefer_author_sort = opts.prefer_author_sort, write_page_breaks_after_item = write_page_breaks_after_item)
  180.         writer(oeb, output_path)
  181.  
  182.  
  183.