home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_887 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  2.6 KB  |  69 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, Marshall T. Vandegrift <llasram@gmail.com>'
  7. __docformat__ = 'restructuredtext en'
  8. import re
  9. from itertools import count
  10. from calibre.ebooks.oeb.base import XHTML_NS
  11. from calibre.ebooks.oeb.base import OEBBook
  12. from lxml.etree import XPath
  13. NSMAP = {
  14.     'h': XHTML_NS,
  15.     'html': XHTML_NS,
  16.     'xhtml': XHTML_NS }
  17. PAGE_RE = re.compile('page', re.IGNORECASE)
  18. ROMAN_RE = re.compile('^[ivxlcdm]+$', re.IGNORECASE)
  19.  
  20. def filter_name(name):
  21.     name = name.strip()
  22.     name = PAGE_RE.sub('', name)
  23.     for word in name.split():
  24.         if word.isdigit() or ROMAN_RE.match(word):
  25.             name = word
  26.             break
  27.             continue
  28.     
  29.     return name
  30.  
  31.  
  32. def build_name_for(expr):
  33.     if not expr:
  34.         counter = count(1)
  35.         return (lambda elem: str(counter.next()))
  36.     selector = XPath(expr, namespaces = NSMAP)
  37.     
  38.     def name_for(elem):
  39.         results = selector(elem)
  40.         if not results:
  41.             return ''
  42.         name = ' '.join(results)
  43.         return filter_name(name)
  44.  
  45.     return name_for
  46.  
  47.  
  48. def add_page_map(opfpath, opts):
  49.     oeb = OEBBook(opfpath)
  50.     selector = XPath(opts.page, namespaces = NSMAP)
  51.     name_for = build_name_for(opts.page_names)
  52.     idgen = (lambda .0: for n in .0:
  53. 'calibre-page-%d' % n)(count(1))
  54.     for item in oeb.spine:
  55.         data = item.data
  56.         for elem in selector(data):
  57.             name = name_for(elem)
  58.             id = elem.get('id', None)
  59.             if id is None:
  60.                 id = elem.attrib['id'] = idgen.next()
  61.             
  62.             href = '#'.join((item.href, id))
  63.             oeb.pages.add(name, href)
  64.         
  65.     
  66.     writer = None
  67.     writer.dump(oeb, opfpath)
  68.  
  69.