home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1004 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  4.0 KB  |  81 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.             return None
  40.         if not getattr(getattr(oeb, 'toc', False), 'nodes', False):
  41.             return None
  42.         oeb.logger.info('Generating in-line TOC...')
  43.         if not self.title:
  44.             pass
  45.         title = oeb.translate(DEFAULT_TITLE)
  46.         style = self.style
  47.         (id, css_href) = oeb.manifest.generate('tocstyle', 'tocstyle.css')
  48.         oeb.manifest.add(id, css_href, CSS_MIME, data = STYLE_CSS[style])
  49.         language = str(oeb.metadata.language[0])
  50.         contents = element(None, XHTML('html'), nsmap = {
  51.             None: XHTML_NS }, attrib = {
  52.             XML('lang'): language })
  53.         head = element(contents, XHTML('head'))
  54.         htitle = element(head, XHTML('title'))
  55.         htitle.text = title
  56.         element(head, XHTML('link'), rel = 'stylesheet', type = CSS_MIME, href = css_href)
  57.         body = element(contents, XHTML('body'), attrib = {
  58.             'class': 'calibre_toc' })
  59.         h1 = element(body, XHTML('h1'), attrib = {
  60.             'class': 'calibre_toc_header' })
  61.         h1.text = title
  62.         self.add_toc_level(body, oeb.toc)
  63.         (id, href) = oeb.manifest.generate('contents', 'contents.xhtml')
  64.         item = oeb.manifest.add(id, href, XHTML_MIME, data = contents)
  65.         oeb.spine.add(item, linear = False)
  66.         oeb.guide.add('toc', 'Table of Contents', href)
  67.  
  68.     
  69.     def add_toc_level(self, elem, toc):
  70.         for node in toc:
  71.             block = element(elem, XHTML('div'), attrib = {
  72.                 'class': 'calibre_toc_block' })
  73.             line = element(block, XHTML('a'), attrib = {
  74.                 'href': node.href,
  75.                 'class': 'calibre_toc_line' })
  76.             line.text = node.title
  77.             self.add_toc_level(block, node)
  78.         
  79.  
  80.  
  81.