home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- from __future__ import with_statement
- __license__ = 'GPL v3'
- __copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'
- from calibre.ebooks.oeb.base import XML, XHTML, XHTML_NS
- from calibre.ebooks.oeb.base import XHTML_MIME, CSS_MIME
- from calibre.ebooks.oeb.base import element
- __all__ = [
- 'HTMLTOCAdder']
- DEFAULT_TITLE = __('Table of Contents')
- STYLE_CSS = {
- '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',
- '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' }
-
- class HTMLTOCAdder(object):
-
- def __init__(self, title = None, style = 'nested'):
- self.title = title
- self.style = style
-
-
- def config(cls, cfg):
- group = cfg.add_group('htmltoc', _('HTML TOC generation options.'))
- group('toc_title', [
- '--toc-title'], default = None, help = _('Title for any generated in-line table of contents.'))
- return cfg
-
- config = classmethod(config)
-
- def generate(cls, opts):
- return cls(title = opts.toc_title)
-
- generate = classmethod(generate)
-
- def __call__(self, oeb, context):
- if 'toc' in oeb.guide:
- return None
- if not getattr(getattr(oeb, 'toc', False), 'nodes', False):
- return None
- oeb.logger.info('Generating in-line TOC...')
- if not self.title:
- pass
- title = oeb.translate(DEFAULT_TITLE)
- style = self.style
- (id, css_href) = oeb.manifest.generate('tocstyle', 'tocstyle.css')
- oeb.manifest.add(id, css_href, CSS_MIME, data = STYLE_CSS[style])
- language = str(oeb.metadata.language[0])
- contents = element(None, XHTML('html'), nsmap = {
- None: XHTML_NS }, attrib = {
- XML('lang'): language })
- head = element(contents, XHTML('head'))
- htitle = element(head, XHTML('title'))
- htitle.text = title
- element(head, XHTML('link'), rel = 'stylesheet', type = CSS_MIME, href = css_href)
- body = element(contents, XHTML('body'), attrib = {
- 'class': 'calibre_toc' })
- h1 = element(body, XHTML('h1'), attrib = {
- 'class': 'calibre_toc_header' })
- h1.text = title
- self.add_toc_level(body, oeb.toc)
- (id, href) = oeb.manifest.generate('contents', 'contents.xhtml')
- item = oeb.manifest.add(id, href, XHTML_MIME, data = contents)
- oeb.spine.add(item, linear = False)
- oeb.guide.add('toc', 'Table of Contents', href)
-
-
- def add_toc_level(self, elem, toc):
- for node in toc:
- block = element(elem, XHTML('div'), attrib = {
- 'class': 'calibre_toc_block' })
- line = element(block, XHTML('a'), attrib = {
- 'href': node.href,
- 'class': 'calibre_toc_line' })
- line.text = node.title
- self.add_toc_level(block, node)
-
-
-
-