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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL 3'
  5. __copyright__ = '2010, Li Fanxi <lifanxi@freemindworld.com>'
  6. __docformat__ = 'restructuredtext en'
  7. import os
  8. import uuid
  9. from calibre.customize.conversion import InputFormatPlugin
  10. from calibre.ebooks.oeb.base import DirContainer
  11. from calibre.ebooks.snb.snbfile import SNBFile
  12. from calibre.ptempfile import TemporaryDirectory
  13. from calibre.utils.filenames import ascii_filename
  14. from lxml import etree
  15. HTML_TEMPLATE = u'<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title>%s</title></head><body>\n%s\n</body></html>'
  16.  
  17. def html_encode(s):
  18.     return s.replace(u'&', u'&').replace(u'<', u'<').replace(u'>', u'>').replace(u'"', u'"').replace(u"'", u''').replace(u'\n', u'<br/>').replace(u' ', u' ')
  19.  
  20.  
  21. class SNBInput(InputFormatPlugin):
  22.     name = 'SNB Input'
  23.     author = 'Li Fanxi'
  24.     description = 'Convert SNB files to OEB'
  25.     file_types = set([
  26.         'snb'])
  27.     options = set([])
  28.     
  29.     def convert(self, stream, options, file_ext, log, accelerators):
  30.         log.debug('Parsing SNB file...')
  31.         snbFile = SNBFile()
  32.         
  33.         try:
  34.             snbFile.Parse(stream)
  35.         except:
  36.             raise ValueError('Invalid SNB file')
  37.  
  38.         if not snbFile.IsValid():
  39.             log.debug('Invaild SNB file')
  40.             raise ValueError('Invalid SNB file')
  41.         snbFile.IsValid()
  42.         log.debug('Handle meta data ...')
  43.         create_oebbook = create_oebbook
  44.         import calibre.ebooks.conversion.plumber
  45.         oeb = create_oebbook(log, None, options, self, encoding = options.input_encoding, populate = False)
  46.         meta = snbFile.GetFileStream('snbf/book.snbf')
  47.         if meta != None:
  48.             meta = etree.fromstring(meta)
  49.             oeb.metadata.add('title', meta.find('.//head/name').text)
  50.             oeb.metadata.add('creator', meta.find('.//head/author').text, attrib = {
  51.                 'role': 'aut' })
  52.             oeb.metadata.add('language', meta.find('.//head/language').text.lower().replace('_', '-'))
  53.             oeb.metadata.add('creator', meta.find('.//head/generator').text)
  54.             oeb.metadata.add('publisher', meta.find('.//head/publisher').text)
  55.             cover = meta.find('.//head/cover')
  56.             if cover != None and cover.text != None:
  57.                 oeb.guide.add('cover', 'Cover', cover.text)
  58.             
  59.         
  60.         bookid = str(uuid.uuid4())
  61.         oeb.metadata.add('identifier', bookid, id = 'uuid_id', scheme = 'uuid')
  62.         for ident in oeb.metadata.identifier:
  63.             if 'id' in ident.attrib:
  64.                 oeb.uid = oeb.metadata.identifier[0]
  65.                 break
  66.                 continue
  67.         
  68.         
  69.         try:
  70.             tdir = _[1]
  71.             log.debug('Process TOC ...')
  72.             toc = snbFile.GetFileStream('snbf/toc.snbf')
  73.             oeb.container = DirContainer(tdir, log)
  74.             if toc != None:
  75.                 toc = etree.fromstring(toc)
  76.                 i = 1
  77.                 for ch in toc.find('.//body'):
  78.                     chapterName = ch.text
  79.                     chapterSrc = ch.get('src')
  80.                     fname = 'ch_%d.htm' % i
  81.                     data = snbFile.GetFileStream('snbc/' + chapterSrc)
  82.                     oeb.toc.add(ch.text, fname)
  83.                     (id, href) = oeb.manifest.generate(id = 'html', href = ascii_filename(fname))
  84.                     item = oeb.manifest.add(id, href, 'text/html')
  85.                     item.html_input_href = fname
  86.                     oeb.spine.add(item, True)
  87.                     i = i + 1
  88.                 
  89.                 imageFiles = snbFile.OutputImageFiles(tdir)
  90.                 for f, m in imageFiles:
  91.                     (id, href) = oeb.manifest.generate(id = 'image', href = ascii_filename(f))
  92.                     item = oeb.manifest.add(id, href, m)
  93.                     item.html_input_href = f
  94.                 
  95.         finally:
  96.             pass
  97.  
  98.         return oeb
  99.  
  100.  
  101.