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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL 3'
  5. __copyright__ = '2009, John Schember <john@nachtimwald.com>'
  6. __docformat__ = 'restructuredtext en'
  7. import os
  8. import re
  9. from lxml import etree
  10. from calibre.ebooks.oeb.base import XHTML, XHTML_NS, barename, namespace
  11. from calibre.ebooks.oeb.stylizer import Stylizer
  12. BLOCK_TAGS = [
  13.     'div',
  14.     'p',
  15.     'h1',
  16.     'h2',
  17.     'h3',
  18.     'h4',
  19.     'h5',
  20.     'h6',
  21.     'li',
  22.     'tr']
  23. BLOCK_STYLES = [
  24.     'block']
  25. SPACE_TAGS = [
  26.     'td']
  27.  
  28. class TXTMLizer(object):
  29.     
  30.     def __init__(self, log):
  31.         self.log = log
  32.  
  33.     
  34.     def extract_content(self, oeb_book, opts):
  35.         self.log.info('Converting XHTML to TXT...')
  36.         self.oeb_book = oeb_book
  37.         self.opts = opts
  38.         return self.mlize_spine()
  39.  
  40.     
  41.     def mlize_spine(self):
  42.         output = [
  43.             u'']
  44.         output.append(self.get_toc())
  45.         for item in self.oeb_book.spine:
  46.             self.log.debug('Converting %s to TXT...' % item.href)
  47.             stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts, self.opts.output_profile)
  48.             content = unicode(etree.tostring(item.data.find(XHTML('body')), encoding = unicode))
  49.             content = self.remove_newlines(content)
  50.             output += self.dump_text(etree.fromstring(content), stylizer)
  51.         
  52.         output = self.cleanup_text(u''.join(output))
  53.         return output
  54.  
  55.     
  56.     def remove_newlines(self, text):
  57.         self.log.debug('\tRemove newlines for processing...')
  58.         text = text.replace('\r\n', ' ')
  59.         text = text.replace('\n', ' ')
  60.         text = text.replace('\r', ' ')
  61.         return text
  62.  
  63.     
  64.     def get_toc(self):
  65.         toc = [
  66.             u'']
  67.         if getattr(self.opts, 'inline_toc', None):
  68.             self.log.debug('Generating table of contents...')
  69.             toc.append(u'%s\n\n' % _(u'Table of Contents:'))
  70.             for item in self.oeb_book.toc:
  71.                 toc.append(u'* %s\n\n' % item.title)
  72.             
  73.         
  74.         return ''.join(toc)
  75.  
  76.     
  77.     def cleanup_text(self, text):
  78.         self.log.debug('\tClean up text...')
  79.         text = text.replace(u'├é', '')
  80.         text = text.replace(u'┬á', ' ')
  81.         text = text.replace('\t+', ' ')
  82.         text = text.replace('\x0b+', ' ')
  83.         text = text.replace('\x0c+', ' ')
  84.         text = re.sub('(?<=.)%s(?=.)' % os.linesep, ' ', text)
  85.         text = re.sub('[ ]{2,}', ' ', text)
  86.         text = re.sub('\n[ ]+\n', '\n\n', text)
  87.         if self.opts.remove_paragraph_spacing:
  88.             text = re.sub('\n{2,}', '\n', text)
  89.             text = re.sub('(?imu)^(?=.)', '\t', text)
  90.         else:
  91.             text = re.sub('\n{3,}', '\n\n', text)
  92.         text = re.sub('(?imu)^[ ]+', '', text)
  93.         text = re.sub('(?imu)[ ]+$', '', text)
  94.         if self.opts.max_line_length:
  95.             max_length = self.opts.max_line_length
  96.             if self.opts.max_line_length < 25 and not (self.opts.force_max_line_length):
  97.                 max_length = 25
  98.             
  99.             short_lines = []
  100.             lines = text.splitlines()
  101.             for line in lines:
  102.                 while len(line) > max_length:
  103.                     space = line.rfind(' ', 0, max_length)
  104.                     if space != -1:
  105.                         short_lines.append(line[:space])
  106.                         line = line[space + 1:]
  107.                         continue
  108.                     if self.opts.force_max_line_length:
  109.                         short_lines.append(line[:max_length])
  110.                         line = line[max_length:]
  111.                         continue
  112.                     space = line.find(' ', max_length, len(line))
  113.                     if space != -1:
  114.                         short_lines.append(line[:space])
  115.                         line = line[space + 1:]
  116.                         continue
  117.                     short_lines.append(line)
  118.                     line = ''
  119.                 short_lines.append(line)
  120.             
  121.             text = '\n'.join(short_lines)
  122.         
  123.         return text
  124.  
  125.     
  126.     def dump_text(self, elem, stylizer, end = ''):
  127.         if not isinstance(elem.tag, basestring) or namespace(elem.tag) != XHTML_NS:
  128.             return [
  129.                 '']
  130.         text = [
  131.             '']
  132.         style = stylizer.style(elem)
  133.         if style['display'] in ('none', 'oeb-page-head', 'oeb-page-foot') or style['visibility'] == 'hidden':
  134.             return [
  135.                 '']
  136.         tag = barename(elem.tag)
  137.         in_block = False
  138.         if tag in SPACE_TAGS:
  139.             if not end.endswith('u ') and hasattr(elem, 'text') and elem.text:
  140.                 text.append(u' ')
  141.             
  142.         
  143.         if hasattr(elem, 'text') and elem.text:
  144.             text.append(elem.text)
  145.         
  146.         for item in elem:
  147.             en = u''
  148.             if len(text) >= 2:
  149.                 en = text[-1][-2:]
  150.             
  151.             text += self.dump_text(item, stylizer, en)
  152.         
  153.         if in_block:
  154.             text.append(u'\n\n')
  155.         
  156.         if hasattr(elem, 'tail') and elem.tail:
  157.             text.append(elem.tail)
  158.         
  159.         return text
  160.  
  161.  
  162.