home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1002 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  4.2 KB  |  95 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. from calibre.ebooks.oeb.base import XML, XHTML, XHTML_NS
  8. from calibre.ebooks.oeb.base import XHTML_MIME, CSS_MIME
  9. from calibre.ebooks.oeb.base import element
  10. __all__ = [
  11.     'HTMLTOCAdder']
  12. DEFAULT_TITLE = __('Table of Contents')
  13. STYLE_CSS = {
  14.     'nested': '\n.calibre_toc_header {\n  text-align: center;\n}\n.calibre_toc_block {\n  margin-left: 1.2em;\n  text-indent: -1.2em;\n}\n.calibre_toc_block .calibre_toc_block {\n  margin-left: 2.4em;\n}\n.calibre_toc_block .calibre_toc_block .calibre_toc_block {\n  margin-left: 3.6em;\n}\n',
  15.     'centered': '\n.calibre_toc_header {\n  text-align: center;\n}\n.calibre_toc_block {\n  text-align: center;\n}\nbody > .calibre_toc_block {\n  margin-top: 1.2em;\n}\n' }
  16.  
  17. class HTMLTOCAdder(object):
  18.     
  19.     def __init__(self, title = None, style = 'nested'):
  20.         self.title = title
  21.         self.style = style
  22.  
  23.     
  24.     def config(cls, cfg):
  25.         group = cfg.add_group('htmltoc', _('HTML TOC generation options.'))
  26.         group('toc_title', [
  27.             '--toc-title'], default = None, help = _('Title for any generated in-line table of contents.'))
  28.         return cfg
  29.  
  30.     config = classmethod(config)
  31.     
  32.     def generate(cls, opts):
  33.         return cls(title = opts.toc_title)
  34.  
  35.     generate = classmethod(generate)
  36.     
  37.     def __call__(self, oeb, context):
  38.         if 'toc' in oeb.guide:
  39.             urlnormalize = urlnormalize
  40.             import calibre.ebooks.oeb.base
  41.             href = urlnormalize(oeb.guide['toc'].href)
  42.             if href in oeb.manifest.hrefs:
  43.                 item = oeb.manifest.hrefs[href]
  44.                 if oeb.spine.index(item) < 0:
  45.                     oeb.spine.add(item, linear = False)
  46.                 
  47.                 return None
  48.             oeb.guide.remove('toc')
  49.         
  50.         if not getattr(getattr(oeb, 'toc', False), 'nodes', False):
  51.             return None
  52.         oeb.logger.info('Generating in-line TOC...')
  53.         if not self.title:
  54.             pass
  55.         title = oeb.translate(DEFAULT_TITLE)
  56.         style = self.style
  57.         if style not in STYLE_CSS:
  58.             oeb.logger.error('Unknown TOC style %r' % style)
  59.             style = 'nested'
  60.         
  61.         (id, css_href) = oeb.manifest.generate('tocstyle', 'tocstyle.css')
  62.         oeb.manifest.add(id, css_href, CSS_MIME, data = STYLE_CSS[style])
  63.         language = str(oeb.metadata.language[0])
  64.         contents = element(None, XHTML('html'), nsmap = {
  65.             None: XHTML_NS }, attrib = {
  66.             XML('lang'): language })
  67.         head = element(contents, XHTML('head'))
  68.         htitle = element(head, XHTML('title'))
  69.         htitle.text = title
  70.         element(head, XHTML('link'), rel = 'stylesheet', type = CSS_MIME, href = css_href)
  71.         body = element(contents, XHTML('body'), attrib = {
  72.             'class': 'calibre_toc' })
  73.         h1 = element(body, XHTML('h1'), attrib = {
  74.             'class': 'calibre_toc_header' })
  75.         h1.text = title
  76.         self.add_toc_level(body, oeb.toc)
  77.         (id, href) = oeb.manifest.generate('contents', 'contents.xhtml')
  78.         item = oeb.manifest.add(id, href, XHTML_MIME, data = contents)
  79.         oeb.spine.add(item, linear = False)
  80.         oeb.guide.add('toc', 'Table of Contents', href)
  81.  
  82.     
  83.     def add_toc_level(self, elem, toc):
  84.         for node in toc:
  85.             block = element(elem, XHTML('div'), attrib = {
  86.                 'class': 'calibre_toc_block' })
  87.             line = element(block, XHTML('a'), attrib = {
  88.                 'href': node.href,
  89.                 'class': 'calibre_toc_line' })
  90.             line.text = node.title
  91.             self.add_toc_level(block, node)
  92.         
  93.  
  94.  
  95.