home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1072 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  7.2 KB  |  223 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 re
  8. from calibre import prepare_string_for_xml
  9. from calibre.ebooks.oeb.base import XHTML, XHTML_NS, barename, namespace
  10. from calibre.ebooks.oeb.stylizer import Stylizer
  11. from calibre.ebooks.rb import unique_name
  12. TAGS = [
  13.     'b',
  14.     'big',
  15.     'blockquote',
  16.     'br',
  17.     'center',
  18.     'code',
  19.     'div',
  20.     'h1',
  21.     'h2',
  22.     'h3',
  23.     'h4',
  24.     'h5',
  25.     'h6',
  26.     'hr',
  27.     'i',
  28.     'li',
  29.     'ol',
  30.     'p',
  31.     'pre',
  32.     'small',
  33.     'sub',
  34.     'sup',
  35.     'ul']
  36. LINK_TAGS = [
  37.     'a']
  38. IMAGE_TAGS = [
  39.     'img']
  40. STYLES = [
  41.     ('font-weight', {
  42.         'bold': 'b',
  43.         'bolder': 'b' }),
  44.     ('font-style', {
  45.         'italic': 'i' }),
  46.     ('text-align', {
  47.         'center': 'center' })]
  48.  
  49. class RBMLizer(object):
  50.     
  51.     def __init__(self, log, name_map = { }):
  52.         self.log = log
  53.         self.name_map = name_map
  54.         self.link_hrefs = { }
  55.  
  56.     
  57.     def extract_content(self, oeb_book, opts):
  58.         self.log.info('Converting XHTML to RB markup...')
  59.         self.oeb_book = oeb_book
  60.         self.opts = opts
  61.         return self.mlize_spine()
  62.  
  63.     
  64.     def mlize_spine(self):
  65.         self.link_hrefs = { }
  66.         output = [
  67.             u'<HTML><HEAD><TITLE></TITLE></HEAD><BODY>']
  68.         output.append(self.get_cover_page())
  69.         output.append(u'ghji87yhjko0Caliblre-toc-placeholder-for-insertion-later8ujko0987yjk')
  70.         output.append(self.get_text())
  71.         output.append(u'</BODY></HTML>')
  72.         output = ''.join(output).replace(u'ghji87yhjko0Caliblre-toc-placeholder-for-insertion-later8ujko0987yjk', self.get_toc())
  73.         output = self.clean_text(output)
  74.         return output
  75.  
  76.     
  77.     def get_cover_page(self):
  78.         output = u''
  79.         if 'cover' in self.oeb_book.guide:
  80.             if self.name_map.get(self.oeb_book.guide['cover'].href, None):
  81.                 output += '<IMG SRC="%s">' % self.name_map[self.oeb_book.guide['cover'].href]
  82.             
  83.         
  84.         if 'titlepage' in self.oeb_book.guide:
  85.             self.log.debug('Generating cover page...')
  86.             href = self.oeb_book.guide['titlepage'].href
  87.             item = self.oeb_book.manifest.hrefs[href]
  88.             if item.spine_position is None:
  89.                 stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts, self.opts.output_profile)
  90.                 output += ''.join(self.dump_text(item.data.find(XHTML('body')), stylizer, item))
  91.             
  92.         
  93.         return output
  94.  
  95.     
  96.     def get_toc(self):
  97.         toc = [
  98.             u'']
  99.         if self.opts.inline_toc:
  100.             self.log.debug('Generating table of contents...')
  101.             toc.append(u'<H1>%s</H1><UL>\n' % _('Table of Contents:'))
  102.             for item in self.oeb_book.toc:
  103.                 if item.href in self.link_hrefs.keys():
  104.                     toc.append('<LI><A HREF="#%s">%s</A></LI>\n' % (self.link_hrefs[item.href], item.title))
  105.                     continue
  106.                 self.oeb.warn('Ignoring toc item: %s not found in document.' % item)
  107.             
  108.             toc.append('</UL>')
  109.         
  110.         return ''.join(toc)
  111.  
  112.     
  113.     def get_text(self):
  114.         output = [
  115.             u'']
  116.         for item in self.oeb_book.spine:
  117.             self.log.debug('Converting %s to RocketBook HTML...' % item.href)
  118.             stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts, self.opts.output_profile)
  119.             output.append(self.add_page_anchor(item))
  120.             output += self.dump_text(item.data.find(XHTML('body')), stylizer, item)
  121.         
  122.         return ''.join(output)
  123.  
  124.     
  125.     def add_page_anchor(self, page):
  126.         return self.get_anchor(page, '')
  127.  
  128.     
  129.     def get_anchor(self, page, aid):
  130.         aid = '%s#%s' % (page.href, aid)
  131.         if aid not in self.link_hrefs.keys():
  132.             self.link_hrefs[aid] = 'calibre_link-%s' % len(self.link_hrefs.keys())
  133.         
  134.         aid = self.link_hrefs[aid]
  135.         return u'<A NAME="%s"></A>' % aid
  136.  
  137.     
  138.     def clean_text(self, text):
  139.         anchors = set(re.findall('(?<=<A NAME=").+?(?="></A>)', text))
  140.         links = set(re.findall('(?<=<A HREF="#).+?(?=">)', text))
  141.         for unused in anchors.difference(links):
  142.             text = text.replace('<A NAME="%s"></A>' % unused, '')
  143.         
  144.         return text
  145.  
  146.     
  147.     def dump_text(self, elem, stylizer, page, tag_stack = []):
  148.         if not isinstance(elem.tag, basestring) or namespace(elem.tag) != XHTML_NS:
  149.             return [
  150.                 u'']
  151.         text = [
  152.             u'']
  153.         style = stylizer.style(elem)
  154.         if style['display'] in ('none', 'oeb-page-head', 'oeb-page-foot') or style['visibility'] == 'hidden':
  155.             return [
  156.                 u'']
  157.         tag = barename(elem.tag)
  158.         tag_count = 0
  159.         rb_tag = None if tag in IMAGE_TAGS else namespace(elem.tag) != XHTML_NS if tag in TAGS else None
  160.         if rb_tag:
  161.             tag_count += 1
  162.             text.append('<%s>' % rb_tag)
  163.             tag_stack.append(rb_tag)
  164.         
  165.         if tag in LINK_TAGS:
  166.             href = elem.get('href')
  167.             if href:
  168.                 href = page.abshref(href)
  169.                 if '://' not in href:
  170.                     if '#' not in href:
  171.                         href += '#'
  172.                     
  173.                     if href not in self.link_hrefs.keys():
  174.                         self.link_hrefs[href] = 'calibre_link-%s' % len(self.link_hrefs.keys())
  175.                     
  176.                     href = self.link_hrefs[href]
  177.                     text.append('<A HREF="#%s">' % href)
  178.                 
  179.                 tag_count += 1
  180.                 tag_stack.append('A')
  181.             
  182.         
  183.         id_name = elem.get('id')
  184.         if id_name:
  185.             text.append(self.get_anchor(page, id_name))
  186.         
  187.         for s in STYLES:
  188.             style_tag = s[1].get(style[s[0]], None)
  189.             if style_tag:
  190.                 style_tag = style_tag.upper()
  191.                 tag_count += 1
  192.                 text.append('<%s>' % style_tag)
  193.                 tag_stack.append(style_tag)
  194.                 continue
  195.         
  196.         if hasattr(elem, 'text') and elem.text:
  197.             text.append(prepare_string_for_xml(elem.text))
  198.         
  199.         for item in elem:
  200.             text += self.dump_text(item, stylizer, page, tag_stack)
  201.         
  202.         close_tag_list = []
  203.         for i in range(0, tag_count):
  204.             close_tag_list.insert(0, tag_stack.pop())
  205.         
  206.         text += self.close_tags(close_tag_list)
  207.         if hasattr(elem, 'tail') and elem.tail:
  208.             text.append(prepare_string_for_xml(elem.tail))
  209.         
  210.         return text
  211.  
  212.     
  213.     def close_tags(self, tags):
  214.         text = [
  215.             u'']
  216.         for i in range(0, len(tags)):
  217.             tag = tags.pop()
  218.             text.append('</%s>' % tag)
  219.         
  220.         return text
  221.  
  222.  
  223.