home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_891 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  4.2 KB  |  103 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, Anatoly Shipitsin <norguhtar at gmail.com>'
  7. import os
  8. import re
  9. from base64 import b64decode
  10. from lxml import etree
  11. from calibre.customize.conversion import InputFormatPlugin, OptionRecommendation
  12. from calibre import guess_type
  13. FB2NS = 'http://www.gribuser.ru/xml/fictionbook/2.0'
  14.  
  15. class FB2Input(InputFormatPlugin):
  16.     name = 'FB2 Input'
  17.     author = 'Anatoly Shipitsin'
  18.     description = 'Convert FB2 files to HTML'
  19.     file_types = set([
  20.         'fb2'])
  21.     recommendations = set([
  22.         ('level1_toc', '//h:h1', OptionRecommendation.MED),
  23.         ('level2_toc', '//h:h2', OptionRecommendation.MED),
  24.         ('level3_toc', '//h:h3', OptionRecommendation.MED)])
  25.     options = set([
  26.         OptionRecommendation(name = 'no_inline_fb2_toc', recommended_value = False, level = OptionRecommendation.LOW, help = _('Do not insert a Table of Contents at the beginning of the book.'))])
  27.     
  28.     def convert(self, stream, options, file_ext, log, accelerators):
  29.         OPFCreator = OPFCreator
  30.         import calibre.ebooks.metadata.opf2
  31.         get_metadata = get_metadata
  32.         import calibre.ebooks.metadata.meta
  33.         XLINK_NS = XLINK_NS
  34.         import calibre.ebooks.oeb.base
  35.         NAMESPACES = {
  36.             'f': FB2NS,
  37.             'l': XLINK_NS }
  38.         log.debug('Parsing XML...')
  39.         raw = stream.read()
  40.         
  41.         try:
  42.             doc = etree.fromstring(raw)
  43.         except etree.XMLSyntaxError:
  44.             doc = etree.fromstring(raw.replace('& ', '&'))
  45.  
  46.         self.extract_embedded_content(doc)
  47.         log.debug('Converting XML to HTML...')
  48.         ss = open(P('templates/fb2.xsl'), 'rb').read()
  49.         if options.no_inline_fb2_toc:
  50.             log('Disabling generation of inline FB2 TOC')
  51.             ss = re.compile('<!-- BUILD TOC -->.*<!-- END BUILD TOC -->', re.DOTALL).sub('', ss)
  52.         
  53.         styledoc = etree.fromstring(ss)
  54.         transform = etree.XSLT(styledoc)
  55.         result = transform(doc)
  56.         for img in result.xpath('//img[@src]'):
  57.             src = img.get('src')
  58.             img.set('src', self.binary_map.get(src, src))
  59.         
  60.         open('index.xhtml', 'wb').write(transform.tostring(result))
  61.         stream.seek(0)
  62.         mi = get_metadata(stream, 'fb2')
  63.         if not mi.title:
  64.             mi.title = _('Unknown')
  65.         
  66.         if not mi.authors:
  67.             mi.authors = [
  68.                 _('Unknown')]
  69.         
  70.         opf = OPFCreator(os.getcwdu(), mi)
  71.         entries = [ (f, guess_type(f)[0]) for f in os.listdir('.') ]
  72.         opf.create_manifest(entries)
  73.         opf.create_spine([
  74.             'index.xhtml'])
  75.         for img in doc.xpath('//f:coverpage/f:image', namespaces = NAMESPACES):
  76.             href = img.get('{%s}href' % XLINK_NS, img.get('href', None))
  77.             if href is not None:
  78.                 opf.guide.set_cover(os.path.abspath(href))
  79.                 continue
  80.             None if href.startswith('#') else []
  81.         
  82.         opf.render(open('metadata.opf', 'wb'))
  83.         return os.path.join(os.getcwd(), 'metadata.opf')
  84.  
  85.     
  86.     def extract_embedded_content(self, doc):
  87.         self.binary_map = { }
  88.         for elem in doc.xpath('./*'):
  89.             if 'binary' in elem.tag and elem.attrib.has_key('id'):
  90.                 ct = elem.get('content-type', '')
  91.                 fname = elem.attrib['id']
  92.                 ext = ct.rpartition('/')[-1].lower()
  93.                 if ext in ('png', 'jpeg', 'jpg'):
  94.                     fname += '.' + ext
  95.                     self.binary_map[elem.get('id')] = fname
  96.                 
  97.                 data = b64decode(elem.text.strip())
  98.                 open(fname, 'wb').write(data)
  99.                 continue
  100.         
  101.  
  102.  
  103.